SpringBoot集成Mybatis用法笔记

开发 架构
今天给大家整理SpringBoot集成Mybatis用法笔记。希望对大家能有所帮助!

[[403817]]

今天给大家整理SpringBoot集成Mybatis用法笔记。希望对大家能有所帮助!

1.搭建一个SpringBoot基础项目。

具体可以参考SpringBoot:搭建第一个Web程序

2.引入相关依赖

  1. <dependencies> 
  2. <dependency> 
  3. <groupId>org.springframework.boot</groupId> 
  4. <artifactId>spring-boot-starter</artifactId> 
  5. </dependency> 
  6. <dependency> 
  7. <groupId>org.springframework.boot</groupId> 
  8. <artifactId>spring-boot-starter-web</artifactId> 
  9. </dependency> 
  10. <dependency> 
  11. <groupId>org.mybatis.spring.boot</groupId> 
  12. <artifactId>mybatis-spring-boot-starter</artifactId> 
  13. <version>2.1.3</version> 
  14. </dependency> 
  15. <dependency> 
  16. <groupId>mysql</groupId> 
  17. <artifactId>mysql-connector-java</artifactId> 
  18. </dependency> 
  19. <dependency> 
  20. <groupId>org.springframework.boot</groupId> 
  21. <artifactId>spring-boot-starter-test</artifactId> 
  22. <scope>test</scope> 
  23. </dependency> 
  24. <dependency> 
  25. <groupId>junit</groupId> 
  26. <artifactId>junit</artifactId> 
  27. <version>4.12</version> 
  28. <scope>test</scope> 
  29. </dependency> 
  30. </dependencies> 

 

 

3.准备数据库脚本

