1. Mybatis的逆向工程(generator). 由表帮我们来生成dao,bean,xml映射文件。
http://www.mybatis.org/generator/index.html。
- 引入mybatis-generator的jar包
2. 创建generator的配置文件。
3. 运行generator
2. 创建generator的配置文件。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 <!-- generator的配置文件 --> 6 <generatorConfiguration> 7 <!-- mysql驱动jar所在的位置 --> 8 <classPathEntry location="C:\\javaHouse\\mybatis04\\lib\\mysql-connector-java-5.1.47.jar" /> 9 10 <!-- 数据源信息 --> 11 <context id="DB2Tables" targetRuntime="MyBatis3"> 12 <!-- 默认不加注释 --> 13 <commentGenerator> 14 <property name="suppressAllComments" value="true" /> 15 </commentGenerator> 16 17 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 18 connectionURL="jdbc:mysql://localhost:3306/mybatis" 19 userId="root" 20 password="root"> 21 </jdbcConnection> 22 23 <javaTypeResolver > 24 <property name="forceBigDecimals" value="false" /> 25 </javaTypeResolver> 26 27 <!-- 生产的实体类所在的位置 --> 28 <javaModelGenerator targetPackage="com.zhiyou100.wc.bean" targetProject="./src"> 29 <property name="enableSubPackages" value="true" /> 30 <property name="trimStrings" value="true" /> 31 </javaModelGenerator> 32 <!-- 生成的映射文件所在的位置 --> 33 <sqlMapGenerator targetPackage="com.zhiyou100.wc.mapper" targetProject="./resources"> 34 <property name="enableSubPackages" value="true" /> 35 </sqlMapGenerator> 36 <!-- 生成的dao 文件所在的位置 --> 37 <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou100.wc.dao" targetProject="./src"> 38 <property name="enableSubPackages" value="true" /> 39 </javaClientGenerator> 40 41 <!-- 某张表与实体类的对应关系 42 schema:该表所在的数据库 43 tableName:表名 44 domainObjectName:实体类名 45 --> 46 <table schema="mybatis" tableName="users" domainObjectName="Users" 47 enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"> 48 <property name="useActualColumnNames" value="true"/> 49 <generatedKey column="ID" sqlStatement="DB2" identity="true" /> 50 <columnOverride column="DATE_FIELD" property="startDate" /> 51 <ignoreColumn column="FRED" /> 52 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> 53 </table> 54 55 </context> 56 </generatorConfiguration>
3. 运行generator
1 package com.zhiyou100.wc.test; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.mybatis.generator.api.MyBatisGenerator; 8 import org.mybatis.generator.config.Configuration; 9 import org.mybatis.generator.config.xml.ConfigurationParser; 10 import org.mybatis.generator.internal.DefaultShellCallback; 11 //Ctrl+Shift+o 导包的快捷键 12 public class TestGRT { 13 public static void main(String[] args) throws Exception{ 14 List<String> warnings = new ArrayList<String>(); 15 boolean overwrite = true; 16 File configFile = new File("generator.xml"); 17 ConfigurationParser cp = new ConfigurationParser(warnings); 18 Configuration config = cp.parseConfiguration(configFile); 19 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 20 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 21 myBatisGenerator.generate(null); 22 } 23 24 }