IDEA——mybatis-generator插件自动生成实体代码(Maven)

利用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 }
User
  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 <mapper namespace="com.mmall.dao.UserMapper" >
  4   <resultMap id="BaseResultMap" type="com.mmall.pojo.User" >
  5     <constructor >
  6       <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
  7       <arg column="username" jdbcType="VARCHAR" javaType="java.lang.String" />
  8       <arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" />
  9       <arg column="email" jdbcType="VARCHAR" javaType="java.lang.String" />
 10       <arg column="phone" jdbcType="VARCHAR" javaType="java.lang.String" />
 11       <arg column="question" jdbcType="VARCHAR" javaType="java.lang.String" />
 12       <arg column="answer" jdbcType="VARCHAR" javaType="java.lang.String" />
 13       <arg column="role" jdbcType="INTEGER" javaType="java.lang.Integer" />
 14       <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
 15       <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
 16     </constructor>
 17   </resultMap>
 18   <sql id="Base_Column_List" >
 19     id, username, password, email, phone, question, answer, role, create_time, update_time
 20   </sql>
 21   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
 22     select 
 23     <include refid="Base_Column_List" />
 24     from mmall_user
 25     where id = #{id,jdbcType=INTEGER}
 26   </select>
 27   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
 28     delete from mmall_user
 29     where id = #{id,jdbcType=INTEGER}
 30   </delete>
 31   <insert id="insert" parameterType="com.mmall.pojo.User" >
 32     insert into mmall_user (id, username, password, 
 33       email, phone, question, 
 34       answer, role, create_time, 
 35       update_time)
 36     values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
 37       #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR}, 
 38       #{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, 
 39       #{updateTime,jdbcType=TIMESTAMP})
 40   </insert>
 41   <insert id="insertSelective" parameterType="com.mmall.pojo.User" >
 42     insert into mmall_user
 43     <trim prefix="(" suffix=")" suffixOverrides="," >
 44       <if test="id != null" >
 45         id,
 46       </if>
 47       <if test="username != null" >
 48         username,
 49       </if>
 50       <if test="password != null" >
 51         password,
 52       </if>
 53       <if test="email != null" >
 54         email,
 55       </if>
 56       <if test="phone != null" >
 57         phone,
 58       </if>
 59       <if test="question != null" >
 60         question,
 61       </if>
 62       <if test="answer != null" >
 63         answer,
 64       </if>
 65       <if test="role != null" >
 66         role,
 67       </if>
 68       <if test="createTime != null" >
 69         create_time,
 70       </if>
 71       <if test="updateTime != null" >
 72         update_time,
 73       </if>
 74     </trim>
 75     <trim prefix="values (" suffix=")" suffixOverrides="," >
 76       <if test="id != null" >
 77         #{id,jdbcType=INTEGER},
 78       </if>
 79       <if test="username != null" >
 80         #{username,jdbcType=VARCHAR},
 81       </if>
 82       <if test="password != null" >
 83         #{password,jdbcType=VARCHAR},
 84       </if>
 85       <if test="email != null" >
 86         #{email,jdbcType=VARCHAR},
 87       </if>
 88       <if test="phone != null" >
 89         #{phone,jdbcType=VARCHAR},
 90       </if>
 91       <if test="question != null" >
 92         #{question,jdbcType=VARCHAR},
 93       </if>
 94       <if test="answer != null" >
 95         #{answer,jdbcType=VARCHAR},
 96       </if>
 97       <if test="role != null" >
 98         #{role,jdbcType=INTEGER},
 99       </if>
100       <if test="createTime != null" >
101         #{createTime,jdbcType=TIMESTAMP},
102       </if>
103       <if test="updateTime != null" >
104         #{updateTime,jdbcType=TIMESTAMP},
105       </if>
106     </trim>
107   </insert>
108   <update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.User" >
109     update mmall_user
110     <set >
111       <if test="username != null" >
112         username = #{username,jdbcType=VARCHAR},
113       </if>
114       <if test="password != null" >
115         password = #{password,jdbcType=VARCHAR},
116       </if>
117       <if test="email != null" >
118         email = #{email,jdbcType=VARCHAR},
119       </if>
120       <if test="phone != null" >
121         phone = #{phone,jdbcType=VARCHAR},
122       </if>
123       <if test="question != null" >
124         question = #{question,jdbcType=VARCHAR},
125       </if>
126       <if test="answer != null" >
127         answer = #{answer,jdbcType=VARCHAR},
128       </if>
129       <if test="role != null" >
130         role = #{role,jdbcType=INTEGER},
131       </if>
132       <if test="createTime != null" >
133         create_time = #{createTime,jdbcType=TIMESTAMP},
134       </if>
135       <if test="updateTime != null" >
136         update_time = #{updateTime,jdbcType=TIMESTAMP},
137       </if>
138     </set>
139     where id = #{id,jdbcType=INTEGER}
140   </update>
141   <update id="updateByPrimaryKey" parameterType="com.mmall.pojo.User" >
142     update mmall_user
143     set username = #{username,jdbcType=VARCHAR},
144       password = #{password,jdbcType=VARCHAR},
145       email = #{email,jdbcType=VARCHAR},
146       phone = #{phone,jdbcType=VARCHAR},
147       question = #{question,jdbcType=VARCHAR},
148       answer = #{answer,jdbcType=VARCHAR},
149       role = #{role,jdbcType=INTEGER},
150       create_time = #{createTime,jdbcType=TIMESTAMP},
151       update_time = #{updateTime,jdbcType=TIMESTAMP}
152     where id = #{id,jdbcType=INTEGER}
153   </update>
154 </mapper>
UserMapper.xml

五、换一种路径进行自动生成代码

 

 

posted @ 2018-02-23 00:51  TigerExpensive  阅读(12903)  评论(0编辑  收藏  举报