SpringBoot整合Mybatis框架
前期准备——数据库
表
SQL脚本
1 -- ---------------------------- 2 -- Table structure for user 3 -- ---------------------------- 4 DROP TABLE IF EXISTS `user`; 5 CREATE TABLE `user` ( 6 `id` int(11) NOT NULL AUTO_INCREMENT, 7 `username` varchar(255) DEFAULT NULL, 8 `password` varchar(255) DEFAULT NULL, 9 PRIMARY KEY (`id`) 10 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
第一步:导包,直接去Maven搜mybatis,可以看到有一个mybatis-spring-boot-starter,点进去将其依赖复制下来,然后粘贴到我们SpringBoot项目的pom.xml文件的中
1 <!-- mybatis-spring-boot-starter --> 2 <dependency> 3 <groupId>org.mybatis.spring.boot</groupId> 4 <artifactId>mybatis-spring-boot-starter</artifactId> 5 <version>2.1.4</version> 6 </dependency>
项目目录结构
第二步:在springboot的核心配置文件中对Mybatis进行相关配置
这里有对别名和Mybatis中的所有mapper文件的路径进行配置,如有需要,自己可另行配置
1 #配置数据源 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 3 spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 4 spring.datasource.username=root 5 spring.datasource.password=123 6 7 # 整合mybatis 8 # 别名 9 mybatis.type-aliases-package=com.lzp.pojo 10 # 绑定mapper文件 11 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
第三步:编写实体类、接口以及对应的xxxMapper.xml文件,这里我用User业务来举例(同下)
User类
1 package com.lzp.pojo; 2 3 import org.springframework.context.annotation.Bean; 4 import java.io.Serializable; 5 6 /** 7 * @Author LZP 8 * @Date 2021/4/24 15:17 9 * @Version 1.0 10 */ 11 public class User implements Serializable { 12 private int id; 13 private String userName; 14 private String password; 15 16 public User() { 17 } 18 19 public User(String userName, String password) { 20 this.userName = userName; 21 this.password = password; 22 } 23 24 public User(int id, String userName, String password) { 25 this.id = id; 26 this.userName = userName; 27 this.password = password; 28 } 29 30 public int getId() { 31 return id; 32 } 33 34 public void setId(int id) { 35 this.id = id; 36 } 37 38 public String getUserName() { 39 return userName; 40 } 41 42 public void setUserName(String userName) { 43 this.userName = userName; 44 } 45 46 public String getPassword() { 47 return password; 48 } 49 50 public void setPassword(String password) { 51 this.password = password; 52 } 53 54 55 @Override 56 public String toString() { 57 return "User{" + 58 "id=" + id + 59 ", userName='" + userName + '\'' + 60 ", password='" + password + '\'' + 61 '}'; 62 } 63 }
UserMapper.java
1 package com.lzp.mapper; 2 3 import com.lzp.pojo.User; 4 import org.apache.ibatis.annotations.Mapper; 5 import org.springframework.stereotype.Repository; 6 import java.util.List; 7 8 /** 9 * @Author LZP 10 * @Date 2021/4/24 15:17 11 * @Version 1.0 12 */ 13 // @Mapper这个注解表示该类是一个mybatis的mapper类,Dao 14 @Mapper 15 // @Repository这个注解加上,表示该类会被Spring的IOC容器进行托管 16 @Repository 17 public interface UserMapper { 18 19 int addUser(User user); 20 21 int deleteUser(int id); 22 23 int updateUser(User user); 24 25 User queryUserById(int id); 26 27 List<User> queryUserList(); 28 }
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.lzp.mapper.UserMapper"> 6 7 <!-- 这取User中的属性时,可以直接使用实体类的属性名,不需要通过实体类的类名去点出来 --> 8 <insert id="addUser" parameterType="User"> 9 insert into user (username, password) 10 values (#{userName}, #{password}) 11 </insert> 12 13 <delete id="deleteUser" parameterType="_int"> 14 delete 15 from user 16 where id = #{id} 17 </delete> 18 19 <update id="updateUser" parameterType="User"> 20 update user 21 set 22 username = #{userName}, 23 password = #{password} 24 where id = #{id} 25 </update> 26 27 <select id="queryUserById" parameterType="_int" resultType="User"> 28 select * 29 from user 30 where id = #{id}; 31 </select> 32 33 <select id="queryUserList" resultType="User"> 34 select * 35 from user 36 </select> 37 </mapper>
第四步:编写UserController.java(Controller层)去测试增删改查,这里采用RestFull风格
1 package com.lzp.controller; 2 3 import com.lzp.mapper.UserMapper; 4 import com.lzp.pojo.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import java.util.List; 10 11 /** 12 * @Author LZP 13 * @Date 2021/4/24 15:32 14 * @Version 1.0 15 * 16 * 用户控制层 17 * 基本功能: 18 * 实现用户的增删改查 19 * 请求URL采用了RestFull风格 20 */ 21 @RestController 22 @RequestMapping("/user") 23 public class UserController { 24 25 @Autowired 26 private UserMapper userMapper; 27 28 @RequestMapping("/queryUserList") 29 public List<User> queryUserList() { 30 List<User> users = userMapper.queryUserList(); 31 for (User user : users) { 32 System.out.println(user); 33 } 34 return users; 35 } 36 37 @RequestMapping("/queryUserById/{id}") 38 public User queryUserById(@PathVariable("id") int id) { 39 User user = userMapper.queryUserById(id); 40 System.out.println(user); 41 return user; 42 } 43 44 /** 45 * 如需使用RestFull风格,就必须添加@PathVariable这个注解,以保证数据能够正确绑定获取到 46 * @param username 47 * @param password 48 * @return 49 */ 50 @RequestMapping("/addUser/{username}/{password}") 51 public String addUser(@PathVariable("username") String username, @PathVariable("password") String password) { 52 User user = new User(username, password); 53 int result = userMapper.addUser(user); 54 if (result > 0) { 55 return "成功添加用户"; 56 } 57 return "添加用户失败"; 58 } 59 60 @RequestMapping("/deleteUser/{id}") 61 public String deleteUser(@PathVariable("id") int id) { 62 int result = userMapper.deleteUser(id); 63 if (result > 0) { 64 return "成功删除用户"; 65 } 66 return "删除用户失败"; 67 } 68 69 @RequestMapping("/updateUser/{id}/{username}/{password}") 70 public String updateUser(@PathVariable("id") int id, @PathVariable("username") String username, 71 @PathVariable("password") String password) { 72 User user = new User(id, username, password); 73 int result = userMapper.updateUser(user); 74 if (result > 0) { 75 return "成功修改用户"; 76 } 77 return "修改用户失败"; 78 } 79 }