idea怎么mybatis自动生成实体类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idea怎么mybatis自动生成实体类相关的知识,希望对你有一定的参考价值。
参考技术A 首先:你最好把你的实体类里面的变量名称和你的表里面字段写成一致。 然后:在SqlMapConfig.xml中 这就是把你的实体类写了个别名 最后:在你写查询添加的时候: 返回你的实体类对象 select * from tb_game 这样查出的结果就对应上你的数据了。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 }
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> IDEA——mybatis-generator插件自动生成实体代码(Maven)使用maven的mybatis-generator代码生成器插件生成实体类mapper配置文件和mapper接口(使用idea)