Mybatis 역방향 공사 의 세 가지 방법 에 대한 상세 한 설명

20577 단어 Mybatis역공학
Mybatis 역방향 프로젝트
  역방향 프로젝트 는 일반적으로 데이터베이스 테이블 에서 자바 코드 를 생 성하 고 자바 코드 를 통 해 데이터베이스 테이블 을 생 성 하 는 것 을 포함한다.Mybatis 역방향 프로젝트 는 데이터베이스 시트 에서 자바 코드 를 생 성 하 는 것 을 말한다.
  Mybaits 는 프로그래머 가 직접 SQL 문 구 를 작성 해 야 하지만 Mybatis 는 공식 적 으로 역방향 프로젝트 를 제공 하여 단일 표 에 대해 Mybaits 실행 에 필요 한 코드 를 자동 으로 생 성 할 수 있 습 니 다.POJO,Mapper.자바,Mapper.xml 를 포함 합 니 다.
1.Eclipse 플러그 인 을 통 해 Mybatis 역방향 프로젝트 를 완성 합 니 다.
1.온라인 으로 Eclipse 플러그 인 설치
  작업 절차:Eclipse=>Help=>Eclipse Marketplace=>Mybatis Generator 검색=>Mybatis Generator 버 전 선택=>Install=>재 부팅.
下载安装 Mybatis Generator
2.자바 프로젝트 새로 만 들 기
  mybatisGenerator 라 는 자바 프로젝트 를 새로 만 들 고 MySQL 드라이버 패 키 지 를 가 져 옵 니 다.Oracle 데이터베이스 라면 Oracle 드라이버 패 키 지 를 가 져 옵 니 다.저 는 MySQL 데이터베이스 이기 때문에 MySQL 을 가 져 옵 니 다.
Java 项目结构
3.프로필 작성
  역방향 프로젝트 는 xml 프로필 을 사용 해 야 합 니 다.프로필(generator Config.xml)을 작성 하려 면 다음 과 같 습 니 다.

<?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="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!--             true:  : false:  -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--        :   、    、   、   -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!--   false, JDBC DECIMAL   NUMERIC       Integer,  true  JDBC DECIMAL   
			NUMERIC      java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:  PO     -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="mybatisGenerator">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
			<!--                  -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
  <!-- targetProject:mapper          -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="mybatisGenerator">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper        -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="mybatisGenerator">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!--        -->
		<!-- 
			tableName:      
  		domainObjectName:       
	  enableCountByExample:Count     where    ,  true  
	  enableUpdateByExample:Update     where    ,  true  
	  enableDeleteByExample:Delete     where    ,  true  
	  enableSelectByExample:Select       where    ,  true  
	  selectByExampleQueryId:Select         where    ,  true  
		 -->
		<table tableName="items">
			<!-- 
				  :
				property:             ,    
				ignoreColumn:         
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>
주의:targetProject="mybatisGenerator"4.플러그 인 으로 실행
  작업 절차:generator Config.xml 파일=>Run as=>Run Mybatis Generator=>새로 고침 프로젝트 를 우 클릭 합 니 다.
生成的代码
  오류 가 발생 한 것 은 Mybatis 관련 가방 을 가 져 오지 않 았 기 때 문 입 니 다.마지막 으로 생 성 된 파일 을 관련 프로젝트 에 복사 합 니 다.
2.자바 코드 를 통 해 Mybatis 역방향 프로젝트 를 완성 합 니 다.
1.자바 프로젝트 새로 만 들 기
  자바 프로젝트 를 새로 만 들 고 Mybatis 역방향 프로젝트 패키지 mybatis-generator-core-1.3.2.jar 과 데이터베이스 드라이브 패키지 mysql-connector-java-5.1.39-bin.jar 을 가 져 옵 니 다.
Java Project 项目结构
2.프로필 작성
  프로필 을 작성 합 니 다.이전 방법의 프로필 과 차이 가 많 지 않 습 니 다.이 곳 의 targetProject 와 달리 targetProject="./src" 방식 으로 생 성 된 파일 도 이 아래 에 있 습 니 다.

<?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="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!--             true:  : false:  -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--        :   、    、   、   -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!--   false, JDBC DECIMAL   NUMERIC       Integer,  true  JDBC DECIMAL   
			NUMERIC      java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:  PO     -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
			<!--                  -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
  <!-- targetProject:mapper          -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper        -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!--        -->
		<!-- 
			tableName:      
  		domainObjectName:       
	  enableCountByExample:Count     where    ,  true  
	  enableUpdateByExample:Update     where    ,  true  
	  enableDeleteByExample:Delete     where    ,  true  
	  enableSelectByExample:Select       where    ,  true  
	  selectByExampleQueryId:Select         where    ,  true  
		 -->
		<table tableName="items">
			<!-- 
				  :
				property:             ,    
				ignoreColumn:         
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>
3.생 성 코드 프로그램 작성
마지막 으로 간단 한 자바 실행 프로그램 을 만 들 고 실행 후 프로젝트 를 새로 고치 면 됩 니 다.

