Mybatis中反向生成代碼使用的實現(xiàn)
反向生成核心配置文件generatorConfig.xml (eclipse)
<?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 >
<!-- 本地電腦的 mysql驅動位置 一定需要加 jar名稱 -->
<classPathEntry location="" />
<context id="context1" defaultModelType="flat">
<!-- PO序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
</plugin>
<commentGenerator>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!-- 數(shù)據(jù)庫的連接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/no4"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model類存放位置 targetPackage="實體包位置"-->
<javaModelGenerator targetPackage="com.oracle.pojo"
targetProject="">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="com.oracle.mapper"
targetProject="">
<property name="trimStrings" value="true" />
</sqlMapGenerator>
<!--生成Dao類存放位置 -->
<javaClientGenerator targetPackage="com.oracle.mapper"
targetProject="" type="XMLMAPPER" />
<!--生成對應表及類名-->
<table schema="" tableName="" domainObjectName="" enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
</table>
</context>
</generatorConfiguration>
反向生成核心配置文件generatorConfig.xml (idea)
<?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>
<context id="test" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
<commentGenerator>
<!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護 -->
<!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發(fā)生變化,不利于版本控制,所以設置為true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數(shù)據(jù)庫鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.25.100:3306/authority" userId="root" password="xxx.">
<!-- 解決table schema中有多個重名的表生成表結構不一致問題 -->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<!-- This property is used to specify whether MyBatis Generator should
force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.li.pojo"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="/mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.li.mapper" implementationPackage="com.li.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="categorys" domainObjectName="Category"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
</table>
</context>
</generatorConfiguration>
導入maven
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路徑 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>注意:mysql8.0版本需要添加以下語句,否則實體類中生成字段不一致
<!-- 數(shù)據(jù)庫的連接信息 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" userId="root" password="xxx"> <!-- 解決table schema中有多個重名的表生成表結構不一致問題 --> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection>
關于反向生成多條件查詢代碼的使用
1.創(chuàng)建對應的實體類example對象
2.創(chuàng)建Criteria對象
3.向Criteria對象中封裝對應參數(shù)的各種條件
4.將example對象交給selectByExample方法進行查詢
public List<Cities> findCityByProId(String provinceId) {
SqlSession sqlSession = DBUtils.createDbUtils().getSQLSession();
CitiesMapper mapper = sqlSession.getMapper(CitiesMapper.class);
try {
CitiesExample citiesExample = new CitiesExample();
Criteria criteria = citiesExample.createCriteria();
criteria.andProvinceidEqualTo(provinceId);
List<Cities> citiesList = mapper.selectByExample(citiesExample);
return citiesList;
}finally {
sqlSession.close();
}
}
到此這篇關于Mybatis中反向生成代碼使用的實現(xiàn)的文章就介紹到這了,更多相關Mybatis 反向生成代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用IDEA創(chuàng)建servlet?JavaWeb?應用及使用Tomcat本地部署的實現(xiàn)
本文主要介紹了使用IDEA創(chuàng)建servlet?JavaWeb?應用及使用Tomcat本地部署2022-01-01
Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫詳解
這篇文章主要為大家詳細介紹了Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫的相關方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
spring-boot通過@Scheduled配置定時任務及定時任務@Scheduled注解的方法
這篇文章主要介紹了spring-boot通過@Scheduled配置定時任務,文中還給大家介紹了springboot 定時任務@Scheduled注解的方法,需要的朋友可以參考下2017-11-11
springboot微服務項目集成html頁面的實現(xiàn)
本文主要介紹了springboot微服務項目集成html頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
利用IDEA社區(qū)版創(chuàng)建SpringBoot項目的詳細圖文教程
大家應該都知道Idea社區(qū)版本,默認是不能創(chuàng)建SpringBoot項目的,下面這篇文章主要給大家介紹了關于利用IDEA社區(qū)版創(chuàng)建SpringBoot項目的詳細圖文教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-04-04
java組件SmartUpload和FileUpload實現(xiàn)文件上傳功能
這篇文章主要為大家詳細介紹了java組件SmartUpload和FileUpload實現(xiàn)文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11

