前言

网上发布了不少关于搭建 Spring+SpringMVC+Mybatis 的技术文章,但是基本都是缺斤少两。我利用闲余时间自己搭建了一个完整的项目,希望对各位coder有所帮助,此项目仅供参考,自己搭建一遍才会有所收获。搭建过程中出现什么问题可以提出来大家一起解决

项目源码

流程:搭建maven项目 > 搭建SpriingMVC > 搭建Spring > 搭建Mybatis > 新建各种类(controller/service…)

PS:不想看文章,可以忽略文章内容直接去看代码源码
JDK1.8.0_201百度云盘链接 提取码:ow66
Apache-maven-3.6.0百度云盘链接 提取码:jcu1
Apache-tomcat-8.5.24百度云盘链接 提取码:sm45
Github 项目源码链接

开发环境

  • 开发工具:Eclipse
  • 框架选型:SpringMVCSpringMybatis
  • 数据库:MySQL
  • Jar包依赖方式:apache-maven-3.6.0
  • JDK版本:jdk1.8.0_201
  • Tomcat容器:apache-tomcat-8.5.24

创建Maven项目

新建成功的项目结构:


新建项目只有一个 src/main/resources
并且项目会出现错误,而且项目报下面的 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Patht 错误



解决方案:右击项目 > 点击Properties > 点击Java Build Path >选择Library > 点击Add Library > 选择Server Runtime > Apache Tomcat7 服务器(Tomcat8 需要 web版本3.0

PS:注意:Eclipse中需要先配置好Tomcat【Windows > Preferences > Server > Runtime Environments】





点击Apply,干掉项目错误后,项目结构发生了改变,自动生成了src/main/java和src/test/java目录



此时,新建项目的jre版本是1.5,web.xml版本为2.3。我们需要对版本进行修改
修改pom.xml中的配置



代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<build>
<finalName>Demo_SSM_Maven</finalName>
<plugins>
<!-- 根据自己的JRE版本修改maven默认的JRE编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

修改完配置,会出现下面这个问题

解决方法:只需要更新maven。右击项目> 点击Maven >点击Update Project..

把jre 指定到Workspace default JRE

修改web.xml版本
默认的Dynamic Web Module为2.3,使用Tomcat 8.5,需要修改为3.0

  • 方法一:修改maven工程所在目录下org.eclipse.wst.common.project.facet.core.xml
    1
    2
    3
    4
    5
    6
    7
    8
    <?xml version="1.0" encoding="UTF-8"?>
    <faceted-project>
    <fixed facet="wst.jsdt.web"/>
    <installed facet="java" version="1.8"/>
    <!—2.3修改为3.0 -->
    <installed facet="jst.web" version="2.3"/>
    <installed facet="wst.jsdt.web" version="1.0"/>
    </faceted-project>
  • 方法二:右击项目 > 点击Properties > 点击Project Facets(项目模板)

    如下图,可以看到Dynamic Web Module版本为2.3

    直接修改3.0会出现错误不允许修改

    解决方法:先取消Dynamic Web Module前面的勾选,再选择3.0,然后Apple and Close

    重新打开这个页面,对Dynamic Web Module进行勾选。
    这时候先不要着急保存关闭。点击下面的Furter configuration avail…

    修改Content directory为webapp,并勾选下面,如果保存保存保存

    最后的项目结构是这样的

    此时项目中的web.xml是下图这样的

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>Demo_SSM_Maven</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

搭建SpringMVC

Maven项目搭建好了以后,就开始搭建SpringMVC框架

  1. pom.xml文件中添加springmvc依赖
    1
    2
    3
    4
    5
    6
    <!-- springMVC -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.14.RELEASE</version>
    </dependency>
  2. 创建springmvc配置文件

    在src/main/resource下创建spring-mvc.xml,内容如下,配置好 里的路径

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <!-- 开启注解 -->
    <mvc:annotation-driven />
    <!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
    <context:component-scan base-package="com.ryx.demo.controller"></context:component-scan>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/views/" />
    <property name="suffix" value=".jsp"></property>
    </bean>
    </beans>
  3. 在web.xml中添加配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!-- 定义前端控制器springmvc -->
    <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 指定路径 -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!-- 随spring启动而启动 -->
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
  4. 创建一个controller测试SpringMVC框架

    可以使用PostMan接口测试工具进行测试
    http://127.0.0.1:8080/Demo_SSM_Maven/demo/test

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    @RequestMapping("/demo")
    @Controller
    public class studentController {

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
    return "hello,word";
    }
    }

搭建Spring

SpringMVC框架搭建成功后,就开始搭建Spring框架

