我是 ABin-阿斌:写一生代码,创一世佳话,筑一揽芳华。 如果小伙伴们觉得我的文章有点 feel ,那就点个赞再走哦。
SpringBoot其它两大整合:
更多整合可关注首页
SpringBoot
分栏:SpringBoot大整合
一、 前言
当大家进入到这篇文章的时候我相信已经对 MyBatis 以及如何使用 MyBatis 有一个全面的了解了,那么这里我就不在阐述如何使用 MyBatis,直接进入整合主题。
二、相关介绍
- SpringBoot 整合MyBatis的官方文档地址: http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
- 相关依赖Maven仓库地址: https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.1
三、整合测试
3.1、建立一个新的模块,导入我们所需的一个 MyBatis
依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
3.2、配置数据库连接信息
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3.3、编写测试用例,查看数据库是否连接成功
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws SQLException {
//查看默认数据源:com.zaxxer.hikari.HikariDataSource(目前速度最快的)
System.out.println(dataSource.getClass());
//获取数据库连接
Connection connection = dataSource.getConnection();
System.out.println("数据库连接信息:" + connection);
//关闭连接
connection.close();
}
}
3.4、创建实体类,导入 Lombok
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
/**
* 部门编号
*/
private Integer id;
/**
* 部门名称
*/
private String dname;
/**
* 部门地址
*/
private String loc;
}
3.4、创建 mapper
目录以及对应的 Mapper
接口
/**
* @Mapper:该注解表示本类是一个 Mybatis 的 Mapper类
*/
@Repository
@Mapper
public interface DeptMapper {
/**
* 获取所有部门信息
*/
List<Dept> getDeptList();
/**
* 添加部门信息
* @Param: [dept]
* @return: int
*/
int saveDept(Dept dept);
/**
* 通过ID查询部门信息
* @Param: [id]
* @return: com.abin.pojo.Dept
*/
Dept queryDeptById(Integer id);
/**
* 通过ID删除部门信息
* @Param: [id]
* @return: int
*/
int deleteDept(Integer id);
/**
* 修改部门信息
* @Param: [dept]
* @return: int
*/
int updateDept(Dept dept);
}
3.5、对应的 Mapper
映射文件
<?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.abin.mapper.DeptMapper">
<select id="getDeptList" resultType="Dept">
select * from Dept
</select>
<insert id="saveDept" parameterType="Dept">
insert into dept (id,dname,loc) values(#{id},#{dname},#{loc});
</insert>
<select id="queryDeptById" resultType="Dept">
select * from Dept where id = #{id}
</select>
<delete id="deleteDept" parameterType="Dept">
delete from dept where id = #{id}
</delete>
<update id="updateDept" parameterType="Dept">
update dept set dname = #{dname},loc = #{loc} where id = #{id}
</update>
</mapper>
3.6、maven
配置资源过滤问题
<!--在 pom.xml的<build> 标签中加上即可-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
3.7、编写部门的 DaptController
进行测试
@RestController
public class DeptController {
@Autowired
private DeptMapper deptMapper;
@GetMapping("/getDeptList")
public List<Dept> getDeptList() {
List<Dept> deptList = deptMapper.getDeptList();
for (Dept dept : deptList) {
System.out.println(dept);
}
return deptList;
}
/**
* 添加部门
* @return: java.lang.String
*/
@GetMapping("/save")
public String saveDept(){
deptMapper.saveDept(new Dept(60,"技术部","上海"));
return "添加部门成功!";
}
/**
* 根据 id 删除部门
* @Param: [id]
* @return: java.lang.String
*/
@GetMapping("/clear")
public String deleteDept(){
deptMapper.deleteDept(60);
return "删除部门成功!";
}
/**
* 通过ID查询部门信息
* @Param: [id]
* @return: java.lang.String
*/
@GetMapping("/query/{id}")
public Dept queryDeptById(@PathVariable("id") Integer id){
Dept dept = deptMapper.queryDeptById(id);
return dept;
}
/**
* 修改部门信息
* @Param: []
* @return: java.lang.String
*/
@GetMapping("/updateDept")
public String updateDept(){
deptMapper.updateDept(new Dept(60,"运营部","杭州"));
return "修改部门表数据成功!";
}
}
总结
最后直接拿着测试类的 path 去测试就可以,如果在测试的过程中有问题可以在评论下方回复。