Prerequisites: already integrated MyBatis
Code generation steps:
- Add dependency
<Dependency> <groupId>Tk.mybatis</groupId> <Artifactid>Mapper-spring-boot-starter</Artifactid> <version>1.1.4</version></Dependency><Dependency> <groupId>Org.mybatis.generator</groupId> <Artifactid>Mybatis-generator-core</Artifactid> <version>1.3.6</version></Dependency>
- Create a class with the main method, the code is as follows
PackageCom.ghaien.generator;ImportOrg.mybatis.generator.api.MyBatisGenerator;Importorg.mybatis.generator.config.Configuration;ImportOrg.mybatis.generator.config.xml.ConfigurationParser;ImportOrg.mybatis.generator.internal.DefaultShellCallback;ImportSun.nio.cs.Surrogate.Generator;Importjava.util.ArrayList;Importjava.util.List; Public classGeneratormain {/*** Code Generator *@paramargs *@throwsException*/ Public Static voidMain (string[] args)throwsException {List<String> warnings =NewArraylist<>(); BooleanOverwrite =true; Configurationparser CP=Newconfigurationparser (warnings); Configuration Config=cp.parseconfiguration (Generator.class. getResourceAsStream ("/generator/generatorconfig.xml")); Defaultshellcallback Callback=Newdefaultshellcallback (overwrite); Mybatisgenerator Mybatisgenerator=Newmybatisgenerator (config, callback, warnings); Mybatisgenerator.generate (NULL); }}
- Create the Generatorconfig.xml configuration file in the corresponding directory in the above code
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Htt P://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "><generatorconfiguration> <ContextID= "Mysql"Targetruntime= "Mybatis3simple"Defaultmodeltype= "Flat"> <plugintype= "Tk.mybatis.mapper.generator.MapperPlugin"> < Propertyname= "Mappers"value= "Tk.mybatis.mapper.common.Mapper"/> <!--casesensitive Default False, this property can be set to True when the database table name is case-sensitive - < Propertyname= "CaseSensitive"value= "true"/> </plugin> <jdbcconnectionDriverclass= "Com.mysql.jdbc.Driver"Connectionurl= "Jdbc:mysql://localhost:3306/test"userId= "root"Password= "1234"> </jdbcconnection> <!--the storage path and package name of the generated entity class - <JavamodelgeneratorTargetpackage= "Com.ghaien.dao.pojo.vo"
Targetproject= "E:\document\IdeaProjects\spring-boot-demo\src\main\java"/> <!--where to store the generated *mapper.xml file - <SqlmapgeneratorTargetpackage= "Mapper"
Targetproject= "E:\document\IdeaProjects\spring-boot-demo\src\main\resources"/> <!--the storage location and package name of the generated *mapper.java file - <JavaclientgeneratorTargetpackage= "Com.ghaien.dao.mapper"
Targetproject= "E:\document\IdeaProjects\spring-boot-demo\src\main\java"type= "Xmlmapper" /> <!--The table name in the corresponding database (you can use "%" for all tables) - <TableTableName= "User" > <!--The table has a primary key with an ID increment, and the corresponding annotation is generated based on that configuration when the entity class is generated - <Generatedkeycolumn= "id"SQLStatement= "Mysql"Identity= "true"/> </Table> </Context></generatorconfiguration>
The above configuration can be configured according to the specific requirements
- Execute the Main method
Integration steps:
- Add dependency
<Dependency> <groupId>Tk.mybatis</groupId> <Artifactid>Mapper-spring-boot-starter</Artifactid> <version>1.1.4</version></Dependency><Dependency> <groupId>Com.github.pagehelper</groupId> <Artifactid>Pagehelper-spring-boot-starter</Artifactid> <version>1.2.1</version></Dependency>
- Add the following configuration in Application.properties
# #mapper # #mappers multiple interfaces comma separated mapper.mappers=com.ghaien.utils.mapper.BaseMappermapper.not-empty= false mapper.identity=mysql## #pagehelperpagehelper. Helperdialect=mysqlpagehelper.reasonable =truepagehelper.supportmethodsarguments=truepagehelper.params=count= Countsql
- Move the generated *mapper.xml file to application.properties in the Mybatis.mapperlocations directory, move the generated entity class location arbitrarily
- Create the Basemapper (named arbitrary) interface, which corresponds to the mapper.mappers in the configuration file, and the code is as follows
Package Com.ghaien.utils.mapper; Import Tk.mybatis.mapper.common.Mapper; Import Tk.mybatis.mapper.common.MySqlMapper; Public Interface extends Mapper<t>, mysqlmapper<t> {}
Modify the generated *mapper.java file so that it inherits Basemapper, and move to the path @mapperscan can scan to, the code is as follows
PackageCom.ghaien.dao.mapper;ImportCom.ghaien.dao.pojo.vo.User;ImportOrg.apache.ibatis.annotations.Param;ImportTk.mybatis.mapper.common.BaseMapper; Public InterfaceUsermapperextendsBasemapper<user> { /*** Test usermapper.xml file path is correct *@paramID *@return */User Querybyid (@Param ("id") Long ID);}
Note : Basemapper cannot be scanned by @mapperscan
- Test for successful integration, the code is as follows
PackageCom.ghaien.dao.mapper;ImportCom.ghaien.dao.pojo.vo.User;ImportCom.github.pagehelper.Page;ImportCom.github.pagehelper.PageHelper;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.test.context.junit4.SpringRunner;Importjava.util.List;/*** Created by Ghaien on 2018/1/29.*/@SpringBootTest @runwith (springrunner.class) Public classusermappertest {@AutowiredPrivateUsermapper Usermapper; PrivateLogger log = Loggerfactory.getlogger ( This. GetClass ()); @Test Public voidTestquerybypage () {//Pagehelper.startpage (2, 1);//list<user> users = Usermapper.selectall ();//For (User user:users) {//log.info ("userName =" + User.getusername ());// }Page<User> page = Pagehelper.startpage (2, 1). Doselectpage ((){usermapper.selectall (); }); for(User user:page) {log.info ("UserName =" +user.getusername ()); } }}
The results of the last two methods are consistent, if you want to view the SQL statement simply add the following configuration in the configuration file
Logging.level.com.ghaien.dao=debug
Full code
Spring-boot Integrated Pagehelper and general mapper