1. 使用maven构建SpringBoot的名叫spring-boot-mybatis项目

2. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-mybatis</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html标签不规范报错 --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- Mybatis启动器 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.2</version></dependency></dependencies>
</project>

3. 在src/main/resources下创建全局配置文件application.properties

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.25.138:3306/StudyMybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=lyw123456spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource

4. 数据库表设计

5. 新建User.java

package com.bjbs.pojo;import java.io.Serializable;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private String sex;@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")private Date birthday;private String address;public User() {}public User(String name, String sex, Date birthday, String address) {this.name = name;this.sex = sex;this.birthday = birthday;this.address = address;}public User(Integer id, String name, String sex, Date birthday, String address) {this.id = id;this.name = name;this.sex = sex;this.birthday = birthday;this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address+ "]";}}

6. 新建UserMapper.java

package com.bjbs.mapper;import java.util.List;
import com.bjbs.pojo.User;public interface UserMapper {public List<User> findAllUser();void saveUser(User user);User findUserById(Integer id);void updateUser(User user);void deleteUserById(Integer id);
}

7. 新建UserMapper.xml

<?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.bjbs.mapper.UserMapper"><select id="findAllUser" resultType="com.bjbs.pojo.User">select * from user</select><insert id="saveUser" parameterType="com.bjbs.pojo.User">insert into user values(null, #{name}, #{sex}, #{birthday}, #{address})</insert><select id="findUserById" resultType="com.bjbs.pojo.User">select * from user where id = #{value}</select><update id="updateUser" parameterType="com.bjbs.pojo.User">update user set name=#{name}, sex = #{sex}, birthday = #{birthday}, address = #{address} where id=#{id}</update><delete id="deleteUserById">delete from user where id = #{value}</delete></mapper>

8. 新建UserService.java

package com.bjbs.service;import java.util.List;
import com.bjbs.pojo.User;public interface UserService {List<User> findAllUser();void saveUser(User user);User findUserById(Integer id);void updateUser(User user);void deleteUserById(Integer id);
}

9. 新建UserServiceImpl.java

package com.bjbs.service.impl;import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bjbs.mapper.UserMapper;
import com.bjbs.pojo.User;
import com.bjbs.service.UserService;@Service
@Transactional
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> findAllUser() {return userMapper.findAllUser();}@Overridepublic void saveUser(User user) {userMapper.saveUser(user);}@Overridepublic User findUserById(Integer id) {return userMapper.findUserById(id);}@Overridepublic void updateUser(User user) {userMapper.updateUser(user);}@Overridepublic void deleteUserById(Integer id) {userMapper.deleteUserById(id);}}

10. 新建App.java

package com.bjbs;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot启动类*/
@SpringBootApplication
@MapperScan("com.bjbs.mapper") //@MapperScan 用户扫描MyBatis的Mapper接口
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}

11. 在src/main/resources/templates下, 新建allUser.html

<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>展示用户数据</title><style type="text/css">th {width: 100px;}</style></head><body><table border="1"><tr><th>用户ID</th><th>用户姓名</th><th>用户性别</th><th style="width: 300px;">用户生日</th><th>用户地址</th><th style="width: 300px;">操作</th></tr><tr th:each="user : ${list}"><td th:text="${user.id}"></td><td th:text="${user.name}"></td><td th:text="${user.sex}"></td><td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd HH:mm:ss')}"></td><td th:text="${user.address}"></td><td><a th:href="@{/findUserById(id=${user.id})}">更新用户</a><a th:href="@{/delUser(id=${user.id})}">删除用户</a><a th:href="@{/addUser}">添加用户</a></td></tr></table></body>
</html>

12. 在src/main/resources/templates下, 新建addUser.html

<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>添加用户</title></head><body><form th:action="@{/saveUser}" method="post">用户姓名: <input type="text" name="name"/><br/>用户性别: <input type="text" name="sex"/><br/>用户生日: <input type="text" name="birthday"/><br/>用户地址: <input type="text" name="address"/><br/><input type="submit" value="确定"/><br/></form></body>
</html>

13. 在src/main/resources/templates下, 新建editUser.html

<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>更新用户信息</title></head><body><form th:action="@{/updateUser}" method="post"><input type="hidden" name="id" th:field="${user.id}"/>用户姓名: <input type="text" name="name" th:field="${user.name}"/><br/>用户性别: <input type="text" name="sex" th:field="${user.sex}"/><br/>用户生日: <input type="text" name="birthday" th:field="${user.birthday}"/><br/>用户地址: <input type="text" name="address" th:field="${user.address}"/><br/><input type="submit" value="确定"/><br/></form></body>
</html>