创建一个Mysql数据库,数据库名为test,然后执行一下脚本。

  1. /* 
  2. Navicat MySQL Data Transfer 
  3.  
  4. Source Server         : 本地MYSQL 
  5. Source Server Version : 50644 
  6. Source Host           : localhost:3306 
  7. Source Database       : test 
  8.  
  9. Target Server Type    : MYSQL 
  10. Target Server Version : 50644 
  11. File Encoding         : 65001 
  12.  
  13. Date: 2021-05-16 17:20:26 
  14. */ 
  15.  
  16. SET FOREIGN_KEY_CHECKS=0; 
  17.  
  18. -- ---------------------------- 
  19. -- Table structure for t_user 
  20. -- ---------------------------- 
  21. DROP TABLE IF EXISTS `t_user`; 
  22. CREATE TABLE `t_user` ( 
  23.   `id` int(11) NOT NULL AUTO_INCREMENT, 
  24.   `user_name` varchar(255) CHARACTER SET armscii8 DEFAULT NULL
  25.   `passwordvarchar(255) CHARACTER SET armscii8 DEFAULT NULL
  26.   `last_login_time` datetime DEFAULT NULL
  27.   `sex` tinyint(4) DEFAULT NULL
  28.   PRIMARY KEY (`id`) 
  29. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; 
  30.  
  31. -- ---------------------------- 
  32. -- Records of t_user 
  33. -- ---------------------------- 
  34. INSERT INTO `t_user` VALUES ('1''xiaoxin''123''2019-07-27 16:01:21''1'); 
  35. INSERT INTO `t_user` VALUES ('2''jack jo''123''2019-07-24 16:01:37''1'); 
  36. INSERT INTO `t_user` VALUES ('4''landengdeng''123''2019-07-24 16:01:37''1'); 
  37. INSERT INTO `t_user` VALUES ('5''max''123''2019-07-24 16:01:37''1'); 
  38. INSERT INTO `t_user` VALUES ('6''liua11''123456'null'1'); 
  39. INSERT INTO `t_user` VALUES ('7''xiaozhang''888888'null'1'); 

4.配置项目配置文件 application.yml

  1. server: 
  2. port: 8090 
  3. mybatis: 
  4. configuration: 
  5. map-underscore-to-camel-casetrue 
  6.   mapper-locations: mybatis/**/*Mapper.xml 
  7.  
  8. spring: 
  9. datasource: 
  10. driverClassName: com.mysql.cj.jdbc.Driver 
  11. url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8 
  12. username: root 
  13. password: root 
  14. logging: 
  15. level
  16. my.springboot.mybatis.dao: debug 

5.创建实体类 UserDO.java

  1. package my.springboot.mybatis.entity; 
  2.  
  3. import java.util.Date
  4.  
  5. public class UserDO { 
  6. private Integer id; 
  7. private String userName; 
  8. private String password
  9. private Integer sex; 
  10. private Date lastLoginTime; 
  11.  
  12. public Integer getId() { 
  13. return id; 
  14.     } 
  15.  
  16. public void setId(Integer id) { 
  17. this.id = id; 
  18.     } 
  19.  
  20. public String getUserName() { 
  21. return userName; 
  22.     } 
  23.  
  24. public void setUserName(String userName) { 
  25. this.userName = userName; 
  26.     } 
  27.  
  28. public String getPassword() { 
  29. return password
  30.     } 
  31.  
  32. public void setPassword(String password) { 
  33. this.password = password
  34.     } 
  35.  
  36. public Integer getSex() { 
  37. return sex; 
  38.     } 
  39.  
  40. public void setSex(Integer sex) { 
  41. this.sex = sex; 
  42.     } 
  43.  
  44. public Date getLastLoginTime() { 
  45. return lastLoginTime; 
  46.     } 
  47.  
  48. public void setLastLoginTime(Date lastLoginTime) { 
  49. this.lastLoginTime = lastLoginTime; 
  50.     } 
  51.  
  52. @Override 
  53.     public String toString() { 
  54. return "UserDO{" + 
  55. "id=" + id + 
  56. ", userName='" + userName + '\'' + 
  57. ", password='" + password + '\'' + 
  58. ", sex=" + sex + 
  59. ", lastLoginTime=" + lastLoginTime + 
  60. '}'
  61.     } 

6.创建mapper文件 UserInfoMapper.java

  1. package my.springboot.mybatis.dao; 
  2.  
  3. import java.util.List; 
  4. import java.util.Map; 
  5. import my.springboot.mybatis.entity.UserDO; 
  6. import org.apache.ibatis.annotations.Mapper; 
  7.  
  8. @Mapper 
  9. public interface UserInfoMapper { 
  10.     UserDO get(Integer id); 
  11.     List<UserDO> list(Map<String, Object> map); 
  12. int count(Map<String, Object> map); 
  13. int save(UserDO user); 
  14. int update(UserDO user); 
  15. int remove(Integer id); 
  16. int batchRemove(Integer[] ids); 

7.创建Mapper映射文件 UserInfoMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"
  3. <mapper namespace="my.springboot.mybatis.dao.UserInfoMapper"
  4. <select id="get" resultType="my.springboot.mybatis.entity.UserDO"
  5.       select id,user_name,password,last_login_time,sex from t_user where id = #{value} 
  6. </select
  7. <select id="list" resultType="my.springboot.mybatis.entity.UserDO"
  8.         select id,user_name,password,last_login_time,sex from t_user 
  9. <where
  10. <if test="id != null   and id != '-1' " > and id = #{id} </if> 
  11. <if test="userName != null  and userName != '' " > and user_name = #{userName} </if> 
  12. <if test="password != null  and password != '' " > and password = #{password} </if> 
  13. <if test="lastLoginTime != null  and lastLoginTime != '' " > and last_login_time = #{lastLoginTime} </if> 
  14. <if test="sex != null   and sex != '-1' " > and sex = #{sex} </if> 
  15. </where
  16. <choose> 
  17. <when test="sort != null and sort.trim() != ''"
  18.                 order by ${sort} ${order
  19. </when
  20. <otherwise> 
  21.                 order by id desc 
  22. </otherwise> 
  23. </choose> 
  24. <if test="offset != null and limit != null"
  25.             limit #{offset}, #{limit} 
  26. </if> 
  27. </select
  28. <select id="count" resultType="int"
  29.         select count(*) from t_user 
  30. <where
  31. <if test="id != null   and id != '-1'  " > and id = #{id} </if> 
  32. <if test="userName != null  and userName != ''  " > and user_name = #{userName} </if> 
  33. <if test="password != null  and password != ''  " > and password = #{password} </if> 
  34. <if test="lastLoginTime != null  and lastLoginTime != ''  " > and last_login_time = #{lastLoginTime} </if> 
  35. <if test="sex != null   and sex != '-1'  " > and sex = #{sex} </if> 
  36. </where
  37. </select
  38. <insert id="save" parameterType="my.springboot.mybatis.entity.UserDO" useGeneratedKeys="true" keyProperty="id"
  39.       insert into t_user 
  40.       ( 
  41.          user_name, 
  42.          password
  43.          last_login_time, 
  44.          sex 
  45.       values 
  46.       ( 
  47.          #{userName}, 
  48.          #{password}, 
  49.          #{lastLoginTime}, 
  50.          #{sex} 
  51. </insert
  52. <update id="update" parameterType="my.springboot.mybatis.entity.UserDO"
  53.         update t_user 
  54. <set
  55. <if test="userName != null">user_name = #{userName}, </if> 
  56. <if test="password != null">password = #{password}, </if> 
  57. <if test="lastLoginTime != null">last_login_time = #{lastLoginTime}, </if> 
  58. <if test="sex != null">sex = #{sex}</if> 
  59. </set
  60.         where id = #{id} 
  61. </update
  62. <delete id="remove"
  63.       delete from t_user where id = #{value} 
  64. </delete
  65.  
  66. <delete id="batchRemove"
  67.         delete from t_user where id in 
  68. <foreach item="id" collection="array" open="(" separator="," close=")"
  69.             #{id} 
  70. </foreach> 
  71. </delete
  72. </mapper> 

 

 

 

8.创建服务接口 IUserInfoService.java

  1. package my.springboot.mybatis.service; 
  2. import my.springboot.mybatis.entity.UserDO; 
  3.  
  4. import java.util.List; 
  5.  
  6. public interface IUserInfoService { 
  7.     List<UserDO> findAll(); 
  8.  
  9.     UserDO findById(Integer id); 
  10.  
  11. void insert(UserDO model); 
  12.  
  13.     Integer update(UserDO model); 
  14.  
  15.     Integer deleteById(Integer id); 
  16.  

9.创建服务实现类 UserInfoService.java

  1. package my.springboot.mybatis.service.impl; 
  2.  
  3.  
  4. import my.springboot.mybatis.dao.UserInfoMapper; 
  5. import my.springboot.mybatis.entity.UserDO; 
  6. import my.springboot.mybatis.service.IUserInfoService; 
  7. import org.springframework.beans.factory.annotation.Autowired; 
  8. import org.springframework.stereotype.Service; 
  9.  
  10. import java.util.List; 
  11. @Service 
  12. public class UserInfoService implements IUserInfoService { 
  13. @Autowired 
  14.     private UserInfoMapper mapper; 
  15. @Override 
  16.     public List<UserDO> findAll() { 
  17. return mapper.list(null); 
  18.     } 
  19.  
  20. @Override 
  21.     public UserDO findById(Integer id) { 
  22. return mapper.get(id); 
  23.     } 
  24.  
  25. @Override 
  26.     public void insert(UserDO model) { 
  27. mapper.save(model); 
  28.     } 
  29.  
  30. @Override 
  31.     public Integer update(UserDO model) { 
  32. return  mapper.update(model); 
  33.     } 
  34.  
  35. @Override 
  36.     public Integer deleteById(Integer id) { 
  37. return mapper.remove(id); 
  38.     } 

10.创建控制器 HomeController.java

  1. package my.springboot.mybatis.controller; 
  2.  
  3.  
  4. import my.springboot.mybatis.entity.UserDO; 
  5. import my.springboot.mybatis.service.IUserInfoService; 
  6. import org.springframework.beans.factory.annotation.Autowired; 
  7. import org.springframework.stereotype.Controller; 
  8. import org.springframework.web.bind.annotation.RequestMapping; 
  9. import org.springframework.web.bind.annotation.ResponseBody; 
  10.  
  11. import javax.jws.soap.SOAPBinding; 
  12. import java.util.Date
  13.  
  14.  
  15. @Controller 
  16. public class HomeController { 
  17. @Autowired 
  18.     private IUserInfoService userInfoService; 
  19. @RequestMapping("index") //注解映射请求路径 
  20.     @ResponseBody //可以将java对象转为json格式的数据 
  21.     public String index() 
  22.     { 
  23.         UserDO user=userInfoService.findById(1); 
  24. // 新增用户 
  25.         UserDO add=new UserDO(); 
  26.         add.setSex(1); 
  27.         add.setUserName("xiaozhang"); 
  28.         add.setPassword("888888"); 
  29.         add.setLastLoginTime(null); 
  30. //userInfoService.insert(add); 
  31.         // 更新用户 
  32.         user.setUserName("xiaoxin"); 
  33. //userInfoService.update(user); 
  34.         // 删除用户 
  35.         userInfoService.deleteById(3); 
  36.  
  37. return "Hello World !"
  38.     } 

启动地址:http://localhost:8090/index

11.项目结构文件截图

本文转载自微信公众号「IT技术分享社区」,可以通过以下二维码关注。转载本文请联系IT技术分享社区公众号。

博客链接:https://programmerblog.xyz

 

责任编辑:武晓燕 来源: IT技术分享社区
相关推荐

2021-07-11 07:05:28

RedisSpringBoot用法

2021-05-26 06:22:34

SpringBootJPA后端开发

2021-09-26 05:02:00

缓存Ehcache用法

2021-06-28 07:09:24

MybatisresultMapJava

2023-09-08 09:10:33

SpringBoot微服务架构

2023-08-07 14:28:07

SpringBoot工具

2023-01-11 15:11:36

SpringEhcache

2021-05-19 09:53:16

SpringbootMyBatisMySQL

2024-01-16 08:17:29

Mybatis验证业务

2010-08-16 15:11:02

DIV

2021-12-22 22:32:48

鸿蒙HarmonyOS应用

2010-08-26 16:40:35

DIV定位

2021-01-05 05:36:39

设计Spring Boot填充

2010-09-14 16:20:19

DIV定位

2022-07-11 09:00:37

依赖配置文件Mybati

2021-06-07 08:39:58

SpringBootMyBatisMapper

2023-06-07 08:08:37

MybatisSpringBoot

2009-12-02 17:07:27

LINUX系统

2010-07-12 15:16:29

UML关联

2021-04-21 09:04:43

开发SpringBoot框架
点赞
收藏

51CTO技术栈公众号