PS:由于在依赖springmvc的时候已经添加了许多spring相关包了,所以此时不需要添加额外的包,可以直接创建配置文件了。

  1. 创建spring-context.xml配置文件

    src/main/resource下创建spring-context.xml,内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <?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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    </beans>

    温馨提示:如果xml文件头文件报错 ,并显示一下错误

    解决方式:window > Prefereces > XML > XML Files > Validation > 取消掉Honour all XML schema locations 前面的对勾

  2. web.xml中配置spring

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <!-- 配置适配器spring -->
    <listener>
    <description>启动spring容器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context.xml</param-value>
    </context-param>
  3. pom.xml中配置 数据源+数据库 的依赖jar包(我用的数据源是C3P0)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <!-- 数据库连接 //start -->
    <!-- c3p0 数据库连接池 -->
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
    </dependency>
    <!-- 数据库 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
    </dependency>
    <!-- 数据库连接 //end -->
  4. 在spring-context.xml配置c3p0数据源
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!-- 配置c3p0 -->
    <!-- 连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost/demo?characterEncoding=utf8&amp;serverTimezone=UTC"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123456"></property>
    <property name="minPoolSize" value="1"></property>
    <property name="maxPoolSize" value="5"></property>
    <property name="initialPoolSize" value="1"></property>
    <property name="acquireIncrement" value="1"></property>
    </bean>
  5. 配置spring声明式事务管理,pom.xml中添加 声明式事务的依赖jar包
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <!-- spring声明式事务管理 //start -->
    <!-- spring-tx -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.14.RELEASE</version>
    </dependency>
    <!-- spring-jdbc -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.14.RELEASE</version>
    </dependency>
    <!-- spring声明式事务管理 //end -->
  6. 在spring-context.xml配置事务管理器
    1
    2
    3
    4
    5
    6
    7
    <!-- 配置spring声明式事务管理 -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 使用注解来控制事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

搭建Mybatis

配置好SpringMVC、Spring后,开始搭建Mybatis

  1. pom.xml中添加Mybatis的依赖jar包
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <!-- Mybatis //start -->
    <!-- mybatis -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.1</version>
    </dependency>
    <!-- mybatis-spring//无缝整合mybatis+spring -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
    </dependency>
    <!-- cglib代理 -->
    <dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.2</version>
    </dependency>

    <!-- Mybatis //end -->
  2. 创建mybatis-config.xml配置文件

    src/main/resource下创建mybatis-config.xml,内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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>
    <settings>
    <!-- 全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存 -->
    <setting name="cacheEnabled" value="true" />
    <!-- 设置超时时间,它决定驱动等待数据库响应的秒数 -->
    <setting name="defaultStatementTimeout" value="3000" />
    <!-- 是否开启自动驼峰命名规则(camel case)映射 -->
    <setting name="mapUnderscoreToCamelCase" value="true" />
    <!-- 代理 -->
    <!-- 指定 Mybatis 创建具有延迟加载能力的对象所用到的代理工具 -->
    <setting name="proxyFactory" value="CGLIB" />
    <!-- 延迟加载 -->
    <setting name="lazyLoadingEnabled" value="true" />
    </settings>

    </configuration>
  3. spring-context.xml中配置mybatis
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!-- 配置mybatis, 绑定c3p0 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <property name="mapperLocations" >
    <list>
    <value>classpath:mapper/*.xml</value>
    </list>
    </property>
    </bean>

    <!-- 扫描生成所有dao层 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.ryx.demo.dao"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

编写Java业务逻辑

创建entity、controller、servcie . . . .class

项目结构如下

我这里用到了阿里的fastjson,用的时候需要在pom.xml中引入依赖

1
2
3
4
5
6
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>

Po实体类层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
public class Student {
private int sid;
private String sname;
private String adress;
private String username;
private String password;
private String createtime;
private String updatetime;

public Student() {
super();
// TODO Auto-generated constructor stub
}

public Student(int sid, String sname, String adress, String username, String password, String createtime,
String updatetime) {
super();
this.sid = sid;
this.sname = sname;
this.adress = adress;
this.username = username;
this.password = password;
this.createtime = createtime;
this.updatetime = updatetime;
}

public Student(String sname, String adress, String createtime) {
super();
this.sname = sname;
this.adress = adress;
this.createtime = createtime;
}

public Student(int sid, String sname, String adress, String updatetime) {
super();
this.sid = sid;
this.sname = sname;
this.adress = adress;
this.updatetime = updatetime;
}

public int getSid() {
return sid;
}

public void setSid(int sid) {
this.sid = sid;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public String getAdress() {
return adress;
}

public void setAdress(String adress) {
this.adress = adress;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getCreatetime() {
return createtime;
}

public void setCreatetime(String createtime) {
this.createtime = createtime;
}

public String getUpdatetime() {
return updatetime;
}

public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}

@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", adress=" + adress + ", username=" + username
+ ", password=" + password + ", createtime=" + createtime + ", updatetime=" + updatetime + "]";
}
}

Controller控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;

@RequestMapping("/student")
@Controller
public class StudentController {

@Autowired
public IStudentService service;

@RequestMapping("/test")
@ResponseBody
public String test() {
return "hello,word";
}

/**
* 查询所有学生
* @return
*/
@ResponseBody
@RequestMapping(value="/queryStudentList",produces = "text/plain;charset=UTF-8")
public String queryStudentList() {
List<Student> list = service.queryStudentList();
return JSON.toJSONString(list);
}

