mybatis +如何实现在春天引导上使用

  介绍

mybatis +如何实现在春天引导上使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

maven依赖

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

配置文件

@ configuration   公开课MybatisPlusConfig {   @ autowired   私人数据源的数据源;      @ autowired   私人MybatisProperties属性;      @ autowired   私人ResourceLoader ResourceLoader=new DefaultResourceLoader ();      @ autowired(要求=false)   私人拦截器[]拦截器;      @ autowired(要求=false)   私人DatabaseIdProvider DatabaseIdProvider;/* *   * mybatis-plus分页插件   */@ bean   公共PaginationInterceptor PaginationInterceptor () {   PaginationInterceptor页面=new PaginationInterceptor ();   page.setDialectType (“mysql");   返回页面;   }/* *   *这里全部使用mybatis-autoconfigure已经自动加载的资源。不手动指定   *配置文件和mybatis-boot的配置文件同步   * @return   */@ bean   公共MybatisSqlSessionFactoryBean MybatisSqlSessionFactoryBean () {   MybatisSqlSessionFactoryBean mybatisPlus=new MybatisSqlSessionFactoryBean ();   mybatisPlus.setDataSource(数据源);   mybatisPlus.setVfs (SpringBootVFS.class);   如果(StringUtils.hasText (this.properties.getConfigLocation ())) {   mybatisPlus.setConfigLocation (this.resourceLoader.getResource (this.properties.getConfigLocation ()));   }   mybatisPlus.setConfiguration (properties.getConfiguration ());   如果(! ObjectUtils.isEmpty (this.interceptors)) {   mybatisPlus.setPlugins (this.interceptors);   }   MybatisConfiguration mc=new MybatisConfiguration ();   mc.setDefaultScriptingLanguage (MybatisXMLLanguageDriver.class);   mybatisPlus.setConfiguration (mc);   如果这一点。databaseIdProvider !=null) {   mybatisPlus.setDatabaseIdProvider (this.databaseIdProvider);   }   如果(StringUtils.hasLength (this.properties.getTypeAliasesPackage ())) {   mybatisPlus.setTypeAliasesPackage (this.properties.getTypeAliasesPackage ());   }   如果(StringUtils.hasLength (this.properties.getTypeHandlersPackage ())) {   mybatisPlus.setTypeHandlersPackage (this.properties.getTypeHandlersPackage ());   }   如果(! ObjectUtils.isEmpty (this.properties.resolveMapperLocations ())) {   mybatisPlus.setMapperLocations (this.properties.resolveMapperLocations ());   }   返回mybatisPlus;   }   }

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

@ bean   公共PaginationInterceptor PaginationInterceptor () {   PaginationInterceptor页面=new PaginationInterceptor ();   page.setDialectType (“mysql");   返回页面;   }

这是一个分页插件。

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

springboot属性文件配置

 # mybatis_config
  mybatis.mapper-locations=类路径:com/boot/mapper/xml/* Mapper.xml
  mybatis.typeAliasesPackage=com.boot。实体

前一个是xml文件的路径

后面一个时别名包路径

在springboot的启动类上加上注解

@MapperScan (“com.boot.mapper *“)   @SpringBootApplication   公开课BootApplication {

@mapperscan里面是刀的扫描路径

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

//可以在控制器:   鸡蛋蛋=新蛋();   eggService.insert(蛋);//可以在服务   鸡蛋蛋=新蛋();   这一点。selectList(新EntityWrapper<蛋祝辞(蛋));//mybatisplus提供依靠实体查询的方法的写法//也可以   映射器。selectList(新EntityWrapper<蛋祝辞(蛋));

分页查询演示:

道:返回列表

代码如下:
ListgetPage(分页页面,RoleParam param)抛出DataAccessException;

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

& lt;选择id=癵etPage"resultMap=癛oleResultMap"比;   选择   & lt;包括refid=癱olumns"/比;   从ella_role   & lt;包括refid=皐here"/比;   & lt;/select>服务:

mybatis +如何实现在春天引导上使用