Mybatis逆向工程,自動生成 entity類和經常使用的增刪改查方法。java
- 1.pom.xml引入類
<!-- 通用mapper 用於mabatis封裝的基礎增刪改查的功能-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId><!--已引入了這個,故此處例外掉其默認自帶的-->
</exclusion>
</exclusions>
</dependency>
<!-- 逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<scope>provided</scope>
</dependency>
- 2.springboot入口類,引入換一下
- 3.配置文件:\src\main\resources\resources\generatorConfig.xml
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <properties resource="application.properties"></properties> <!-- 數據庫驅動:選擇你的本地硬盤上面的數據庫驅動包--> <classPathEntry location="D:\sqljdbc4.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數據庫連接URL,用戶名、密碼 --> <!--<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=gkzp" userId="sa" password="nsoft"> </jdbcConnection>--> <jdbcConnection driverClass="${spring.datasource.druid.driverClassName}" connectionURL="${spring.datasource.druid.url}" userId="${spring.datasource.druid.username}" password="${spring.datasource.druid.password}"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.generator.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.generator.mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.generator.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名--> <!-- <table tableName="HR_Recruit_entryInfo_base" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Recruit_entryInfo_education" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Recruit_entryInfo_family" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Recruit_entryInfo_other" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Recruit_entryInfo_work" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Post_Type" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Recurit_Plan_Needs" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="HR_Recurit_Plan_Return" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> --> <table tableName="sys_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
- 4.配置Java類
\src\main\java\com\nsoft\gkzp\system\sysuser\service\SysUserServiceImpl.java spring
File configFile文件參數指定generatorConfig.xml配置文件位置
package com.nsoft.gkzp.util; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.util.ArrayList; import java.util.List; public class GeneratorSqlmap { public void generator() throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("./src/main/resources/resources/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings); myBatisGenerator.generate(null); } /** * 運行這個main方法就能夠進行逆向工程 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { try { GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
運行上面的main方法,就會自動生成三個文件(以sys_User表爲例)(以下圖)sql
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
create table sys_user( id int identity(1,1) primary key not null, --id主鍵 自增加 loginName nvarchar(50) NOT NULL , --登陸用戶名 password nvarchar(50) NOT NULL , --登陸密碼 email nvarchar(255) , --用戶郵箱 (郵箱登陸) phone nvarchar(15) , --用戶手機號(手機號登陸) createTime datetime default CONVERT(varchar(100), GETDATE(), 20), --用戶建立時間 pwdChangeTime datetime default CONVERT(varchar(100), GETDATE(), 20), --改密碼時間(初始值爲用戶建立時間) code varchar(255) , --'激活碼' usertype int NOT NULL default 2, --用戶類型(0系統管理員 一、內網用戶 二、普通用戶) state int NOT NULL default 0 , --用戶激活狀態:0表示未激活,1表示激活 nstatusid int default 1 --用戶狀態 0無效 1有效 );
另:SysUser實體類中,例如loginname字段均爲小寫的,若是表字段設計爲login_name的話,生成的實體類此字段映射爲loginName,就會大寫。同理表名也是如此。數據庫
- 5.實際應用
生成後,能夠將api
5.1 接口類SysUserMapper.java、映射文件UserMapper.xml 兩個文件刪除了,用不到。 實體類SysUser.java複製粘貼到你要放實際放的地方就能夠了。springboot
5.2 實際的dao層接口類繼承 Mapper<com.generator.model.SysUser> ,繼承後,基礎的增刪改查操做mybatis已經自動生成並封裝,不須要寫了,這裏只寫其餘業務邏輯方法就能夠了。mybatis
注意:1).這裏<com.generator.model.SysUser>爲你生成實體類存放位置,如你在4.1步驟將SysUser.java實體類複製粘貼到其餘位置,那麼這裏就寫新位置的路徑app
2). 繼承的Mapper爲 tk.mybatis.mapper.common.Mapper。dom
在service實現類,就能夠直接使用mybatis自動生成並封裝的基礎的增刪改爲操做,以下圖。ide