Mybatis-Generator 的使用

簡介:Mybatis-Generator是一個數據庫逆向框架代碼生成工具,利用該工具能夠自動生成Dao,Model,Mapping。java

1. 準備工做:mysql

  step1: 下載Mybatis-Generatorspring

  step2: 數據庫(本例爲mysql)添加表sql

DROP TABLE IF EXISTS `springmvc_user`; CREATE TABLE `springmvc_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(11) NOT NULL, `password` varchar(50) NOT NULL, `age` int(11) NOT NULL, `email` varchar(12) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  step3: 下載mysql鏈接驅動包(本例爲mysql-connector-java-5.1.18-bin.jar)數據庫

 

2. 正式操做mybatis

  step1:打開配置文件generator.xml,修改註釋部份內容mvc

<?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="E:\ZHY\app\generator\mysql-connector-java-5.1.18-bin.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 數據庫連接URL、用戶名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/zhy" userId="root" password="">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="zhy.model" targetProject="E:\ZHY\app\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="zhy.mapping" targetProject="E:\ZHY\app\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="zhy.dao" targetProject="E:\ZHY\app\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就能夠) -->
        <table tableName="springmvc_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

  step2:shift+鼠標右鍵 選擇「在此處打開命令窗口」 輸入命令:app

    java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite框架

 

   這樣即爲成功,能夠看到在src目錄下已經生成了對應文件。dom

   

UserMapper.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="zhy.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="zhy.model.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" />
    <result column="email" property="email" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" > id, username, password, age, email </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from springmvc_user where id = #{id,jdbcType=INTEGER} </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from springmvc_user where id = #{id,jdbcType=INTEGER} </delete>
  <insert id="insert" parameterType="zhy.model.User" > insert into springmvc_user (id, username, password, age, email) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{email,jdbcType=VARCHAR}) </insert>
  <insert id="insertSelective" parameterType="zhy.model.User" > insert into springmvc_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>
      <if test="email != null" > email, </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>
      <if test="email != null" > #{email,jdbcType=VARCHAR}, </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="zhy.model.User" > update springmvc_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>
      <if test="email != null" > email = #{email,jdbcType=VARCHAR}, </if>
    </set> where id = #{id,jdbcType=INTEGER} </update>
  <update id="updateByPrimaryKey" parameterType="zhy.model.User" > update springmvc_user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, email = #{email,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update>
</mapper>

 

User.java:
package zhy.model; public class User { private Integer id; private String username; private String password; private Integer age; private String email; 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; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email == null ? null : email.trim(); } }

 

UserMapper.java:

package zhy.dao; import zhy.model.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); }

 

另外,也能夠在IDE中使用該工具,eclipse有相關插件,能夠在eclipse裏面安裝,地址爲:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/

相關文章
相關標籤/搜索