利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件。
mysql-connector-java-5.1.6-bin.jar mysql驱动包
mybatis-generator-core-1.3.5.jar 自动生成器包
maven 配置mybatis-generator插件
一、pom.xml 两处配置
(1)
(2)
二、创建 generatorConfig.xml
配置如下:
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
6 <generatorConfiguration>
7 <!--导入属性配置-->
8 <properties resource="datasource.properties"></properties>
9
10 <!--指定特定数据库的jdbc驱动jar包的位置-->
11 <classPathEntry location="${db.driverLocation}"/>
12
13 <context id="default" targetRuntime="MyBatis3">
14
15 <!-- optional,旨在创建class时,对注释进行控制 -->
16 <commentGenerator>
17 <property name="suppressDate" value="true"/>
18 <!-- 是否去除自动生成的注释 true:是 : false:否 -->
19 <property name="suppressAllComments" value="true"/>
20 </commentGenerator>
21
22 <!--jdbc的数据库连接 -->
23 <jdbcConnection
24 driverClass="${db.driverClassName}"
25 connectionURL="${db.url}"
26 userId="${db.username}"
27 password="${db.password}">
28 </jdbcConnection>
29
30
31 <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
32 <javaTypeResolver>
33 <property name="forceBigDecimals" value="false"/>
34 </javaTypeResolver>
35
36 <!-- 生成模型的包名和位置-->
37 <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
38 targetPackage 指定生成的model生成所在的包名
39 targetProject 指定在该项目下所在的路径
40 -->
41 <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
42 <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
43 <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
44 <property name="enableSubPackages" value="false"/>
45 <!-- 是否对model添加 构造函数 -->
46 <property name="constructorBased" value="true"/>
47 <!-- 是否对类CHAR类型的列的数据进行trim操作 (去空)-->
48 <property name="trimStrings" value="true"/>
49 <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
50 <property name="immutable" value="false"/>
51 </javaModelGenerator>
52
53 <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
54 <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
55 <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
56 <property name="enableSubPackages" value="false"/>
57 </sqlMapGenerator>
58
59 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
60 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
61 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
62 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
63 -->
64
65 <!-- targetPackage:mapper接口dao生成的位置 -->
66 <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
67 <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
68 <!-- enableSubPackages:是否让schema作为包的后缀 -->
69 <property name="enableSubPackages" value="false" />
70 </javaClientGenerator>
71
72 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
73 <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
74 <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
75 <!-- 数据库中该字段的类型是 txt ,不同版本生成对应字体类的属性类型可能不同,因此指定转换类型 -->
76 <columnOverride column="detail" jdbcType="VARCHAR" />
77 <columnOverride column="sub_images" jdbcType="VARCHAR" />
78 </table>
79 <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
80
81 <!-- mybatis插件的搭建 -->
82 </context>
83 </generatorConfiguration>
属性配置文件如下:
#MySQL的JDBC驱动包,用JDBC连接MySQL数据库时必须使用该jar包
db.driverLocation=mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver
#db.url=jdbc:mysql://localhost:3306/mmall?characterEncoding=utf-8
db.url=jdbc:mysql://localhost:3306/mmall?characterEncoding=utf-8
db.username=root
db.password=123456
三、当前项目结构
备注:双击 maven 配置的 mybatis-generator 插件时,当前路径为 pom.xml 的路径,
此时 mysql-connector-java-5.1.6-bin.jar mysql驱动包与 pom.xml 在同一路径。
路径配置错误会报错:
[ERROR]:Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project Mybatis-Generator: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Cannot resolve classpath entry: mysql-connector-java-5.1.6-bin.jar -> [Help 1]
双击运行后自动生成的代码结果:
1 package com.mmall.pojo; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; 7 8 private String username; 9 10 private String password; 11 12 private String email; 13 14 private String phone; 15 16 private String question; 17 18 private String answer; 19 20 private Integer role; 21 22 private Date createTime; 23 24 private Date updateTime; 25 26 public User(Integer id, String username, String password, String email, String phone, String question, String answer, Integer role, Date createTime, Date updateTime) { 27 this.id = id; 28 this.username = username; 29 this.password = password; 30 this.email = email; 31 this.phone = phone; 32 this.question = question; 33 this.answer = answer; 34 this.role = role; 35 this.createTime = createTime; 36 this.updateTime = updateTime; 37 } 38 39 public User() { 40 super(); 41 } 42 43 public Integer getId() { 44 return id; 45 } 46 47 public void setId(Integer id) { 48 this.id = id; 49 } 50 51 public String getUsername() { 52 return username; 53 } 54 55 public void setUsername(String username) { 56 this.username = username == null ? null : username.trim(); 57 } 58 59 public String getPassword() { 60 return password; 61 } 62 63 public void setPassword(String password) { 64 this.password = password == null ? null : password.trim(); 65 } 66 67 public String getEmail() { 68 return email; 69 } 70 71 public void setEmail(String email) { 72 this.email = email == null ? null : email.trim(); 73 } 74 75 public String getPhone() { 76 return phone; 77 } 78 79 public void setPhone(String phone) { 80 this.phone = phone == null ? null : phone.trim(); 81 } 82 83 public String getQuestion() { 84 return question; 85 } 86 87 public void setQuestion(String question) { 88 this.question = question == null ? null : question.trim(); 89 } 90 91 public String getAnswer() { 92 return answer; 93 } 94 95 public void setAnswer(String answer) { 96 this.answer = answer == null ? null : answer.trim(); 97 } 98 99 public Integer getRole() { 100 return role; 101 } 102 103 public void setRole(Integer role) { 104 this.role = role; 105 } 106 107 public Date getCreateTime() { 108 return createTime; 109 } 110 111 public void setCreateTime(Date createTime) { 112 this.createTime = createTime; 113 } 114 115 public Date getUpdateTime() { 116 return updateTime; 117 } 118 119 public void setUpdateTime(Date updateTime) { 120 this.updateTime = updateTime; 121 } 122 }