1.简介
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
2.搭建环境
2.1pom.xml配置
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
2.2spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- dataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis??characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--配置工厂 -->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- sql映射文件路径 -->
<!-- <property name="mapperLocations" value="classpath:com/qianfeng/openapi/web/master/mapper/*.xml"></property>-->
<property name="configLocation" value="classpath:mybatis/mybatis.xml"/>
</bean>
<!-- 配置扫描mapper文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>
</beans>
2.3mybatis配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<plugins>
<!--配置分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
实体配置
//@TableName 注解,帮助mybatis与数据库表关联
@TableName("t_departments")
public class Department {
@TableId(value = "id",type = IdType.AUTO)//指定自增策略
private Integer id;
private String name;
private String location;
}
3.案例
3.1插入
/**
* 测试,mybatisplus的插入功能
*/
@Test
public void testInsert() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
//调用mybatisplus的插入方法
departmentMapper.insert(new Department(null, "人事部", "杭州"));
}
3.2修改
/**
* 根据 ID 修改
*/
@Test
public void testupdateById() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
Department d = new Department();
d.setId(15);
d.setName("abc");
d.setLocation("abc");
departmentMapper.updateById(d);
}
/**
* 根据 whereEntity 条件,更新记录
*
* 实体对象 (set 条件值,可以为 null)
* 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
@Test
public void testupdate() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
Department d = new Department();
d.setId(16);
d.setName("123");
d.setLocation("123");
departmentMapper.update(d,new UpdateWrapper<Department>()
.eq("name","abc")
.eq("location","abc"));
}
3.3删除
@Test
public void testDeleteByMap(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
Map map = new HashMap<>();
map.put("id",11);
map.put("name","人事部");
departmentMapper.deleteByMap(map);
}
/**
* 根据 entity 条件,删除记录
*
* 实体对象封装操作类(可以为 null)
*/
@Test
public void testDelete() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
departmentMapper.delete(new QueryWrapper<Department>().eq("id",12));
}
/**
* 删除多条数据
* 传入参数为collection
*/
@Test
public void testdeleteBatchIds() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
departmentMapper.deleteBatchIds(Arrays.asList(10,13,14));
}
3.4查询
/**
* 根据 ID 查询
*
* id 主键ID
*/
@Test
public void testselectById(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
Department department = departmentMapper.selectById(1);
System.out.println(department);
}
/**
* 查询(根据ID 批量查询)
*
* 主键ID列表(不能为 null 以及 empty)
*/
@Test
public void testselectBatchIds(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
List<Department> list = departmentMapper.selectBatchIds(Arrays.asList(1,2,15));
for (Department department : list) {
System.out.println(department);
}
}
/**
* 查询(根据 columnMap 条件)
*
* 表字段 map 对象
*/
@Test
public void testselectByMap(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
Map map = new HashMap();
map.put("name","研发部");
map.put("location","上海");
List<Department> list = departmentMapper.selectByMap(map);
for (Department department : list) {
System.out.println(department);
}
}
/**
* 根据 entity 条件,查询一条记录
* 实体对象封装操作类(可以为 null)
*/
@Test
public void testselectOne(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
Department department = departmentMapper.selectOne(new QueryWrapper<Department>().gt("id",10));
System.out.println(department);
}
/**
* 根据 Wrapper 条件,查询总记录数
* 实体对象封装操作类(可以为 null)
*/
@Test
public void testselectCount(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
int count = departmentMapper.selectCount(null);
System.out.println(count);
}
/**
* 根据 entity 条件,查询全部记录
*
* 实体对象封装操作类(可以为 null)
*/
@Test
public void testselectList(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
List<Department> list = departmentMapper.selectList(null);
for (Department department : list) {
System.out.println(department);
}
}
/**
* 根据 Wrapper 条件,查询全部记录
*
* 实体对象封装操作类(可以为 null)
*/
@Test
public void testselectMaps(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
List<Map<String,Object>> list = departmentMapper.selectMaps(new QueryWrapper<Department>().ne("id",100));
for (Map<String, Object> stringObjectMap : list) {
System.out.println(stringObjectMap);
}
}
/**
* 根据 Wrapper 条件,查询全部记录
* <p>注意: 只返回第一个字段的值</p>
*
* 实体对象封装操作类(可以为 null)
*/
@Test
public void testselectObjs(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
List<Object> list = departmentMapper.selectObjs(null);
System.out.println(list);
}
/**
* 分页查询
*/
@Test
public void testselectpage(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
PageHelper.startPage(1,2);
List<Department> list = departmentMapper.selectList(null);
System.out.println(list);
}