发表于: 2020-06-02 23:38:31
1 1539
加油!!!
今天完成的事:
SSM整合
创建Maven工程,添加web支持
导入相关pom依赖
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
建立基本的结构和配置框架
配置置整合的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
编写Mybatis层
编写数据库配置文件db.properties
IDEA关联数据库
创建mybatis的核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 别名-->
<typeAliases>
<package name="com.ptt.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.ptt.dao.BmbMapper"/>
</mappers>
</configuration>
编写数据库对应的实体类com.ptt.pojo.Bmb
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Bmb {
private Integer id;
private String name;
private String qq;
private String type;
private String jointime;
private String school;
private String studyid;
private String dailylink;
private String hope;
private Timestamp create_at;
private Timestamp update_at;
}
编写dao层的Mapper接口
public interface BmbMapper {
/**
* 添加用户
*/
int saveBmb(Bmb bmb);
/**
* 根据id删除用户
*/
int deleteBmbById(Integer id);
/**
* 修改用户
*/
int updateBmb(Bmb bmb);
/**
* 根据id查询单个用户
*/
Bmb getBmbById(Integer id);
/**
* 查询所有操作
*/
List<Bmb> queryAllBmb();
}
编写对应的Mapper.xml配置文件,实现增删改查操作
<mapper namespace="com.ptt.dao.BmbMapper">
<!--添加用户-->
<insert id="saveBmb" parameterType="Bmb">
insert into bmb(name,qq,type,jointime,school,studyid,dailylink,hope,create_at) values
(#{name},#{qq},#{type},#{jointime},#{school},#{studyid},#{dailylink},#{hope},now())
</insert>
<!--删除用户-->
<delete id = "deleteBmbById" parameterType="Integer">
delete from bmb where id = #{bmbid}
</delete>
<!--修改用户-->
<update id="updateBmb" parameterType="Bmb">
update bmb set name = #{name}, qq = #{qq},type = #{type},jointime = #{jointime} where id = #{id}
</update>
<!--查询单个用户-->
<select id="getBmbById" resultType="Bmb">
select * from bmb where id = #{bmbid}
</select>
<!--配置查询所有-->
<select id="queryAllBmb" resultType="Bmb">
select * from bmb
</select>
编写Service层的接口和实现类
public interface BmbService {
/**
* 添加用户
*/
int saveBmb(Bmb bmb);
/**
* 根据id删除用户
*/
int deleteBmbById(Integer id);
/**
* 修改用户
*/
int updateBmb(Bmb bmb);
/**
* 根据id查询单个用户
*/
Bmb getBmbById(Integer id);
/**
* 查询所有操作
*/
List<Bmb> queryAllBmb();
public class BmbServiceImpl implements BmbService {
private BmbMapper bmbMapper;
public void setBmbMapper(BmbMapper bmbMapper) {
this.bmbMapper = bmbMapper;
}
public int saveBmb(Bmb bmb) {
return bmbMapper.saveBmb(bmb);
}
public int deleteBmbById(Integer id) {
return bmbMapper.deleteBmbById(id);
}
public int updateBmb(Bmb bmb) {
return bmbMapper.updateBmb(bmb);
}
public Bmb getBmbById(Integer id) {
return bmbMapper.getBmbById(id);
}
public List<Bmb> queryAllBmb() {
return bmbMapper.queryAllBmb();
}
}
到这里,底层需求操作编写完成
Spring层
配置Spring整合Mybatis的配置文件Spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<context:property-placeholder location="classpath:log4j.properties"/>
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.ptt.dao"/>
</bean>
</beans>
Spring整合service层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描service相关的bean -->
<context:component-scan base-package="com.ptt.service" />
<!--BmbServiceImpl注入到IOC容器中-->
<bean id="BmbServiceImpl" class="com.ptt.service.BmbServiceImpl">
<property name="bmbMapper" ref="bmbMapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Spring层完成
配置SpringMVC层
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
配置SpringMVC的核心配置文件spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.ptt.controller"/>
</beans>
编写Spring配置整合文件,applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
配置文件完成!
编写Controller层和视图层
@Controller
@RequestMapping("/bmb")
public class BmbController {
@Autowired
@Qualifier("BmbServiceImpl")
private BmbService bmbService;
@RequestMapping("/allBmb")
public String list(Model model){
List<Bmb> list = bmbService.queryAllBmb();
for (Bmb bmb : list) {
System.out.println("data is : "+bmb);
bmb.getDailylink();
}
model.addAttribute("list",list);
return "allBmb";
}
//添加成员
@RequestMapping("/toAddBmb")
public String toAddPeople(){
return "addBmb";
}
@RequestMapping("/addBmb")
public String addPeople(Bmb bmb){
System.out.println(bmb);
bmbService.saveBmb(bmb);
return "redirect:/bmb/allBmb";
}
//修改一条记录
@RequestMapping(value = "/toUpdateBmb")
public String toUpdateBmb(Model model,int id){
Bmb bmb = bmbService.getBmbById(id);
System.out.println(bmb);
model.addAttribute("Qbmb",bmb);
return "updateBmb";
}
@RequestMapping("/updateBmb")
public String updateBmb(Model model,Bmb bmb){
System.out.println(bmb);
bmbService.updateBmb(bmb);
Bmb bmb1 = bmbService.getBmbById(bmb.getId());
model.addAttribute("bmb",bmb1);
return "redirect:/bmb/allBmb";
}
//删除一本书籍
@RequestMapping("/del/{id}")
public String deleteBook(@PathVariable("id") int id) {
bmbService.deleteBmbById(id);
return "redirect:/bmb/allBmb";
}
首页index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
<title>首页</title>
<style type="text/css">
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h3 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 4px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/bmb/allBmb">点击进入列表页</a>
</h3>
</body>
</html>
报名表信息列表页面allBmb.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生信息列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>学生信息列表 —— 显示所有学生信息</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="${pageContext.request.contextPath}/bmb/toAddBmb">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>QQ</th>
<th>修真类型</th>
<th>加入时间</th>
<th>学校</th>
<th>学号</th>
<th>日报链接</th>
<th>口号</th>
<th>创建时间</th>
<th>更新时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="bmb" items="${requestScope.get('list')}">
<tr>
<td>${bmb.getId()}</td>
<td>${bmb.getName()}</td>
<td>${bmb.getQq()}</td>
<td>${bmb.getType()}</td>
<td>${bmb.getJointime()}</td>
<td>${bmb.getSchool()}</td>
<td>${bmb.getStudyid()}</td>
<td>${bmb.getDailylink()}</td>
<td>${bmb.getHope()}</td>
<td>${bmb.getCreate_at()}</td>
<td>${bmb.getUpdate_at()}</td>
<td>
<a href="${pageContext.request.contextPath}/bmb/toUpdateBmb?id=${bmb.getId()}">更改</a> |
<a href="${pageContext.request.contextPath}/bmb/del/${bmb.getId()}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
添加成员信息 addBmb.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增学员</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增学员</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/bmb/addBmb" method="post">
编号:<input type="text" name="id"><br><br><br>
姓名:<input type="text" name="name"><br><br><br>
QQ:<input type="text" name="qq"><br><br><br>
修正类型:<input type="text" name="type"><br><br><br>
加入时间:<input type="text" name="jointime"><br><br><br>
学校:<input type="text" name="school"><br><br><br>
学号:<input type="text" name="studyid"><br><br><br>
日报链接:<input type="text" name="dailylink"><br><br><br>
口号:<input type="text" name="hope"><br><br><br>
<input type="submit" value="添加">
</form>
</div>
更新成员信息update.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改信息</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>修改信息</small>
</h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/bmb/updateBmb" method="post">
<input type="hidden" name="id" value="${Qbmb.getId()}"/>
姓名:<input type="text" name="name" value="${Qbmb.getName()}"/>
QQ:<input type="text" name="qq" value="${Qbmb.getQq()}"/>
修真类型:<input type="text" name="type" value="${Qbmb.getType()}"/>
加入时间:<input type="text" name="jointime" value="${Qbmb.getJointime()}"/>
学校:<input type="text" name="school" value="${Qbmb.getSchool()}"/>
学号:<input type="text" name="studyid" value="${Qbmb.getStudyid()}"/>
日报链接:<input type="text" name="dailylink" value="${Qbmb.getDailylink()}"/>
口号:<input type="text" name="hope" value="${Qbmb.getHope()}"/>
<input type="submit" value="提交">
</form>
</div>
配置Tomcat,进行运行!
添加一条信息
修改一条信息
删除一条信息
ok!
最后的结构
明天计划的事:任务二
遇到的困难:已经找师兄解决
收获:很多时候单词写错是个大的漏洞,以至于发生错误不会第一时间往那边想,还有就是报错信息不会仔细看
评论