最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis中反向生成代碼使用的實(shí)現(xiàn)

 更新時間:2025年07月07日 10:19:14   作者:陌殤殤  
本文詳細(xì)介紹了在Mybatis中使用代碼反向生成器,包括generatorConfig.xml配置在Eclipse和IntelliJ IDEA環(huán)境下的設(shè)置,如何通過Maven導(dǎo)入依賴并執(zhí)行多條件查詢,

反向生成核心配置文件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ū)動位置  一定需要加  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="實(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" />

		<!--生成對應(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>
            <!-- 這個元素用來去除指定生成的注釋中是否包含生成的日期 false:表示保護(hù) -->
            <!-- 如果生成日期,會造成即使修改一個字段,整個實(shí)體類所有屬性都會發(fā)生變化,不利于版本控制,所以設(shè)置為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中有多個重名的表生成表結(jié)構(gòu)不一致問題 -->
            <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版本需要添加以下語句,否則實(shí)體類中生成字段不一致

			<!-- 數(shù)據(jù)庫的連接信息 -->
		  <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" 
		          connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" 
		          userId="root" 
		          password="xxx"> 
		       <!-- 解決table schema中有多個重名的表生成表結(jié)構(gòu)不一致問題 -->
			<property name="nullCatalogMeansCurrent" value="true"/>
			</jdbcConnection>
			

關(guān)于反向生成多條件查詢代碼的使用

1.創(chuàng)建對應(yīng)的實(shí)體類example對象

2.創(chuàng)建Criteria對象

3.向Criteria對象中封裝對應(yīng)參數(shù)的各種條件

4.將example對象交給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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • java求數(shù)組第二大元素示例

    java求數(shù)組第二大元素示例

    這篇文章主要介紹了java求數(shù)組第二大元素示例,需要的朋友可以參考下
    2014-04-04
  • mybatis使用pageHelper插件進(jìn)行查詢分頁

    mybatis使用pageHelper插件進(jìn)行查詢分頁

    這篇文章主要介紹了mybatis使用pageHelper插件進(jìn)行查詢分頁,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Springboot?通過FastJson實(shí)現(xiàn)bean對象和Json字符串互轉(zhuǎn)問題

    Springboot?通過FastJson實(shí)現(xiàn)bean對象和Json字符串互轉(zhuǎn)問題

    這篇文章主要介紹了Springboot?通過FastJson實(shí)現(xiàn)bean對象和Json字符串互轉(zhuǎn),本文嘗試驗(yàn)證兩種場景給大家詳細(xì)介紹,對Springboot?FastJson實(shí)現(xiàn)bean和Json互轉(zhuǎn)問題,感興趣的朋友一起看看吧
    2022-08-08
  • IntelliJ IDEA 2017 漢化包及圖文教程

    IntelliJ IDEA 2017 漢化包及圖文教程

    這篇文章主要介紹了IntelliJ IDEA 2017 漢化包及圖文教程,文中給大家提供兩種方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-03-03
  • Java Map 在put值時value值不被覆蓋的解決辦法

    Java Map 在put值時value值不被覆蓋的解決辦法

    這篇文章主要介紹了Java Map 在put值時value值不被覆蓋的解決辦法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-04-04
  • 通過入門demo簡單了解netty使用方法

    通過入門demo簡單了解netty使用方法

    這篇文章主要介紹了通過入門demo簡單了解netty使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • IDEA插件之mybatisx?插件使用教程

    IDEA插件之mybatisx?插件使用教程

    這篇文章主要介紹了mybatisx?插件使用教程,包括插件安裝自動生成代碼的相關(guān)知識,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Springboot源碼 TargetSource解析

    Springboot源碼 TargetSource解析

    這篇文章主要介紹了Springboot源碼 TargetSource解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • SpringBoot工程搭建打包、啟動jar包和war包的教程圖文詳解

    SpringBoot工程搭建打包、啟動jar包和war包的教程圖文詳解

    這篇文章主要介紹了SpringBoot工程搭建打包、啟動jar包和war包的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java實(shí)現(xiàn)飛機(jī)航班管理系統(tǒng)的思路詳解

    Java實(shí)現(xiàn)飛機(jī)航班管理系統(tǒng)的思路詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)飛機(jī)航班管理系統(tǒng)的思路詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07

最新評論

呈贡县| 佛坪县| 南漳县| 宝清县| 枣阳市| 麟游县| 平果县| 滕州市| 凉城县| 孝昌县| 岐山县| 罗江县| 宜黄县| 墨江| 庆云县| 德安县| 鱼台县| 嵊泗县| 南陵县| 崇礼县| 郯城县| 察隅县| 常德市| 罗江县| 田东县| 桓台县| 哈尔滨市| 兰坪| 武功县| 钦州市| 昆明市| 新郑市| 横峰县| 拜城县| 四子王旗| 旬邑县| 安庆市| 车险| 池州市| 建水县| 吴桥县|