一, 前一篇博客中,介紹了一下Mybatis和hibernate的對比,在這一篇博客說說mybatis的逆向工程,展現一下,只要有一個數據庫,你的持久層,你的D層今後不用你本身手寫了。html
mybaits須要程序員本身編寫sql語句,mybatis官方提供逆向工程 能夠針對單表自動生成mybatis執行所須要的代碼(mapper.java,mapper.xml、pojo),企業實際開發中,經常使用的逆向工程方式:因爲數據庫的表生成java代碼。java
把generatorSqlmapCustom JavaProject導入程序員
修改配置文件的數據庫鏈接以及數據庫sql
指定數據庫表數據庫
生成表對應的實體類apache
<javaModelGenerator targetPackage="com.dtt.mybatis.pojo" targetProject=".\src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> <!-- 從數據庫返回的值被清理先後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator>
mapper映射文件生成mybatis
<!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.dtt.mybatis.mapper" targetProject=".\src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator>
mapper接口生成app
<!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dtt.mybatis.mapper" targetProject=".\src"> <!-- enableSubPackages:是否讓schema做爲包的後綴 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator>
幾個關鍵屬性:
javaModelGenerator :實體類生成;
sqlMapGenerator :映射文件生成;
javaClientGenerator :接口生成;
targetProject:生成文件的位置,本例中.\src,是指src下的目錄;
targetPackage:生成包的名稱;
修改完成後,運行GeneratorSqlmap.java中的main方法,就能夠生成。
ide
以User表爲例,展現生成的代碼:學習
package com.dtt.mybatis.pojo; import java.util.Date; public class User { private Integer id; private String username; private Date birthday; private String sex; private String address; 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex == null ? null : sex.trim(); } public String getAddress() { return address; } public void setAddress(String address) { this.address = address == null ? null : address.trim(); } }
這裏生成了全部的增刪改查的sql語句,調用的時候就直接使用對應的方法就能夠了很方便吧。(重點學習一下自動生成的代碼是如何封裝的)
package com.dtt.mybatis.mapper; import com.dtt.mybatis.pojo.User; import com.dtt.mybatis.pojo.UserExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface UserMapper { int countByExample(UserExample example); int deleteByExample(UserExample example); int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); List<User> selectByExample(UserExample example); User selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); int updateByExample(@Param("record") User record, @Param("example") UserExample example); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }
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="com.dtt.mybatis.mapper.UserMapper" > <resultMap id="BaseResultMap" type="com.dtt.mybatis.pojo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="username" property="username" jdbcType="VARCHAR" /> <result column="birthday" property="birthday" jdbcType="DATE" /> <result column="sex" property="sex" jdbcType="CHAR" /> <result column="address" property="address" jdbcType="VARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > id, username, birthday, sex, address </sql> <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.dtt.mybatis.pojo.UserExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <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> <delete id="deleteByExample" parameterType="com.dtt.mybatis.pojo.UserExample" > delete from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="com.dtt.mybatis.pojo.User" > insert into user (id, username, birthday, sex, address) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, #{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.dtt.mybatis.pojo.User" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="username != null" > username, </if> <if test="birthday != null" > birthday, </if> <if test="sex != null" > sex, </if> <if test="address != null" > address, </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="birthday != null" > #{birthday,jdbcType=DATE}, </if> <if test="sex != null" > #{sex,jdbcType=CHAR}, </if> <if test="address != null" > #{address,jdbcType=VARCHAR}, </if> </trim> </insert> <select id="countByExample" parameterType="com.dtt.mybatis.pojo.UserExample" resultType="java.lang.Integer" > select count(*) from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update user <set > <if test="record.id != null" > id = #{record.id,jdbcType=INTEGER}, </if> <if test="record.username != null" > username = #{record.username,jdbcType=VARCHAR}, </if> <if test="record.birthday != null" > birthday = #{record.birthday,jdbcType=DATE}, </if> <if test="record.sex != null" > sex = #{record.sex,jdbcType=CHAR}, </if> <if test="record.address != null" > address = #{record.address,jdbcType=VARCHAR}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, birthday = #{record.birthday,jdbcType=DATE}, sex = #{record.sex,jdbcType=CHAR}, address = #{record.address,jdbcType=VARCHAR} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="com.dtt.mybatis.pojo.User" > update user <set > <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="birthday != null" > birthday = #{birthday,jdbcType=DATE}, </if> <if test="sex != null" > sex = #{sex,jdbcType=CHAR}, </if> <if test="address != null" > address = #{address,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.dtt.mybatis.pojo.User" > update user set username = #{username,jdbcType=VARCHAR}, birthday = #{birthday,jdbcType=DATE}, sex = #{sex,jdbcType=CHAR}, address = #{address,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper>
逆向工程在不少軟件裏面都有,好比EA的逆向工程,PB的逆向工程。還有不少,總之,這個就是其中的一些頗有價值的東西,在企業運用的時候很方便,並且這些生成好的類,儘可能不要去修改,若是非要修改的話,能夠用pv類來實現繼承拓展。這是很好的選擇。