Use mybatis-generator generates the bottom

 

Foreword

 

 

 

 

I heard the name mybatis-generator of a long time ago, but nonetheless the middle of several attempts have ended in failure, and finally gave up trying to continue to hand rub the bottom. Recently finally brought power, bite the bullet and overcome the difficulties, then on hand to bid farewell to rub the bottom of hell.

Using springboot2, jdk1.8, idea.

 

First, the introduction of its dependencies 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>

 

note:

mybatis-generator and mysql version if the gap is too large, it may cause an error in the code generation process

 

Second, the configuration file in Resource Configuration in

 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 is              <-! Whether to remove the automatically generated annotations true: is: to false: NO -> 
13 is              < Property name = "suppressAllComments" value = "true" /> 
14          </ commentGenerator > 
15  16 <-! JDBC link information -> . 17 < JdbcConnection driverClass = "com.mysql.jdbc.Driver" 18 is                         the 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         </ 45>Javaclient generator
 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>

 

note:

1. Store the generated code specified package or folder can not advance built, as long as the right path built along the way plug-in

2. Depending on the specified path section generates code may need to be changed. Such as mapping configuration file,

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

namespace is generated based on the above configuration is generated, if you want to move mapper After generating the code, remember to modify the path

Third, the generated code

 

 

Open Maven menu, click Plugins in the mybatis-generator, can be generated automatically (if not wrong, then)

 

Fourth, the directory structure

 

Fifth, the generated code

 

  1, resulting po, get and set methods have been written

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);

}

 

   

 

 

Guess you like

Origin www.cnblogs.com/Createsequence/p/11228448.html