添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
入门示例
代码结构
MPConfig配置
package com.yyoo.boot.mp;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.yyoo.boot.config.MybatisBaseConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import javax.sql.DataSource;
@Configuration
@Slf4j
@Data
// mapper* 表示mapper包及其子包
@MapperScan(value = {
"com.yyoo.boot.mp.mapper*"
})
@ConfigurationProperties("my.mp")
public class MPConfig extends MybatisBaseConfig{
/**
* Mybatis别名文件夹
*/
private String aliasesPackage;
/**
* 映射的 mapper文件
*/
private String[] mapperLocations;
@Bean
@ConfigurationProperties("my.hikari")
public HikariConfig getHikariConfig(){
return new HikariConfig();
}
@Bean
public HikariDataSource hikariDataSource(HikariConfig hikariConfig){
return new HikariDataSource(hikariConfig);
}
@Bean(name = "sessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("hikariDataSource") DataSource ds) throws Exception {
log.info("数据源:{},aliasesPackage:{}",ds,this.aliasesPackage);
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(ds);
if(this.aliasesPackage != null && !"".equals(this.aliasesPackage)) {
factoryBean.setTypeAliasesPackage(this.aliasesPackage);
}
//指定mapper xml目录
Assert.notEmpty(this.mapperLocations,"扫描的Mapper xml不能为空");
factoryBean.setMapperLocations(resolveMapperLocations(this.mapperLocations));
return factoryBean.getObject();
}
@Bean("sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory factory) {
// 使用上面配置的Factory
// 要设置Template为BATCH方式,请使用new SqlSessionTemplate(factory,ExecutorType.BATCH);
SqlSessionTemplate template = new SqlSessionTemplate(factory);
return template;
}
}
其实,除了把之前Mybaits配置的SqlSessionFactoryBean替换成了MybatisSqlSessionFactoryBean 而已,其余都没有变动。
MyEmp
package com.yyoo.boot.mp.beans;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("t_my_emp")
public class MyEmp {
private long id;
private String name;
private int age;
private int sex;
}
@TableName注解,指定了该Bean对应的表的表名为t_my_emp。相关注解还有很多,请查看官网注解说明。
MyEmpMapper
package com.yyoo.boot.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yyoo.boot.mp.beans.MyEmp;
public interface MyEmpMapper extends BaseMapper<MyEmp> {
}
我们的Mapper继承了BaseMapper,BaseMapper集成了我们所泛型的Bean对应的表的增删改查操作,意味着我们无需再编写mapper.xml文件来实现单表的增删改查。
MyEmpService
package com.yyoo.boot.mp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yyoo.boot.mp.beans.MyEmp;
public interface MyEmpService extends IService<MyEmp> {
}
package com.yyoo.boot.mp.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyoo.boot.mp.beans.MyEmp;
import com.yyoo.boot.mp.mapper.MyEmpMapper;
import org.springframework.stereotype.Service;
@Service
public class MyEmpServiceImpl extends ServiceImpl<MyEmpMapper, MyEmp> implements MyEmpService{
}
IService是MP提供的基础CURD实现,与BaseMapper不同的是,其实现了批量插入操作,而且也加了Spring的事务注解进行事务控制。ServiceImpl是其实现类。ServiceImpl的第一个泛型是其对应的Mapper接口对象。
测试代码
package com.yyoo.boot.mp;
import com.yyoo.boot.mp.beans.MyEmp;
import com.yyoo.boot.mp.service.MyEmpService;
import com.yyoo.boot.mybatis.AutoNameUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MPApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test1 {
@Resource
private MyEmpService myEmpService;
@Test
public void t1(){
int num = 1000000;
List<MyEmp> list = new ArrayList<>();
for (int i = 0;i < num;i++) {
Random random = new Random();
MyEmp myEmp = new MyEmp();
myEmp.setName(AutoNameUtil.autoSurAndName());
myEmp.setAge(random.nextInt(50) + 15);// 15岁及以上
myEmp.setSex(random.nextInt(2));
list.add(myEmp);
}
long start = System.currentTimeMillis();
myEmpService.saveBatch(list);
long end = System.currentTimeMillis();
System.out.println("执行时间:"+(end -start));
}
}
测试代码我们使用了前文的批量插入。我们直接使用的saveBatch方法。该方法默认插入1000条数据,当然IService也有重载的可输入插入条数的saveBatch方法。其默认是使用的BATH方式执行,如果你需要更高的执行效率,可以考虑自己实现foreach的方式来执行批量插入。
IService和BaseMapper还有很多常见的方法,请自行测试,有了它们我们已经减少了至少90%以上的单表操作代码与相关配置。这就是使用Mybatis-plus的好处。