14. 运行项目并使用浏览器访问

15. 点击添加用户链接, 跳转到添加用户页面

16. 添加一个ID为41的用户

17. 点击ID为40的更新用户链接, 跳转到更新用户页面

18. 修改用户信息

19. 点击确定按钮, 保存用户信息

20. 点击ID为39的删除用户链接, 删除用户成功

016_SpringBoot整合MyBatis相关推荐

  1. SpringBoot整合MyBatis详细教程~

    目录 1. 导入依赖 2. 连接数据库 3. 编写数据库配置信息 4. 编写pojo实体类 5. 编写mapper接口 6. 编写mapper.xml 7. 编写controller 8. 测试 1. ...

  2. MyBatis - 6.Spring整合MyBatis

    1.查看不同MyBatis版本整合Spring时使用的适配包: http://www.mybatis.org/spring/ 2.下载整合适配包 https://github.com/mybatis/ ...

  3. mybatis 一对多_Springboot整合Mybatis实现级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  4. Spring Boot 教程(三): Spring Boot 整合Mybatis

    教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...

  5. SpringBoot第六篇:springboot整合mybatis

    本文主要讲解如何在springboot下整合mybatis,并访问数据库.由于mybatis这个框架太过于流行,所以我就不讲解了. 引入依赖 在pom文件引入mybatis-spring-boot-s ...

  6. Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 公司需要人.产品.业务和方向,方向又要人.产品.业务和方向,方向- 循环』 本文提纲 一 ...

  7. spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试

    引子:spring整合mybatis.因为,我们看完(我就是这样的)spring和mybatis之后,本想自己写一个小小的项目,以便加深理解,但是我发现在spring中整合mybatis并不是一件容易 ...

  8. Play Framework 2.5 整合 MyBatis

    为什么80%的码农都做不了架构师?>>>    因为不想用 Play 官方支持的一些 ORM 框架,笔者开始在网上查询 Play 怎么整合 MyBatis ,但搜出来的结果往往都是 ...

  9. spring boot 整合mybatis 无法输出sql的问题

    使用spring boot整合mybatis,测试功能的时候,遇到到了sql问题,想要从日志上看哪里错了,但是怎么都无法输出执行的sql,我使用的是log4j2,百度了一下,很多博客都说,加上下面的日 ...

最新文章

  1. stm32内部的CAN总线
  2. jdbc连接操作mysql,直接操作和预处理方式
  3. 每日一皮:每天到公司 VS 工作一小时后
  4. 【数理知识】《数值分析》李庆扬老师-第2章-插值法
  5. python测试代码怎么写_如何使用python做单元测试?
  6. UVA11082 行列模型
  7. 如何在chrome加载.ctx文件(亲测可用,只需3步)
  8. php程序 导出表格文件后缀,PHPExcel生成Excel文件---提示导出文件或者文件扩展名不一致,或导出的文件或文件扩展名无效...
  9. 位置式与增量式PID代码(C语言实现)
  10. 【个人随笔】留下第一个脚印
  11. 使用CS发送钓鱼邮件
  12. python操作excel编号自增加1
  13. axios 的响应结构
  14. 远程匿名聊天_爱还是恨聊天? 远程团队的4个最佳实践
  15. python 股票竞价数据_GitHub - TruthHun/auction-stock: 集合竞价选股(股票),基于收盘价与前收盘价的选股策略...
  16. 我工作时戴耳机是为了治疗电脑
  17. RabbitMQ从入门到实践
  18. 鸡年的年度编程语言,非它莫属
  19. 如何把自己打造成为一名金领架构师-开悟篇
  20. 【JMeter】阶梯式压测

热门文章

  1. 《Swift开发实战》——第16章,第16.2节下标脚本用法
  2. EBS通过SQL查找所有的定时请求
  3. Codeforces 486D D. Valid Sets
  4. SQL Server表分区的NULL值问题
  5. 如何安装和使用RAutomation
  6. Flask项目支持https
  7. 写在树莓派专栏的开篇
  8. 一条SQL语句实现二进制到十进制的转换
  9. WPF入门教程系列十九——ListView示例(一)
  10. 无线通信 -- 跳频技术