/**
* 通过id查询学生
* @return
*/
@ResponseBody
@RequestMapping(value="/queryStudentById",produces = "text/plain;charset=UTF-8")
public String queryStudentById() {
Student student = service.queryStudentById(1002);
return JSON.toJSONString(student);
}

/**
* 添加学生信息
* @return
*/
@ResponseBody
@RequestMapping(value="/addStudent",produces = "text/plain;charset=UTF-8")
public String addStudent() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Student student = new Student("测试员", "四川成都",sdf.format(new Date()));
int i = service.addStudent(student);
return "添加成功";
}

/**
* 删除学生信息
* @return
*/
@ResponseBody
@RequestMapping(value="/deleteStudent",produces = "text/plain;charset=UTF-8")
public String deleteStudent() {
int i = service.deleteStudent(1009);
return "删除成功";
}

/**
* 修改学生信息
* @return
*/
@ResponseBody
@RequestMapping(value="/updateStudent",produces = "text/plain;charset=UTF-8")
public String updateStudent() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Student student = new Student(1009, "测试专员", "西藏拉萨",sdf.format(new Date()));
int i = service.updateStudent(student);
return "修改成功";
}
}

Service层

1
2
3
4
5
6
7
public interface IStudentService {
public List<Student> queryStudentList();
public Student queryStudentById(int sid);
public int addStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(int sid);
}

ServiceImpl业务逻辑层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StudentServiceImpl implements IStudentService{

@Autowired
StudentMapper mapper;

@Override
public List<Student> queryStudentList() {
// TODO Auto-generated method stub
return mapper.queryStudentList();
}

@Override
public Student queryStudentById(int sid) {
// TODO Auto-generated method stub
return mapper.queryStudentById(sid);
}

@Override
public int addStudent(Student student) {
// TODO Auto-generated method stub
return mapper.addStudent(student);
}

@Override
public int updateStudent(Student student) {
// TODO Auto-generated method stub
return mapper.updateStudent(student);
}

@Override
public int deleteStudent(int sid) {
// TODO Auto-generated method stub
return mapper.deleteStudent(sid);
}
}

dao层

1
2
3
4
5
6
7
8
9
10
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface StudentMapper {
List<Student> queryStudentList();
Student queryStudentById(@Param("id")int sid);
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int sid);
}

mapper层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ryx.demo.dao.StudentMapper">

<resultMap id="BaseResultMap" type="com.ryx.demo.po.Student">
<id column="sid" property="sid" />
<result column="sname" property="sname" />
<result column="adress" property="adress" />
</resultMap>

<!-- 查询所有用户 -->
<select id="queryStudentList" resultMap="BaseResultMap">
select * from student
</select>

<!-- 通过id查询用户 -->
<select id="queryStudentById" parameterType="int" resultType="com.ryx.demo.po.Student">
select * from student where sid = #{id}
</select>

<!-- 添加用户 -->
<insert id="addStudent" parameterType="com.ryx.demo.po.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="sname!=null and sname!=''">sname,</if>
<if test="adress!=null and adress!=''">adress,</if>
<if test="createtime!=null and createtime!=''">createtime,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="sname!=null and sname!=''">#{sname},</if>
<if test="adress!=null and adress!=''">#{adress},</if>
<if test="createtime!=null and createtime!=''">#{createtime},</if>
</trim>
</insert>

<!-- 修改用户信息 -->
<update id="updateStudent" parameterType="com.ryx.demo.po.Student">
update student
<trim prefix="SET" suffixOverrides=",">
<if test="sname!=null and sname!=''">sname=#{sname},</if>
<if test="adress!=null and adress!=''">adress=#{adress},</if>
<if test="updatetime!=null and updatetime!=''">updatetime=#{updatetime},</if>
</trim>
where sid=#{sid}
</update>

<!-- 删除用户 -->
<delete id="deleteStudent" parameterType="int">
delete from student where sid = #{value}
</delete>

</mapper>

数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Navicat Premium Data Transfer

Source Server : 127.0.0.1
Source Server Type : MySQL
Source Server Version : 50717
Source Host : 127.0.0.1:3306
Source Schema : demo

Target Server Type : MySQL
Target Server Version : 50717
File Encoding : 65001

Date: 05/20/2019 16:27:45
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '学生姓名',
`adress` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '学生地址',
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用户名',
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '密码',
`createtime` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '创建时间',
`updatetime` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '张三', '北京朝阳', 'zhangsan', '123', '2019-05-20 00:00:00', NULL);
INSERT INTO `student` VALUES (2, '李四', '上海徐汇', 'lisi', '123', '2019-05-20 00:00:00', '2019-05-21 10:11:42');
INSERT INTO `student` VALUES (3, '王五', '山东青岛', 'wangwu', '123', '2019-05-20 00:00:00', NULL);
INSERT INTO `student` VALUES (4, '李刚', '浙江杭州', 'ligang', '123', '2019-05-20 00:00:00', NULL);

SET FOREIGN_KEY_CHECKS = 1;