使用mybatis-generator生成底层

Posted createsequence

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用mybatis-generator生成底层相关的知识,希望对你有一定的参考价值。

 

前言

 

 

 

 

很久以前便听过mybatis-generator的大名,但是奈何中间几次尝试都以失败告终,最后放弃努力继续手搓底层。最近终于又提起动力,硬着头皮攻克了难关,从此以后告别手搓底层的地狱。

使用的是springboot2,jdk1.8,idea。

 

一、在pom引入相关依赖

<!--mybatise-generator-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--在此处指定配置文件位置-->
                    <configurationFile>src/main/resources/generatorConfig/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <!--mybatise-generator-->
                <dependencies>
                <!--在此处引入所需依赖-->
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.44</version>
                    </dependency>
                </dependencies>
            </plugin>

 

注意:

mybatis-generator版本如果和mysql差距過大,可能在生成代碼過程中引起報錯

 

二、在Resource中配置配置配置文件

 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     <!--注意这里的targetRuntime="MyBatis3Simple",指定了不生成Example相关内容-->
 8     <context id="MysqlTables" targetRuntime="MyBatis3Simple">
 9 ?
10         <commentGenerator>
11             <property name="suppressDate" value="true"/>
12             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
13             <property name="suppressAllComments" value="true"/>
14         </commentGenerator>
15 ?
16         <!-- jdbc链接信息 -->
17         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
18                         connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"
19                         userId="root" password="123456">
20         </jdbcConnection>
21 ?
22         <javaTypeResolver>
23             <property name="forceBigDecimals" value="false"/>
24         </javaTypeResolver>
25 ?
26         <!-- 生成PO类的位置 -->
27         <javaModelGenerator targetPackage="com.huang.po"
28                             targetProject="src/main/java">
29             <property name="enableSubPackages" value="true"/>
30             <property name="trimStrings" value="true"/>
31         </javaModelGenerator>
32 ?
33         <!-- mapper映射文件生成的位置 -->
34         <sqlMapGenerator targetPackage="mapper"
35                          targetProject="src/main/resources">
36             <property name="enableSubPackages" value="true"/>
37         </sqlMapGenerator>
38 ?
39         <!-- mapper接口生成的位置 -->
40         <javaClientGenerator type="XMLMAPPER"
41                              targetPackage="com.party.community.template"
42                              targetProject="src/main/java">
43             <property name="enableSubPackages" value="true"/>
44         </javaClientGenerator>
45 ?
46         <!-- 指定要生成的表,主鍵,po類名 -->
47         <table tableName="User" domainObjectName="User">
48             <property name="useActualColumnNames" value="true"/>
49             <generatedKey column="u_id" sqlStatement="MySql" identity="true"/>
50         </table>
51 ?
52     </context>
53 </generatorConfiguration>

 

注意:

1.存放生成代碼的指定包或者文件夾可以不用提前建好,只要路徑沒錯插件會順便一起建好

2.根據指定路徑的不同,部分生成代碼可能會需要更改。比如映射配置文件,

<mapper namespace="com.party.community.mapper.User_noticeMapper" >

namespace的生成是根據上面的配置而生成的,如果你在生成代碼後還想要移動mapper的話,要記得修改 路徑

三、生成代碼

技术图片

 

技术图片

 

打開Maven菜單,點擊Plugins裡的mybatis-generator,即可自動生成(如果沒出問題的話)

 

四、目錄結構

技术图片

 技术图片

五、生成的代碼

 

  1、生成的po,已写好get和set方法

package com.party.community.po;

