天天看點

MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)

MyBatis屬于一種半自動的ORM架構,它需要程式員自己編寫sql語句和映射檔案,但是編寫映射檔案和sql語句很容易出錯,是以mybatis官方提供了Generator生成器,自動生成DAO接口。實體類和Mapping。這個生成器是根據單表自動生成mybatis執行所需要的代碼,是以,首先得先建立資料庫表,然後再自動生成代碼。

1.建立user資料表

create table `user`(
    id int(11) not null AUTO_INCREMENT,
    userName varchar(40) not null,
    password varchar(255) not null,
    age int(4) not null,
    primary key(`id`)
)DEFAULT CHARSET=utf8;  
           

2.通過代碼生成器自動生成代碼

代碼生成器的下載下傳位址為:http://download.csdn.net/download/u013216156/10134751)

解壓代碼生成器,打開它的lib目錄,如下所示:

MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)

lib目錄下包含了代碼生成器生成代碼所需要的mybatis和mysql的jar包,而我們所要做的就是修改generatorConfig.xml檔案,如下所示:

<?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.25-bin.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/ssm" userId="root" password="hello">    
        </jdbcConnection>    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false"/>    
        </javaTypeResolver>    
        <!-- 生成模型的包名和位置-->    
        <javaModelGenerator targetPackage="com.spenglu.pojo" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
            <property name="trimStrings" value="true"/>    
        </javaModelGenerator>    
        <!-- 生成映射檔案的包名和位置-->    
        <sqlMapGenerator targetPackage="com.spenglu.mapping" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
        </sqlMapGenerator>    
        <!-- 生成DAO的包名和位置-->    
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.spenglu.IDao" targetProject="src">    
            <property name="enableSubPackages" value="true"/>    
        </javaClientGenerator>    
        <!-- 要生成的表 tableName是資料庫中的表名或視圖名 domainObjectName是實體類名-->    
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
    </context>    
</generatorConfiguration>
           

隻需要把上面檔案注釋下面的内容修改為自己工程相關的内容就可以了.

檔案修改完以後,需要通過控制台來執行腳本生成代碼。

  • 打開控制台,進入lib目錄下,如下圖所示:
    MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)
    先通過”E:”指令進入E盤,然後通過”cd +lib檔案路徑”進入lib目錄下
  • 執行腳本java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

    自動生成代碼如下圖所示:

    MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)
    執行成功以後會在你自己定義的檔案目錄下生成相關的代碼,如下圖所示:
    MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)
    package com.spenglu.IDao;
    import com.spenglu.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);
    }
               
MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)
<?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.spenglu.IDao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.spenglu.pojo.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="userName" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, userName, password, age
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.spenglu.pojo.User" >
    insert into user (id, userName, password,
      age)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
      #{age,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.spenglu.pojo.User" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="username != null" >
        userName,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="age != null" >
        age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.spenglu.pojo.User" >
    update user
    <set >
      <if test="username != null" >
        userName = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.spenglu.pojo.User" >
    update user
    set userName = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>
           
MyBatis自動生成實體類、DAO接口和Mapping映射檔案的代碼(逆向工程)
package com.spenglu.pojo;
public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}