<?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="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!--             true:  : false:  -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--        :   、    、   、   -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!--   false, JDBC DECIMAL   NUMERIC       Integer,  true  JDBC DECIMAL   
			NUMERIC      java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:  PO     -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
			<!--                  -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
  <!-- targetProject:mapper          -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper        -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!--        -->
		<!-- 
			tableName:      
  		domainObjectName:       
	  enableCountByExample:Count     where    ,  true  
	  enableUpdateByExample:Update     where    ,  true  
	  enableDeleteByExample:Delete     where    ,  true  
	  enableSelectByExample:Select       where    ,  true  
	  selectByExampleQueryId:Select         where    ,  true  
		 -->
		<table tableName="items">
			<!-- 
				  :
				property:             ,    
				ignoreColumn:         
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>
生成的代码结构   
이 항목 에 로 그 를 추가 하면 운행 과정 을 직관 적 으로 볼 수 있 습 니 다.
로그 프로필 log4j.properties 을 추가 합 니 다.

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Generator FromXML.java 를 실행 할 때 발생 하 는 로그 기록:
DEBUG [main] - Retrieving column information for table "items"
DEBUG [main] - Found column "id", data type 4, in table "mybatis..items"
DEBUG [main] - Found column "name", data type 12, in table "mybatis..items"
DEBUG [main] - Found column "price", data type 7, in table "mybatis..items"
DEBUG [main] - Found column "detail", data type -1, in table "mybatis..items"
DEBUG [main] - Found column "pic", data type 12, in table "mybatis..items"
DEBUG [main] - Found column "createtime", data type 93, in table "mybatis..items"
DEBUG [main] - Retrieving column information for table "orders"
DEBUG [main] - Found column "id", data type 4, in table "mybatis..orders"
DEBUG [main] - Found column "user_id", data type 4, in table "mybatis..orders"
DEBUG [main] - Found column "number", data type 12, in table "mybatis..orders"
DEBUG [main] - Found column "createtime", data type 93, in table "mybatis..orders"
DEBUG [main] - Found column "note", data type 12, in table "mybatis..orders"
DEBUG [main] - Retrieving column information for table "orderdetail"
DEBUG [main] - Found column "id", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Found column "orders_id", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Found column "items_id", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Found column "items_num", data type 4, in table "mybatis..orderdetail"
DEBUG [main] - Retrieving column information for table "user"
DEBUG [main] - Found column "ID", data type 4, in table "mybatis..user"
DEBUG [main] - Found column "USERNAME", data type 12, in table "mybatis..user"
DEBUG [main] - Found column "SEX", data type 12, in table "mybatis..user"
DEBUG [main] - Found column "birthday", data type 91, in table "mybatis..user"
DEBUG [main] - Found column "address", data type 12, in table "mybatis..user"
3.Maven 을 통 해 Mybatis 역방향 프로젝트 완성
1.Maven Project 프로젝트 새로 만 들 기
  Maven 프로젝트 를 새로 만 든 다음 폴 더/my batis-maven/src/main/resources 를 새로 만 들 고 폴 더 아래 에 새 파일 generator Config.xml 을 만 듭 니 다.
Maven 项目结构
2.pom.xml 파일 설정
*81955°pom.xml 파일 을 설정 하고 pom.xml 파일 의 procject 태그 에 코드 를 추가 합 니 다.

<build>
	<plugins>
		<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.38</version>
		  </dependency>	
			</dependencies>
			<configuration>
				<overwrite>true</overwrite>
			</configuration>
		</plugin>
	</plugins>
</build>
플러그 인 generator 버 전 은 1.3.2 이 고 Mysql 드라이브 는 5.1.38 입 니 다.
3.설정 파일 generator Config.xml
  generator Config.xml 는 디 렉 터 리 src 의 main 아래 resources 에 있 습 니 다.여기 targetProject="./src" 에서 생 성 된 파일 도 이 아래 에 있 습 니 다.

<?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="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!--             true:  : false:  -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!--        :   、    、   、   -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@localhost:1521:mybatis" 
			userId=""
			password="">
		</jdbcConnection> -->

		<!--   false, JDBC DECIMAL   NUMERIC       Integer,  true  JDBC DECIMAL   
			NUMERIC      java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:  PO     -->
		<javaModelGenerator targetPackage="com.ssm.po"
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
			<!--                  -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
  <!-- targetProject:mapper          -->
		<sqlMapGenerator targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper        -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.ssm.mapper" 
			targetProject="./src">
			<!-- enableSubPackages:   schema       -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!--        -->
		<!-- 
			tableName:      
  		domainObjectName:       
	  enableCountByExample:Count     where    ,  true  
	  enableUpdateByExample:Update     where    ,  true  
	  enableDeleteByExample:Delete     where    ,  true  
	  enableSelectByExample:Select       where    ,  true  
	  selectByExampleQueryId:Select         where    ,  true  
		 -->
		<table tableName="items">
			<!-- 
				  :
				property:             ,    
				ignoreColumn:         
			 -->
		</table>
		<table tableName="orders"></table>
		<table tableName="orderdetail"></table>
		<table tableName="user"></table>

		
	</context>
</generatorConfiguration>
4.Maven 실행
  운행 명령 mybatis-generator:generate.
  작업 절차:선택 항목 우 클릭=>Run As=>Maven build...=>Goals 에 mybatis-generator:generate=>Run=>새로 고침 프로젝트 를 입력 하 십시오.
生成的代码结构
마 이 바 티 스 역방향 공사 에 관 한 세 가지 방법 에 대한 상세 한 설명 은 여기까지 입 니 다.더 많은 마 이 바 티 스 역방향 공사 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기