软件编程
位置:首页>> 软件编程>> java编程>> 详解Spring boot上配置与使用mybatis plus

详解Spring boot上配置与使用mybatis plus

作者:junzibuqi124  发布时间:2023-02-27 08:53:11 

标签:Spring,boot,mybatis,plus

http://mp.baomidou.com/#/?id=%e7%ae%80%e4%bb%8b 这个是mybatisplus的官方文档,上面是mybtisplus的配置使用方法,以及一些功能的介绍

下面开始配置

maven依赖


   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>1.1.1</version>
   </dependency>
   <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus</artifactId>
     <version>2.0-beta</version>
   </dependency>

config文件


@Configuration
public class MybatisPlusConfig {
 @Autowired
 private DataSource dataSource;

@Autowired
 private MybatisProperties properties;

@Autowired
 private ResourceLoader resourceLoader = new DefaultResourceLoader();

@Autowired(required = false)
 private Interceptor[] interceptors;

@Autowired(required = false)
 private DatabaseIdProvider databaseIdProvider;

/**
  *  mybatis-plus分页插件
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
   PaginationInterceptor page = new PaginationInterceptor();
   page.setDialectType("mysql");
   return page;
 }
 /**
  * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
  * 配置文件和mybatis-boot的配置文件同步
  * @return
  */
 @Bean
 public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
   MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
   mybatisPlus.setDataSource(dataSource);
   mybatisPlus.setVfs(SpringBootVFS.class);
   if (StringUtils.hasText(this.properties.getConfigLocation())) {
     mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
   }
   mybatisPlus.setConfiguration(properties.getConfiguration());
   if (!ObjectUtils.isEmpty(this.interceptors)) {
     mybatisPlus.setPlugins(this.interceptors);
   }
   MybatisConfiguration mc = new MybatisConfiguration();
   mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
   mybatisPlus.setConfiguration(mc);
   if (this.databaseIdProvider != null) {
     mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
   }
   if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
     mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
   }
   if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
     mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
   }
   if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
     mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
   }
   return mybatisPlus;
 }
}

插件以@bean的形式添加在config文件里例如:


@Bean
 public PaginationInterceptor paginationInterceptor() {
   PaginationInterceptor page = new PaginationInterceptor();
   page.setDialectType("mysql");
   return page;
 }

这是一个分页插件。

代码生成器参考官方文档,但是他的代码生成器可供修改的地方不多,只能控制一下代码生成路径之类的,自由度不高,推荐把mybatisplus 代码生成部分单独抽出来,修改成自己合适的,再打成jar包进行依赖。

springboot properties文件配置


# mybatis_config
mybatis.mapper-locations=classpath:com/boot/mapper/xml/*Mapper.xml
mybatis.typeAliasesPackage=com.boot.entity

前一个是xml文件的路径

后面一个时别名包路径

在springboot的启动类上加上注解


@MapperScan("com.boot.mapper*")
@SpringBootApplication
public class BootApplication {

@mapperscan 里面是dao的扫描路径

mybatisplus 提供了比较齐全的crud即增删改查,不需要在mapper.xml里写sql可以直接调用
例子:


//可以在controller:
Egg egg = new Egg();
eggService.insert(egg);
//可以在service
Egg egg = new Egg();
this.selectList(new EntityWrapper<Egg >(egg));//mybatisplus提供依靠实体查询的方法的写法
//也可以
mapper.selectList(new EntityWrapper<Egg >(egg));

分页查询demo:

dao:返回list


List<Role> getPage(Pagination page, RoleParam param) throws DataAccessException;

xml:照着普通sql写就可以了,其他的会自动拼接


<select id="getPage" resultMap="RoleResultMap">
 select
 <include refid="columns"/>
 from ella_role
 <include refid="where"/>
</select>

service:


public Page<EllaRole> getPage(RoleParam param) {
//new 一个page 初始化传入current当前页,size每页几个,order 排序(默认asc要改的话page.setAsc(false);)
   Page<Role> page = new Page<Role>(param.getCurrent(), param.getSize(), param.getOrder());
   page.setRecords(iRoleMapper.getPage(page, param));
   return page;
 }

end

接下来是一些小贴士

生成的实体里主键要加上@TableId注解不然会报错

数据库里有下划线的字段在查询返回是会取不到值,需要在config文件中的mybatisSqlSessionFactoryBean方法下加上


mybatisPlus.setDbColumnUnderline(true);

domain里的所有属性都会映射到数据库的字段上,如果你加上数据库里没有但要用的属性需要在上面加上@TableField(exist = false)标签,这样他会被忽略

来源:http://blog.csdn.net/u014519194/article/details/53786630

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com