MyBatis-Plus实现数据库CRUD
- MyBatis-Plus是一个MyBatis的增强工具
- 自动实现Mapper CRUD操作,极致提高数据库开发效率
- MyBatis-Plus在MyBatis的基础上制作增强不做改变
- 官方文档
一、整合步骤
1. 引入mybatis-plus依赖
pom.xml
<dependencies>
<!-- 1.引入mybatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
2. Spring XML更改配置SqlSessionFactory实现类
applicationContext.xml
<beans ... >
<!--
原生Mybatis与Spring整合
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-->
<!-- 2.更改配置SqlSessionFactory实现类 -->
<bean id="sessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/databasename?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="20"/>
</bean>
</beans>
3. mybatis-config.xml增加MyBatis-plus分页插件
mybatis-config.xml
<configuration>
<plugins>
<!-- 3.配置Mybatis-plus分页插件 -->
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></plugin>
</plugins>
</configuration>
二、开发步骤
1. 创建实体类,使用@TableName、@TableId、@TableField注解实现与表字段的映射
核心注解
- @TableName 将实体类与表名映射
-
@TableId 说明对应属性是表的主键
-
@TableField 设置属性与列名的对应关系
..entity.Test
@TableName("test") // 说明实体对应哪一张表
public class Test {
@TableId(type = IdType.AUTO) // 主键
@TableField("id") // 说明属性对应哪个字段
private Integer id;
// 如果字段名与属性名相同或符合驼峰命名转换规则,则可以省略TableField
// @TableField("content")
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
2. 创建Mapper接口继承BaseMapper,创建Mapper XML
..mapper.TestMapper
public interface TestMapper extends BaseMapper<Test> {
public void insertSample();//自定义insert方法
}
..mappers/test.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="..mapper.TestMapper">
<!-- 自定义insert方法SQL语句 -->
<insert id="insertSample">
insert into test(content) values ('测试内容')
</insert>
</mapper>
3. 开发时注入Mapper对象,通过内置API实现CRUD操作
核心API
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyBatisPlusTest {
@Resource
private TestMapper testMapper;
@org.junit.Test
public void testInsert (){
Test test = new Test();
test.setContent("MyBatis Plus测试内容");
testMapper.insert(test);
}
@org.junit.Test
public void testUpdate (){
Test test = testMapper.selectById(16);
test.setContent("新的MyBatis Plus测试内容");
testMapper.updateById(test);
}
@org.junit.Test
public void testDelete (){
testMapper.deleteById(16);
}
@org.junit.Test
public void testSelect (){
QueryWrapper<Test> queryWrapper = new QueryWrapper<Test>();
//QueryWrapper条件生成器:.eq()等于 .gt()大于
queryWrapper.eq("id", 20);
//queryWrapper.gt("id", 20);
List<Test> list = testMapper.selectList(queryWrapper);
System.out.println(list.get(0));
}
}
发表回复