springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能
2017-02-20 17:44
846 查看
整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同。
主要讲maven的pom.xml和一些配置变化,详细说明。
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。http://projects.spring.io/spring-boot/
MyBatis是一个基于Java的数据持久层框架,其原名是iBatis,在升级到3.0版本后,更名为MyBatis。https://github.com/mybatis/mybatis-3/
MyBatis Generator是一个MyBatis的代码生成器,通过配置,可自动生成数据操作接口、实体类以及mapper.xml文件。https://github.com/mybatis/generator
使用eclipseIDE,新建maven工程。
在pom.xml文件中,添加如下内容,引入相关jar。mybatis-generator版本是1.3.5。
View Code
在application启动类中,增加注解,表示何处扫描mapper接口。
至此,mybatis配置完成。
springboot application.properties配置可参考官方文档。
ExampleController
测试类
SpringBoot打包运行
使用maven命令
可将springboot工程,打包为一个可运行的jar包。
注意:mapper的xml文件在src/main/java目录中,maven默认是不编译的,导致jar中缺少xml配置文件。需修改pom.xml配置,初始pom.xml已修改。
主要讲maven的pom.xml和一些配置变化,详细说明。
软件简介
Spring是一个流行的控制反转(IoC)和面向切面(AOP)的容器框架,在java webapp开发中使用广泛。http://projects.spring.io/spring-framework/Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。http://projects.spring.io/spring-boot/
MyBatis是一个基于Java的数据持久层框架,其原名是iBatis,在升级到3.0版本后,更名为MyBatis。https://github.com/mybatis/mybatis-3/
MyBatis Generator是一个MyBatis的代码生成器,通过配置,可自动生成数据操作接口、实体类以及mapper.xml文件。https://github.com/mybatis/generator
maven开发环境搭建
可以使用http://start.spring.io/初始化maven工程。使用eclipseIDE,新建maven工程。
在pom.xml文件中,添加如下内容,引入相关jar。mybatis-generator版本是1.3.5。
package com.founder.ebd.util.mybatis; import java.util.List; import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.Plugin; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.JavaVisibility; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.Parameter; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.XmlElement; public class MySQLPaginationPlugin extends PluginAdapter { @Override public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { // add field, getter, setter for limit clause addLimit(topLevelClass, introspectedTable, "limitStart"); addLimit(topLevelClass, introspectedTable, "count"); // add the method that get the only Criteria addCriteriaGetter(topLevelClass, introspectedTable); return super.modelExampleClassGenerated(topLevelClass, introspectedTable); } @Override public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart >= 0")); //$NON-NLS-1$ //$NON-NLS-2$ isNotNullElement.addElement(new TextElement("limit ${limitStart} , ${count}")); element.addElement(isNotNullElement); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } @Override public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) { XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$ isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart >= 0")); //$NON-NLS-1$ //$NON-NLS-2$ isNotNullElement.addElement(new TextElement("limit ${limitStart} , ${count}")); element.addElement(isNotNullElement); return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable); } @Override public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, Plugin.ModelClassType modelClassType) { return super.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType); } private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(FullyQualifiedJavaType.getIntInstance()); field.setName(name); field.setInitializationString("-1"); commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); char c = name.charAt(0); String camel = Character.toUpperCase(c) + name.substring(1); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("set" + camel); method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name)); method.addBodyLine("this." + name + "=" + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } private void addCriteriaGetter(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { CommentGenerator commentGenerator = context.getCommentGenerator(); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("getCriteria"); method.setReturnType(new FullyQualifiedJavaType("Criteria")); method.addBodyLine("if (oredCriteria.size() != 0) {return oredCriteria.get(0);}"); method.addBodyLine("Criteria criteria = createCriteriaInternal();"); method.addBodyLine("oredCriteria.add(criteria);"); method.addBodyLine("return criteria;"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); } @Override public boolean validate(List<String> arg0) { // TODO Auto-generated method stub return true; } }
View Code
SpringBoot配置
在src/main/resources/application.properties中增加mybatis配置和jdbc配置。表示mybatis的配置文件未知,以及mapper的xml文件位置。mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:com/founder/springboot/mapper/database/*.xml spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://172.19.34.114:3306/founder_ali?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456
在application启动类中,增加注解,表示何处扫描mapper接口。
package com.founder.springboot; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.founder.springboot.mapper.database") public class FirstApplication { public static void main(String[] args) { SpringApplication.run(FirstApplication.class, args); } }
至此,mybatis配置完成。
springboot application.properties配置可参考官方文档。
测试生成的代码
ExampleServicepackage com.founder.springboot.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.founder.springboot.mapper.database.SysUserMapper; import com.founder.springboot.model.database.SysUser; import com.founder.springboot.model.database.SysUserExample; @Service public class ExampleService { @Autowired SysUserMapper sysUser; public List<SysUser> get() { SysUserExample example = new SysUserExample(); example.setLimitStart(0); example.setCount(10); return sysUser.selectByExample(example); } }
ExampleController
package com.founder.springboot.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.founder.springboot.model.database.SysUser; import com.founder.springboot.service.ExampleService; @RestController public class ExampleController { @Autowired private ExampleService service; @RequestMapping(value = "/get", produces = "application/json; charset=UTF-8") public List<SysUser> get() { return service.get(); } }
测试类
package com.founder.springboot.controller; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import com.founder.springboot.FirstApplication; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = FirstApplication.class) @WebAppConfiguration public class ExampleControllerTest { @Autowired private ExampleController controller; @Test public void get() throws Exception { System.out.println(controller.get()); } }
SpringBoot打包运行
使用maven命令
mvn clean package
可将springboot工程,打包为一个可运行的jar包。
注意:mapper的xml文件在src/main/java目录中,maven默认是不编译的,导致jar中缺少xml配置文件。需修改pom.xml配置,初始pom.xml已修改。
相关文章推荐
- spring和mybatis集成,自动生成model、mapper,增加mybatis分页功能
- (二十二)SpringBoot之使用mybatis generator自动生成bean、mapper、mapper xml
- SpringBoot集成Mybatis并具有分页功能PageHelper
- spring boot配置mybatis代码自动生成(mybatis通用mapper相关基础代码自动生成)
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- Spring3 Mybatis3 freemarker 自动生成对应表的model、mapper、service、controller并自动修改mybatis配置文件
- Spring Boot整合mybatis并自动生成mapper和实体实例解析
- SpringBoot学习教程 - 06 - 集成MyBatis Generator自动生成代码
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
- Spring Boot集成Mybatis及通用Mapper
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
- mybatis 之 generator插件使用 集成到了eclipse 自动生成实体,example,mapper
- MyBatis 使用Generator自动生成Model , Dao, mapper
- mybatis-gen自动生成Mapper中加入分页 (MySQL和Oracle)
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
- 使用Mybatis-Generator自动生成Dao、model和mapper.xml相关文件
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
- Spring Boot MyBatis 通用Mapper插件集成
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客