欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間: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ū)動(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ù)庫的連接信息 -->
		  <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è)元素用來去除指定生成的注釋中是否包含生成的日期 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ù)庫鏈接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)不一致問題 -->
            <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中有多個(gè)重名的表生成表結(jié)構(gòu)不一致問題 -->
			<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)文章

最新評(píng)論