Mybatis中反向生成代碼使用的實(shí)現(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驅(qū)動(dòng)位置 一定需要加 jar名稱 -->
<classPathEntry location="" />
<context id="context1" defaultModelType="flat">
<!-- PO序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
</plugin>
<commentGenerator>
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
<!-- 數(shù)據(jù)庫(kù)的連接信息 -->
<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="實(shí)體包位置"-->
<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" />
<!--生成對(duì)應(yīng)表及類名-->
<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>
<!-- 這個(gè)元素用來(lái)去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) -->
<!-- 如果生成日期,會(huì)造成即使修改一個(gè)字段,整個(gè)實(shí)體類所有屬性都會(huì)發(fā)生變化,不利于版本控制,所以設(shè)置為true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數(shù)據(jù)庫(kù)鏈接URL,用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.25.100:3306/authority" userId="root" password="xxx.">
<!-- 解決table schema中有多個(gè)重名的表生成表結(jié)構(gòu)不一致問(wèn)題 -->
<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>
導(dǎo)入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版本需要添加以下語(yǔ)句,否則實(shí)體類中生成字段不一致
<!-- 數(shù)據(jù)庫(kù)的連接信息 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" userId="root" password="xxx"> <!-- 解決table schema中有多個(gè)重名的表生成表結(jié)構(gòu)不一致問(wèn)題 --> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection>
關(guān)于反向生成多條件查詢代碼的使用
1.創(chuàng)建對(duì)應(yīng)的實(shí)體類example對(duì)象
2.創(chuàng)建Criteria對(duì)象
3.向Criteria對(duì)象中封裝對(duì)應(yīng)參數(shù)的各種條件
4.將example對(duì)象交給selectByExample方法進(jìn)行查詢
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();
}
}
到此這篇關(guān)于Mybatis中反向生成代碼使用的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis 反向生成代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用IDEA創(chuàng)建servlet?JavaWeb?應(yīng)用及使用Tomcat本地部署的實(shí)現(xiàn)
本文主要介紹了使用IDEA創(chuàng)建servlet?JavaWeb?應(yīng)用及使用Tomcat本地部署2022-01-01
Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫(kù)詳解
這篇文章主要為大家詳細(xì)介紹了Java執(zhí)行SQL腳本文件到數(shù)據(jù)庫(kù)的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
spring-boot通過(guò)@Scheduled配置定時(shí)任務(wù)及定時(shí)任務(wù)@Scheduled注解的方法
這篇文章主要介紹了spring-boot通過(guò)@Scheduled配置定時(shí)任務(wù),文中還給大家介紹了springboot 定時(shí)任務(wù)@Scheduled注解的方法,需要的朋友可以參考下2017-11-11
springboot微服務(wù)項(xiàng)目集成html頁(yè)面的實(shí)現(xiàn)
本文主要介紹了springboot微服務(wù)項(xiàng)目集成html頁(yè)面的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
利用IDEA社區(qū)版創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)圖文教程
大家應(yīng)該都知道Idea社區(qū)版本,默認(rèn)是不能創(chuàng)建SpringBoot項(xiàng)目的,下面這篇文章主要給大家介紹了關(guān)于利用IDEA社區(qū)版創(chuàng)建SpringBoot項(xiàng)目的詳細(xì)圖文教程,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
java組件SmartUpload和FileUpload實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了java組件SmartUpload和FileUpload實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

