Mybatis逆向工程(六)

Mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码,提高工作效率。

逆向工程是一个普通的java项目:

 GeneratorSqlmap.java



import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

	public void generator() throws Exception{

		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		//指定 逆向工程配置文件
		File configFile = new File("generator.xml"); 
	//	File configFile = new File("generatorConfig2.xml"); 
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
				callback, warnings);
		myBatisGenerator.generate(null);

	} 
	public static void main(String[] args) throws Exception {
		try {
			GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
			generatorSqlmap.generator();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}

generator.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="mysqlTable" targetRuntime="MyBatis3">
     <commentGenerator>
        <property name="suppressDate" value="true"/>
        <!-- 是否去除自动生成的注释 -->
        <property name="suppressAllComments" value="true" />
    </commentGenerator>
  	<!-- 1.数据连接参数 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/sso-crm"
        userId="root"
        password="密码">
    </jdbcConnection>
     <!-- Oracle数据库
	    <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
	        connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
	        userId="yycg"
	        password="yycg">
	    </jdbcConnection> 
    -->
	<!-- 2.默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
			和 NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
	<!-- 3.生成模型的位置 -->
    <javaModelGenerator targetPackage="com.dsx.model" targetProject=".\src">
      <!-- enableSubPackages:是否让schema作为包的后缀 -->
      <property name="enableSubPackages" value="true" />
        <!-- 从数据库返回的值被清理前后的空格 -->
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
	<!-- 4.targetProject:mapper映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="com.dsx.mapper" targetProject=".\src">
      <!-- enableSubPackages:是否让schema作为包的后缀 -->
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
	<!-- 5. targetPackage:mapper接口生成的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.dsx.mapper"  
   		 targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      
    </javaClientGenerator>
	<!-- 6.要生成的表 -->
	<!--
	table标签下的设置属性useActualColumnNames用于指定生成实体类时是否使用实际的列名作为实体类的属性名
	取值
	true:MyBatis Generator会使用数据库中实际的字段名字作为生成的实体类的属性名。
	false:这是默认值。
	如果设置为false,则MyBatis Generator会将数据库中实际的字段名字转换为Camel Case风格作为生成的实体类的属性名。
	  -->
	<table tableName="sys_user" domainObjectName="User">
		<property name="useActualColumnNames" value="true"/>
	</table>
	<!-- 有些表的字段需要指定java类型 
    <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
      <property name="useActualColumnNames" value="true"/>
      <generatedKey column="ID" sqlStatement="DB2" identity="true" />
      <columnOverride column="DATE_FIELD" property="startDate" />
      <ignoreColumn column="FRED" />
      <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
    </table> -->
	
  </context> 
</generatorConfiguration>

配置文件需要修改的内容:

1. 数据库驱动,地址,用户名,密码

2. pojo类,mapper接口,mapper映射文件生成的位置

3.指定数据库的表

配置完成运行main方法就会生成对应的代码,运行完成记得刷新项目。如果需要再次生成,记得先删除原来生成的再执行main方法。

发布了132 篇原创文章 · 获赞 1 · 访问量 7285

猜你喜欢

转载自blog.csdn.net/duan196_118/article/details/104274733
今日推荐