天天看點

MyBatis根據資料庫的表生成dao層資料

文章目錄

      • generatorConfig.xml檔案
      • db.propertis在classpath下
      • MBGenerator.java生成類

generatorConfig.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>
	<properties resource="db.properties" />
	<!--資料庫驅動 -->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--資料庫連結位址賬号密碼 -->
		<jdbcConnection driverClass="${jdbc.driver}"
			connectionURL="${jdbc.url}" userId="${jdbc.username}"
			password="${jdbc.password}">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!--生成Model類存放位置 -->
		<javaModelGenerator
			targetPackage="cn.edu.ujn.hlq001.dao" targetProject="src\main\java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!--生成映射檔案存放位置 -->
		<sqlMapGenerator targetPackage="cn.edu.ujn.hlq001.dao"
			targetProject="src\main\java">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!--生成Dao類存放位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.edu.ujn.hlq001.dao" targetProject="src\main\java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!--生成對應表及類名 -->
		<table tableName="info_teacher" domainObjectName="InfoTeacher"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
		

	</context>
</generatorConfiguration>
           
MyBatis根據資料庫的表生成dao層資料

db.propertis在classpath下

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3316/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123123
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
           

MBGenerator.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 MBGenerator {

	public static void main(String[] args) {
		try {
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			String cfile = "/generatorConfig.xml";
			File configFile = new File(MBGenerator.class.getResource(cfile).getFile());
			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);
			System.out.println("生成結束!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}