SpringBoot(4):整合Mybatis
1. 导入mybatis所需要的依赖
1 <!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot的--> 2 <dependency> 3 <groupId>org.mybatis.spring.boot</groupId> 4 <artifactId>mybatis-spring-boot-starter</artifactId> 5 <version>2.1.0</version> 6 </dependency>
2. 配置数据库连接信息
连接自己的数据库,并配置数据库的连接信息
1 spring.datasource.username=root 2 spring.datasource.password=123456 3 spring.datasource.url=jdbc:mysql://localhost:3306/Users?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
我们使用默认的数据源;先去测试一下连接是否成!
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 public class SpringbootDataApplicationTests { 4 5 //DI注入数据源 6 @Autowired 7 DataSource dataSource; 8 9 @Test 10 public void contextLoads() throws SQLException { 11 12 System.out.println("默认数据源:" + dataSource.getClass()); 13 //获得连接 14 Connection connection = dataSource.getConnection(); 15 System.out.println("数据库连接:"+connection); 16 System.out.println("数据库连接地址:" + connection.getMetaData().getURL()); 17 connection.close(); 18 //关闭连接 19 connection.close(); 20 } 21 }
3. 创建实体类
1 public class User { 2 3 private int id; 4 private String name; 5 private String pwd; 6 7 public User() { 8 } 9 10 public User(int id, String name, String pwd) { 11 this.id = id; 12 this.name = name; 13 this.pwd = pwd; 14 } 15 16 public int getId() { 17 return id; 18 } 19 20 public void setId(int id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public String getPwd() { 33 return pwd; 34 } 35 36 public void setPwd(String pwd) { 37 this.pwd = pwd; 38 } 39 40 @Override 41 public String toString() { 42 return "User{" + 43 "id=" + id + 44 ", name='" + name + '\'' + 45 ", pwd='" + pwd + '\'' + 46 '}'; 47 } 48 }
4. 配置Mapper接口类
1 import com.qsh.springbootdata.pojo.User; 2 import org.apache.ibatis.annotations.Mapper; 3 import org.springframework.stereotype.Repository; 4 import java.util.List; 5 6 @Mapper //表示这是一个Mapper接口 7 @Repository //spring组件 8 public interface UserDao { 9 10 //1.选择全部用户 11 List<User> selectUser(); 12 //2.根据id选择用户 13 User selectUserById(int id); 14 //3.添加一个用户 15 int addUser(User user); 16 //4.修改一个用户 17 int updateUser(User user); 18 //5.根据id删除用户 19 int deleteUser(int id); 20 }
5. Mapper映射文件
在resources下创建Mapper映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.qsh.springbootdata.dao.UserDao"> 7 8 <!--1.选择全部用户--> 9 <select id="selectUser" resultType="User"> 10 select * from user 11 </select> 12 13 <!--2.根据id选择用户--> 14 <select id="selectUserById" resultType="User"> 15 select * from user where id = #{id} 16 </select> 17 18 <!--3.添加一个用户--> 19 <insert id="addUser" parameterType="User"> 20 insert into user (id,name,pwd) values (#{id},#{name},#{pwd}) 21 </insert> 22 23 <!--4.修改一个用户--> 24 <update id="updateUser" parameterType="User"> 25 update user set name=#{name},pwd=#{pwd} where id = #{id} 26 </update> 27 28 <!--5.根据id删除用户--> 29 <delete id="deleteUser" parameterType="int"> 30 delete from user where id = #{id} 31 </delete> 32 33 </mapper>
6. SpringBoot 整合
告诉springboot myBatis的映射配置文件的位置
1 #连接mybatis 指定Mapper映射文件 2 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml 3 4 #别名 注意:对应实体类的路径 5 mybatis.type-aliases-package=com.qsh.springbootdata.pojo
7. 编写controller
1 import com.qsh.springbootdata.dao.UserDao; 2 import com.qsh.springbootdata.pojo.User; 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 import java.util.List; 7 8 @RestController 9 public class UserController { 10 11 @Autowired 12 UserDao userDao; 13 14 //查询全部用户 15 @GetMapping("/selectUser") 16 public String selectUser() { 17 List<User> users = userDao.selectUser(); 18 for (User user : users) { 19 System.out.println(user); 20 } 21 return "ok"; 22 } 23 24 //根据id选择用户 25 @GetMapping("/selectUserById") 26 public String selectUserById() { 27 User user = userDao.selectUserById(1); 28 System.out.println(user); 29 return "ok"; 30 } 31 32 //添加一个用户 33 @GetMapping("/addUser") 34 public String addUser() { 35 userDao.addUser(new User(4, "赵四1", "456789")); 36 return "ok"; 37 } 38 39 //修改一个用户 40 @GetMapping("/updateUser") 41 public String updateUser() { 42 userDao.updateUser(new User(4, "赵四2", "421319")); 43 return "ok"; 44 } 45 46 //根据id删除用户 47 @GetMapping("/deleteUser") 48 public String deleteUser() { 49 userDao.deleteUser(4); 50 return "ok"; 51 } 52 53 }
运行测试即可!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了