MyBatis逆向工程--gengenerator工具生成代码

 

1、创建数据库和表(针对MySQL)

SQL脚本如下:

Create DATABASE spring4_mybatis3;
USE spring4_mybatis3;

DROP TABLE IF EXISTS t_user;
CREATE TABLE t_user (
  user_id char(32) NOT NULL,
  user_name varchar(30) DEFAULT NULL,
  user_birthday date DEFAULT NULL,
  user_salary double DEFAULT NULL,
  PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建好的数据库和表如下:

  https://images0.cnblogs.com/blog/289233/201502/061332247034876.png

2、使用generator工具生成代码

  在网上找到了一个generator工具可以根据创建好的数据库表生成MyBatis的表对应的实体类,SQL映射文件和dao,找到generator工具根目录下的generator.xml文件,这个文件是用来配置代码生成规则的,如下图所示:

  https://images0.cnblogs.com/blog/289233/201502/061336050789568.png

编辑generator.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="D:\javaLib\hiblib\mysql-connector-java-5.1.7-bin.jar" /> 
    <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.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/spring4_mybatis3" userId="root" password="root"> 
        <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在com.demo.domain这个包下 -->
        <javaModelGenerator targetPackage="com.demo.bean" targetProject="D:\workspace2\spring_mybatis\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在com.demo.mapping这个包下 -->
        <sqlMapGenerator targetPackage="com.demo.mapper" targetProject="D:\workspace2\spring_mybatis\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在com.demo.dao这个包下 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.demo.dao" targetProject="D:\workspace2\spring_mybatis\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

 打开命令行窗口,切换到生成工具的根目录下,执行如下命令:

E:\EclipseEE\eclipse\WorkPlace\MyBatisShopping>     java -jar E:\EclipseEE\eclipse\WorkPlace\MyBatisShopping\mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

如下图所示:

https://images0.cnblogs.com/blog/289233/201502/061351006245763.png

刚才我们在generator.xml文件中配置将生成的代码和SQL映射文件放到" D:\workspace2\spring_mybatis\src "这个目录下,这个目录就是我们的spring-mybatis项目所在目录,我们刷新一下src目录,就可以看到生成的代码和映射文件了,如下图所示:

  

  生成的代码和映射文件一行都不用改,可以直接应用到项目当中。下面我们看一眼由generator工具生成的代码和映射文件:

1.生成的dao类:

public interface UserMapper {
    int deleteByPrimaryKey(String userId);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(String userId);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

生成的UserMapper是一个接口,里面定义了一些操作t_user表的增删改查方法。

2、生成的实体类

public class User {
    private String userId;
    private String userName;
    private Date userBirthday;
    private Double userSalary;
    //setter和getter省略
}

User类是t_user表的对应的实体类,User类中定义的属性和t_user表中的字段一一对应。

3、生成的SQL映射文件

<?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.demo.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.demo.bean.User" >
    <id column="user_id" property="userId" jdbcType="CHAR" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="user_birthday" property="userBirthday" jdbcType="DATE" />
    <result column="user_salary" property="userSalary" jdbcType="DOUBLE" />
  </resultMap>
  <sql id="Base_Column_List" >
    user_id, user_name, user_birthday, user_salary
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from t_user
    where user_id = #{userId,jdbcType=CHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from t_user
    where user_id = #{userId,jdbcType=CHAR}
  </delete>
  <insert id="insert" parameterType="com.demo.bean.User" >
    insert into t_user (user_id, user_name, user_birthday, 
      user_salary)
    values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE}, 
      #{userSalary,jdbcType=DOUBLE})
  </insert>
  <insert id="insertSelective" parameterType="com.demo.bean.User" >
    insert into t_user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="userId != null" >
        user_id,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="userBirthday != null" >
        user_birthday,
      </if>
      <if test="userSalary != null" >
        user_salary,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="userId != null" >
        #{userId,jdbcType=CHAR},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="userBirthday != null" >
        #{userBirthday,jdbcType=DATE},
      </if>
      <if test="userSalary != null" >
        #{userSalary,jdbcType=DOUBLE},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.demo.bean.User" >
    update t_user
    <set >
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="userBirthday != null" >
        user_birthday = #{userBirthday,jdbcType=DATE},
      </if>
      <if test="userSalary != null" >
        user_salary = #{userSalary,jdbcType=DOUBLE},
      </if>
    </set>
    where user_id = #{userId,jdbcType=CHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.demo.bean.User" >
    update t_user
    set user_name = #{userName,jdbcType=VARCHAR},
      user_birthday = #{userBirthday,jdbcType=DATE},
      user_salary = #{userSalary,jdbcType=DOUBLE}
    where user_id = #{userId,jdbcType=CHAR}
  </update>
</mapper>

UserMapper.xml这个文件的内容是编写操作t_user表的SQL语句,重点说一下UserMapper.xml配置中需要注意的几个小细节问题:

  1UserMapper.xml<mapper>标签的namespace必须是UserMapper接口的全类名,既<mapper namespace="com.demo.dao.UserMapper" >

  2UserMapper.xml的定义操作数据库的<select><delete><update><insert>这些标签的id属性的值必须和UserMapper接口定义的方法名一致,如下图所示:

  

  之所以有上述说的这两点要求,就是为了能够让MyBatis能够根据UserMapper接口和UserMapper.xml文件去自动实现UserMapper接口中定义的相关方法,这样我们就不再需要针对UserMapper接口去编写具体的实现代码了。

猜你喜欢

转载自blog.csdn.net/qq_41172982/article/details/89251835
今日推荐