5.2 Mybatis反向(逆向)工程代码解读
反向工程生成了两个包entity和mapper,并且在这两个包中生成了8个文件。由于我们只针对两个表进行了反射工程同,所以每个数据库表对应4个文件。其中student表对应的4个文件分别为Student.java、StudentExample.java、StudentMapper.java和StudentMapper.xml。
1、 Student.java为实体类,该类中的属性与数据库中表的字段一一对应。
2、 StudentExample.java为查询条件生成类,该类负责生成执行增删改查时需要的各种条件。该类生成的查询条件是由动态SQL组成的,具有强大的组合能力和适应性。
3、 StudentMapper.java是一个接口,其中定义了重要的数据库操作方法,该接口和配置文件StudentMapper.xml一起通过Mybits框架实现各种数据库操作。其代码如下:
package mapper; import entity.Student; import entity.StudentExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface StudentMapper { int countByExample(StudentExample example); int deleteByExample(StudentExample example); int deleteByPrimaryKey(Integer stuid); int insert(Student record); int insertSelective(Student record); List<Student> selectByExample(StudentExample example); Student selectByPrimaryKey(Integer stuid); int updateByExampleSelective(@Param("record") Student record, @Param("example") StudentExample example); int updateByExample(@Param("record") Student record, @Param("example") StudentExample example); int updateByPrimaryKeySelective(Student record); int updateByPrimaryKey(Student record); } |
4、 StudentMapper.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="mapper.StudentMapper" > <resultMap id="BaseResultMap" type="entity.Student" > <id column="stuid" property="stuid" jdbcType="INTEGER" /> <result column="stuname" property="stuname" jdbcType="VARCHAR" /> <result column="stuage" property="stuage" jdbcType="INTEGER" /> </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" > stuid, stuname, stuage </sql> <select id="selectByExample" resultMap="BaseResultMap" parameterType="entity.StudentExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from student <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 student where stuid = #{stuid,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from student where stuid = #{stuid,jdbcType=INTEGER} </delete> <delete id="deleteByExample" parameterType="entity.StudentExample" > delete from student <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="entity.Student" > insert into student (stuid, stuname, stuage ) values (#{stuid,jdbcType=INTEGER}, #{stuname,jdbcType=VARCHAR}, #{stuage,jdbcType=INTEGER} ) </insert> <insert id="insertSelective" parameterType="entity.Student" > insert into student <trim prefix="(" suffix=")" suffixOverrides="," > <if test="stuid != null" > stuid, </if> <if test="stuname != null" > stuname, </if> <if test="stuage != null" > stuage, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="stuid != null" > #{stuid,jdbcType=INTEGER}, </if> <if test="stuname != null" > #{stuname,jdbcType=VARCHAR}, </if> <if test="stuage != null" > #{stuage,jdbcType=INTEGER}, </if> </trim> </insert> <select id="countByExample" parameterType="entity.StudentExample" resultType="java.lang.Integer" > select count(*) from student <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update student <set > <if test="record.stuid != null" > stuid = #{record.stuid,jdbcType=INTEGER}, </if> <if test="record.stuname != null" > stuname = #{record.stuname,jdbcType=VARCHAR}, </if> <if test="record.stuage != null" > stuage = #{record.stuage,jdbcType=INTEGER}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update student set stuid = #{record.stuid,jdbcType=INTEGER}, stuname = #{record.stuname,jdbcType=VARCHAR}, stuage = #{record.stuage,jdbcType=INTEGER} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="entity.Student" > update student <set > <if test="stuname != null" > stuname = #{stuname,jdbcType=VARCHAR}, </if> <if test="stuage != null" > stuage = #{stuage,jdbcType=INTEGER}, </if> </set> where stuid = #{stuid,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="entity.Student" > update student set stuname = #{stuname,jdbcType=VARCHAR}, stuage = #{stuage,jdbcType=INTEGER} where stuid = #{stuid,jdbcType=INTEGER} </update> </mapper> |
StudentMapper.xml配置文件使用Spring和Mybatis框架共同配合封装了大量操作数据库的功能,这些功能可以通过调用StudentMapper.java接口中的方法来实现,具体使用请参看第六章。