import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class User 
    private String u_idcard;

    private String u_unsername;

    private String u_tel;

    private String u_pwd;

    private String u_avator;

    private String u_sex;

    private Date u_birthday;

    private String u_history;

    private String u_remark;

    public String getU_idcard() 
        return u_idcard;
    

    public void setU_idcard(String u_idcard) 
        this.u_idcard = u_idcard == null ? null : u_idcard.trim();
    

    public String getU_unsername() 
        return u_unsername;
    

    public void setU_unsername(String u_unsername) 
        this.u_unsername = u_unsername == null ? null : u_unsername.trim();
    

    public String getU_tel() 
        return u_tel;
    

    public void setU_tel(String u_tel) 
        this.u_tel = u_tel == null ? null : u_tel.trim();
    

    public String getU_pwd() 
        return u_pwd;
    

    public void setU_pwd(String u_pwd) 
        this.u_pwd = u_pwd == null ? null : u_pwd.trim();
    

    public String getU_avator() 
        return u_avator;
    

    public void setU_avator(String u_avator) 
        this.u_avator = u_avator == null ? null : u_avator.trim();
    

    public String getU_sex() 
        return u_sex;
    

    public void setU_sex(String u_sex) 
        this.u_sex = u_sex == null ? null : u_sex.trim();
    

    public Date getU_birthday() 
        return u_birthday;
    

    public void setU_birthday(Date u_birthday) 
        this.u_birthday = u_birthday;
    

    public String getU_history() 
        return u_history;
    

    public void setU_history(String u_history) 
        this.u_history = u_history == null ? null : u_history.trim();
    

    public String getU_remark() 
        return u_remark;
    

    public void setU_remark(String u_remark) 
        this.u_remark = u_remark == null ? null : u_remark.trim();
    

  

 

  2、映射文件和mapper,已经生成了基本的增删改查方法

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.party.community.mapper.UserMapper" >
  <resultMap id="BaseResultMap" type="com.party.community.po.User" >
    <id column="u_idcard" property="u_idcard" jdbcType="VARCHAR" />
    <result column="u_unsername" property="u_unsername" jdbcType="VARCHAR" />
    <result column="u_tel" property="u_tel" jdbcType="VARCHAR" />
    <result column="u_pwd" property="u_pwd" jdbcType="VARCHAR" />
    <result column="u_avator" property="u_avator" jdbcType="VARCHAR" />
    <result column="u_sex" property="u_sex" jdbcType="VARCHAR" />
    <result column="u_birthday" property="u_birthday" jdbcType="TIMESTAMP" />
    <result column="u_history" property="u_history" jdbcType="VARCHAR" />
    <result column="u_remark" property="u_remark" jdbcType="VARCHAR" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from user
    where u_idcard = #u_idcard,jdbcType=VARCHAR
  </delete>
  <insert id="insert" parameterType="com.party.community.po.User" >
    <selectKey resultType="java.lang.String" keyProperty="u_idcard" order="AFTER" >
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into user (u_unsername, u_tel, u_pwd, 
      u_avator, u_sex, u_birthday, 
      u_history, u_remark)
    values (#u_unsername,jdbcType=VARCHAR, #u_tel,jdbcType=VARCHAR, #u_pwd,jdbcType=VARCHAR, 
      #u_avator,jdbcType=VARCHAR, #u_sex,jdbcType=VARCHAR, #u_birthday,jdbcType=TIMESTAMP, 
      #u_history,jdbcType=VARCHAR, #u_remark,jdbcType=VARCHAR)
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.party.community.po.User" >
    update user
    set u_unsername = #u_unsername,jdbcType=VARCHAR,
      u_tel = #u_tel,jdbcType=VARCHAR,
      u_pwd = #u_pwd,jdbcType=VARCHAR,
      u_avator = #u_avator,jdbcType=VARCHAR,
      u_sex = #u_sex,jdbcType=VARCHAR,
      u_birthday = #u_birthday,jdbcType=TIMESTAMP,
      u_history = #u_history,jdbcType=VARCHAR,
      u_remark = #u_remark,jdbcType=VARCHAR
    where u_idcard = #u_idcard,jdbcType=VARCHAR
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select u_idcard, u_unsername, u_tel, u_pwd, u_avator, u_sex, u_birthday, u_history, 
    u_remark
    from user
    where u_idcard = #u_idcard,jdbcType=VARCHAR
  </select>
  <select id="selectAll" resultMap="BaseResultMap" >
    select u_idcard, u_unsername, u_tel, u_pwd, u_avator, u_sex, u_birthday, u_history, 
    u_remark
    from user
  </select>
  <select id="selectByPhone" resultType="User">
    select *
    from user
    where u_tel = #phone
  </select>


</mapper>

 

package com.party.community.mapper;

import com.party.community.po.User;
import java.util.List;

public interface UserMapper 
    int deleteByPrimaryKey(String u_idcard);

    int insert(User record);

    User selectByPrimaryKey(String u_idcard);

    List<User> selectAll();

    int updateByPrimaryKey(User record);

    User selectByPhone(String phone);

 

   

 

技术图片

 

以上是关于使用mybatis-generator生成底层的主要内容,如果未能解决你的问题,请参考以下文章

使用mybatis-generator插件生成mapper类

使用Mybatis-Generator自动生成DaoModelMapping

使用Mybatis-Generator自动生成DaoModelMapping相关文件

如何利用mybatis-generator生成配置文件

使用Mybatis-Generator自动生成DaoModelMapping相关文件(转)

使用Mybatis-Generator自动生成DaoModelMapping相关文件(转)