zoukankan      html  css  js  c++  java
  • Mybatis代码自动生成

    Mybatis代码自动生成

    一、修改generatorConfig.xml文件

    • 修改本地数据库的:url,用户名,密码,要生成代码的table表格
    <?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>    
    <!-- 数据库驱动-->    
        <classPathEntry  location="mysql-connector-java-5.1.34.jar"/>    
        <context id="DB2Tables"  targetRuntime="MyBatis3">    
            <commentGenerator>    
                <property name="suppressDate" value="true"/>     
                <property name="suppressAllComments" value="true"/>    
            </commentGenerator>    
            <!--数据库链接URL,用户名、密码 -->    
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/first_test" userId="root" password="123456">    
            </jdbcConnection>    
            <javaTypeResolver>    
                <property name="forceBigDecimals" value="false"/>    
            </javaTypeResolver>    
            <!-- 生成模型的包名和位置-->    
            <javaModelGenerator targetPackage="自动生成代码.pojo" targetProject="./">    
                <property name="enableSubPackages" value="true"/>    
                <property name="trimStrings" value="true"/>    
            </javaModelGenerator>    
            <!-- 生成映射文件的包名和位置-->    
            <sqlMapGenerator targetPackage="自动生成代码.mapper" targetProject="./">    
                <property name="enableSubPackages" value="true"/>    
            </sqlMapGenerator>    
            <!-- 生成DAO的包名和位置-->    
            <javaClientGenerator type="XMLMAPPER" targetPackage="自动生成代码.mapperInterface" targetProject="./">    
                <property name="enableSubPackages" value="true"/>    
            </javaClientGenerator>    
            <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->    
            <table tableName="user_table" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
        </context>    
    </generatorConfiguration>
    

    二、双击start.bat运行

    • 运行成功,code目录下便是生成的代码

    avatar

    三、运行结果

    • 这是我的数据库

      avatat

    • 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="code.mapperInterface.UserMapper">
        <resultMap id="BaseResultMap" type="code.pojo.User">
          <id column="id" jdbcType="INTEGER" property="id" />
          <result column="email" jdbcType="VARCHAR" property="email" />
          <result column="password" jdbcType="VARCHAR" property="password" />
          <result column="perm" jdbcType="VARCHAR" property="perm" />
        </resultMap>
        <sql id="Base_Column_List">
          id, email, password, perm
        </sql>
        <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
          select 
          <include refid="Base_Column_List" />
          from user_table
          where id = #{id,jdbcType=INTEGER}
        </select>
        <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
          delete from user_table
          where id = #{id,jdbcType=INTEGER}
        </delete>
        <insert id="insert" parameterType="code.pojo.User">
          insert into user_table (id, email, password, 
            perm)
          values (#{id,jdbcType=INTEGER}, #{email,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
            #{perm,jdbcType=VARCHAR})
        </insert>
        <insert id="insertSelective" parameterType="code.pojo.User">
          insert into user_table
          <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
              id,
            </if>
            <if test="email != null">
              email,
            </if>
            <if test="password != null">
              password,
            </if>
            <if test="perm != null">
              perm,
            </if>
          </trim>
          <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
              #{id,jdbcType=INTEGER},
            </if>
            <if test="email != null">
              #{email,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
              #{password,jdbcType=VARCHAR},
            </if>
            <if test="perm != null">
              #{perm,jdbcType=VARCHAR},
            </if>
          </trim>
        </insert>
        <update id="updateByPrimaryKeySelective" parameterType="code.pojo.User">
          update user_table
          <set>
            <if test="email != null">
              email = #{email,jdbcType=VARCHAR},
            </if>
            <if test="password != null">
              password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="perm != null">
              perm = #{perm,jdbcType=VARCHAR},
            </if>
          </set>
          where id = #{id,jdbcType=INTEGER}
        </update>
        <update id="updateByPrimaryKey" parameterType="code.pojo.User">
          update user_table
          set email = #{email,jdbcType=VARCHAR},
            password = #{password,jdbcType=VARCHAR},
            perm = #{perm,jdbcType=VARCHAR}
          where id = #{id,jdbcType=INTEGER}
        </update>
      </mapper>
      
    • mapperInterface

      package code.mapperInterface;
      
      import code.pojo.User;
      
      public interface UserMapper {
          int deleteByPrimaryKey(Integer id);
      
          int insert(User record);
      
          int insertSelective(User record);
      
          User selectByPrimaryKey(Integer id);
      
          int updateByPrimaryKeySelective(User record);
      
          int updateByPrimaryKey(User record);
      }
      
    • pojo

      package code.pojo;
      
      public class User {
          private Integer id;
      
          private String email;
      
          private String password;
      
          private String perm;
      
          public Integer getId() {
              return id;
          }
      
          public void setId(Integer id) {
              this.id = id;
          }
      
          public String getEmail() {
              return email;
          }
      
          public void setEmail(String email) {
              this.email = email == null ? null : email.trim();
          }
      
          public String getPassword() {
              return password;
          }
      
          public void setPassword(String password) {
              this.password = password == null ? null : password.trim();
          }
      
          public String getPerm() {
              return perm;
          }
      
          public void setPerm(String perm) {
              this.perm = perm == null ? null : perm.trim();
          }
      }
      
  • 相关阅读:
    Mongodb
    Java原子类
    volatile
    uniapp输入空格
    看不见的的html
    小程序隐藏scroll-view滚动条的方法
    云函数调用云函数 openid不存在
    vue路由中 Navigating to current location ("/xxx") is not allowed
    Vue: 单页面应用如何保持登录状态
    letter-spacing
  • 原文地址:https://www.cnblogs.com/yizhixiang/p/12809808.html
Copyright © 2011-2022 走看看