zoukankan      html  css  js  c++  java
  • 使用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中配置配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
            PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
            "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    
    <generatorConfiguration>
        <!--注意这里的targetRuntime="MyBatis3Simple",指定了不生成Example相关内容-->
        <context id="MysqlTables" targetRuntime="MyBatis3Simple">
    
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
    
            <!-- jdbc链接信息 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"
                            userId="root" password="123456">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
            <!-- 生成PO类的位置 -->
            <javaModelGenerator targetPackage="com.huang.po"
                                targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
    
            <!-- mapper映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="mapper"
                             targetProject="src/main/resources">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
    
            <!-- mapper接口生成的位置 -->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.party.community.template"
                                 targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
    
            <!-- 指定要生成的表,主鍵,po类名 -->
            <table tableName="Admins" domainObjectName="Admins">
                <property name="useActualColumnNames" value="true"/>
                <generatedKey column="a_id" sqlStatement="MySql" identity="true"/>
            </table>
    
        </context>
    </generatorConfiguration>
    

    注意:

    ​ 1.存放生成代码的指定包或者文件夹可以不用提前建好,只要路径没错插件会顺便一起建好

    ​ 2.根指定路径的不同,部分生成代码可能会需要更改。比如映射配置文件,

    ​ namespace的生成是根据上面的配置而生成的,如果你在生成代码后还想要移动mapper的話,要记得修改对应路径

    生成代码

    代开Maven菜单,点击Plugins里的mybatis-generator,即可自动生成

    目录结构

    生成的代码

    java代码

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

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

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

    xml映射文件

    <?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>
    

    生成的mapper和映射文件,已经生成了基本的增删改查和根据主键查找的方法

  • 相关阅读:
    第一次个人编程作业
    第一次博客作业
    Put-Me-Down项目Postmortem
    Alpha总结
    git分支与版本管理、版本回退、冲突解决记录
    【Alpha】Daily Scrum Meeting第五次
    【Alpha】Daily Scrum Meeting第四次
    【Alpha】Daily Scrum Meeting第三次
    【Alpha】Daily Scrum Meeting第二次
    一、Daily Scrum Meeting【Alpha】------Clover
  • 原文地址:https://www.cnblogs.com/Createsequence/p/11336549.html
Copyright © 2011-2022 走看看