mybatis-generator-maven-plugin插件的一些坑
阿新 • • 發佈:2019-04-09
函數 覆蓋 ngs ttr param size main llc img 。
1、配置mybatis-generator-maven-plugin插件
在maven的pom.xml的<build></build>節點內添加
<!--要放在與pluginManagement同級別才能生效--> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>false</overwrite> </configuration> </plugin> </plugins>
代碼段中標紅的部分不寫的話,每次自動生成後會出現以數字後綴的備份文件 如圖
配置好maven自動下載,一定要註意把xml代碼段放到與pluginManagement同級別,idea下的maven插件裏才能出來
安裝完成後,idea右側的maven插件裏就有了如下圖:
2、配置generatorConfig.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 56 <generatorConfiguration> 7 8 <!--導入屬性配置--> 9 <properties resource="jdbc.properties"></properties> 10 <classPathEntry location="${jdbc.jar}"/> 11 12 <context id="DB2Tables" targetRuntime="MyBatis3"> 13 14 <!-- 生成註釋為false 不生成為true 【不生成註釋時會被重復寫入導致報錯】 --> 15 <commentGenerator> 16 <property name="suppressAllComments" value="false"/> 17 </commentGenerator> 18 19 <!--jdbc的數據庫連接 --> 20 <jdbcConnection 21 driverClass="${jdbc.Driverclass}" 22 connectionURL="${jdbc.jdbcurl}" 23 userId="${jdbc.user}" 24 password="${jdbc.password}"> 25 </jdbcConnection> 26 27 <javaTypeResolver > 28 <property name="forceBigDecimals" value="false" /> 29 </javaTypeResolver> 30 31 <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 32 targetPackage 指定生成的model生成所在的包名 33 targetProject 指定在該項目下所在的路徑 34 --> 35 <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">--> 36 <javaModelGenerator targetPackage="com.*.pojo" targetProject="./src/main/java"> 37 <!-- 是否允許子包,即targetPackage.schemaName.tableName --> 38 <property name="enableSubPackages" value="false"/> 39 <!-- 是否對model添加 構造函數 --> 40 <property name="constructorBased" value="true"/> 41 <!-- 是否對類CHAR類型的列的數據進行trim操作 --> 42 <property name="trimStrings" value="true"/> 43 <!-- 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> 44 <property name="immutable" value="false"/> 45 </javaModelGenerator> 46 47 <!--mapper映射文件生成所在的目錄 為每一個數據庫的表生成對應的SqlMap文件 --> 48 <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> 49 <property name="enableSubPackages" value="false"/> 50 </sqlMapGenerator> 51 52 <!-- targetPackage:mapper接口dao生成的位置 --> 53 <javaClientGenerator type="XMLMAPPER" targetPackage="com.*.dao" targetProject="./src/main/java"> 54 <!-- enableSubPackages:是否讓schema作為包的後綴 --> 55 <property name="enableSubPackages" value="false" /> 56 </javaClientGenerator> 57 58 59 <table tableName="admin" domainObjectName="Admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 60 <property name="rootInterface" value="com.*.dao.AdminExtMapper"/> 61 </table> 62 <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> 63 64 65 </context> 66 </generatorConfiguration>
http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd 這條報紅 不用管
<!-- 生成註釋為false 不生成為true 【不生成註釋時會被重復寫入導致報錯】 --> <commentGenerator> <property name="suppressAllComments" value="false"/> </commentGenerator>
加上註釋後,每次生成xml和生成的實體類不會被覆蓋,不加註釋的話 xml會被重復寫入,導致程序報錯。
3、數據庫字段更新問題
當後期數據庫的字段新增或者修改後,重新執行插件,會發現DAO層下自己辛辛苦苦寫的方法代碼被覆蓋了,只能使用先備份再生成然後再手工修改xml文件和mapper文件。這樣太麻煩了。
查了下官方文檔,我試著用父接口的形式來實現。
以admin數據表為例
CREATE TABLE `admin` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
generatorConfig.xml 文件下修改 讓AdminMapper 接口繼承一個AdminExtMapper的父接口
<table tableName="admin" domainObjectName="Admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="rootInterface" value="com.*.dao.AdminExtMapper"/> </table>
點擊自動生成了 AdminMapper、AdminMapper.xml 和 Admin類
package com.*.dao;
import com.*.pojo.Admin;
public interface AdminMapper extends AdminExtMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table admin
*
* @mbg.generated Tue Apr 09 17:24:16 CST 2019
*/
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table admin
*
* @mbg.generated Tue Apr 09 17:24:16 CST 2019
*/
int insert(Admin record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table admin
*
* @mbg.generated Tue Apr 09 17:24:16 CST 2019
*/
int insertSelective(Admin record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table admin
*
* @mbg.generated Tue Apr 09 17:24:16 CST 2019
*/
Admin selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table admin
*
* @mbg.generated Tue Apr 09 17:24:16 CST 2019
*/
int updateByPrimaryKeySelective(Admin record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table admin
*
* @mbg.generated Tue Apr 09 17:24:16 CST 2019
*/
int updateByPrimaryKey(Admin record);
}
在DAO層 寫一個 AdminExtMapper 父接口 放入自己寫的方法
public interface AdminExtMapper {
int SelectBy(String username);
Admin findAdmin(@Param("username") String username, @Param("password") String password);
}
在mapper文件夾下創建AdminExtMapper.xml 把AdminMapper.xml下的代碼復制過來,精簡一下,加上自己的sql語句
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.imcdn.dao.AdminExtMapper"> <resultMap id="BaseResultMap" type="com.*.pojo.Admin"> <constructor> <idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER"/> <arg column="username" javaType="java.lang.String" jdbcType="VARCHAR"/> <arg column="password" javaType="java.lang.String" jdbcType="VARCHAR"/> </constructor> </resultMap> <sql id="Base_Column_List"> id, username, password </sql> <select id="SelectBy" parameterType="string" resultType="int"> select count(id) from admin where username=#{username} </select> <select id="findAdmin" parameterType="map" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from admin where username=#{username} and password=#{password} </select> </mapper>
這樣的話 當修改字段後 再次生成時就不用重復改代碼了。
測試
AdminServiceImpl
@Service("iAdminService") public class AdminServiceImpl implements IAdminSevice { @Autowired private AdminMapper adminMapper; @Override public ServerResponse<Admin> login(String username, String password) { int i = adminMapper.SelectBy(username); if(i==0){ return ServerResponse.createByErrorMsg("該用戶名不存在"); } String md5Password= MD5Util.MD5EncodeUtf8(password); Admin admin = adminMapper.findAdmin(username, md5Password); if(admin==null){ return ServerResponse.createByErrorMsg("用戶密碼不匹配"); } return ServerResponse.createBySuccess("登陸成功",admin); } }
AdminController
Controller @RequestMapping("/admin/") public class AdminController { @Autowired private IAdminSevice iAdminSevice; @RequestMapping(value = "login.do", method = RequestMethod.POST) @ResponseBody public ServerResponse<Admin> login(String usename, String password, HttpSession session) { ServerResponse<Admin> response = iAdminSevice.login(usename, password); if (response.isSuccess()) { session.setAttribute(Const.CURRENT_USER, response.getData()); } return response; } }
測試結果
mybatis-generator-maven-plugin插件的一些坑