引用:http://blog.csdn.net/p793049488/article/details/40422975
1、需要手动建表,如T_PRI_USER
2、新建配置文件generator.properties:
- #工程src路径
- project = E:/java/eclipseVillage/icity-yuntv/src
- #工程存放mapper.xml路径
- resource = E:/java/eclipseVillage/icity-yuntv/resource
- #指定数据连接驱动jar地址
- classPath=E:/java/eclipseVillage/icity-yuntv/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.11-bin.jar
- jdbc_driver = com.mysql.jdbc.Driver
- jdbc_url=jdbc:mysql://192.168.22.20:3306/icity_yuntv?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
- jdbc_user=root
- jdbc_password=ffcsadmin
3、新建配置文件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="generator.properties"/>
- <!-- 指定数据连接驱动jar地址 -->
- <classPathEntry location="${classPath}" />
- <!-- 一个数据库一个context -->
- <context id="infoGuardian" targetRuntime="MyBatis3">
- <!-- 注释 -->
- <commentGenerator >
- <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
- <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
- </commentGenerator>
- <!-- jdbc连接 -->
- <jdbcConnection driverClass="${jdbc_driver}"
- connectionURL="${jdbc_url}" userId="${jdbc_user}"
- password="${jdbc_password}" />
- <!-- 类型转换 -->
- <javaTypeResolver>
- <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成实体类地址 -->
- <javaModelGenerator targetPackage="cn.ffcs.yuntv.domain"
- targetProject="${project}" >
- <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->
- <property name="enableSubPackages" value="false"/>
- <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成mapxml文件 -->
- <sqlMapGenerator targetPackage="mybatis"
- targetProject="${resource}" >
- <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->
- <property name="enableSubPackages" value="false" />
- </sqlMapGenerator>
- <!-- 生成mapxml对应client,也就是接口dao -->
- <javaClientGenerator targetPackage="cn.ffcs.yuntv.dao"
- targetProject="${project}" type="XMLMAPPER" >
- <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->
- <property name="enableSubPackages" value="false" />
- </javaClientGenerator>
- <!-- 配置表信息,这里没生成一张表,这里需要改变一次对应表名 -->
- <table schema="icity_yuntv" tableName="T_USER_ADDR"
- domainObjectName="UserAddr" enableCountByExample="false"
- enableDeleteByExample="false" enableSelectByExample="false"
- enableUpdateByExample="false">
- <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
- 是否生成 example类 -->
- <!-- 忽略列,不生成bean 字段
- <ignoreColumn column="FRED" />-->
- <!-- 指定列的java数据类型
- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
- </table>
- </context>
- </generatorConfiguration>
4、新建主类:
- import java.io.File;
- import java.io.IOException;
- 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.exception.XMLParserException;
- import org.mybatis.generator.internal.DefaultShellCallback;
- public class GeneratorMain {
- public static void main(String[] args) {
- List<String> warnings = new ArrayList<String>();
- boolean overwrite = true;
- //读取配置文件
- File configFile = new File("src/generatorConfig.xml");
- ConfigurationParser cp = new ConfigurationParser(warnings);
- Configuration config;
- try {
- config = cp.parseConfiguration(configFile);
- DefaultShellCallback callback = new DefaultShellCallback(overwrite);
- MyBatisGenerator myBatisGenerator;
- try {
- myBatisGenerator = new MyBatisGenerator(config, callback,
- warnings);
- myBatisGenerator.generate(null);
- //打印结果
- for(String str : warnings){
- System.out.println(str);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (XMLParserException e) {
- e.printStackTrace();
- }
- }
- }
5、导入jar包:
MySQL-connector-Java-5.1.11-bin.jar.jar 下载地址;http://download.csdn.net/detail/p793049488/8087305