《Java从入门到放弃》框架入门篇:SpringBoot+mybatis使用注解方式实现mapper

上一篇说到springboot+mybatis可以完全注解不用配置文件,本篇主要将mapper.xml文件改为纯注解方式。

原AuthorMapper.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.pxy.dao.AuthorMapper" >
  <resultMap id="BaseResultMap" type="com.pxy.entity.Author" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password, email, address, phone
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from author
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from author
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.pxy.entity.Author" >
    insert into author (id, username, password, 
      email, address, phone
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.pxy.entity.Author" >
    insert into author
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="address != null" >
        address,
      </if>
      <if test="phone != null" >
        phone,
      </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="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pxy.entity.Author" >
    update author
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.pxy.entity.Author" >
    update author
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

现将mapper.xml文件直接删除。之后修改AuthoerMapper文件,添加如下注解。

public interface AuthorMapper {

	@Delete("delete from from author where id = #{id}")
	int deleteById(Integer id);
	@Insert("insert into author (id, username, password, email, address, phone)values (#{id}, #{username}, #{password}, #{email}, #{address}, #{phone})")
    int insert(Author record);
	@Update("update author set username = #{username},password = #{password},email = #{email},address = #{address},phone = #{phone} where id = #{id}")
    int update(Author record);
    
	@Select("select id, username, password, email, address, phone from author where id = #{id}")
    @Results(id="authorMapper", value={
        @Result(column="id", property="id", id=true),
        @Result(column="username", property="username"),
        @Result(column="password", property="password"),
        @Result(column="email", property="email"),
        @Result(column="address", property="address"),
        @Result(column="phone", property="phone")
    })
    Author selectByPrimaryKey(Integer id);

	@Select("select id, username, password, email, address, phone from from author")
	@ResultMap("authorMapper")
	List<Author> selectAll();

}

修改完毕,运行项目,结果正常显示。

 

下面是对Mapper中常用注解的说明:

  • @Select 是查询类的注解,所有的查询均使用这个
  • @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
  • @Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值
  • @Update 负责修改,也可以直接传入对象
  • @delete 负责删除
  • @Results标注之前xml中的resultMap
  • @ResultMap标注结果集引用

 

上一篇:JdbcType类型和Java类型的对应关系


下一篇:mongodb总用方法总结