《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标注结果集引用

 

使用 Spring Boot 的优势 使用 Spring Boot 开发项目,会给我们带来非常美妙的开发体验,可以从以下几个方面展开来说明 Spring Boot 让开发变得更简单 Spring Boot 对开发效率的提升是全方位的,我们可以简单做一下对比: 在没有使用 Spring Boot 之前我们开发一个 web 项目需要做哪些工作: 1)配置 web.xml,加载 Spring 和 Spring mvc 2)配置数据库连接、配置 Spring 事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件 … n) 配置完成之后部署 tomcat 调试 可能你还需要考虑各个版本的兼容性,jar 包冲突的各种可行性。 那么使用 Spring Boot 之后我们需要开发一个 web 项目需要哪些操作呢? 1)登录网址 http://start.spring.io/ 选择对应的组件直接下载 2)导入项目,直接开发 上面的 N 步和下面的2步形成巨大的反差,这仅仅只是在开发环境搭建的这个方面。 Spring Boot 使测试变得更简单 Spring Boot 对测试的支持不可谓不强大,Spring Boot 内置了7种强大的测试框架: JUnit: 一个 Java 语言的单元测试框架 Spring Test & Spring Boot Test:为 Spring Boot 应用提供集成测试和工具支持 AssertJ:支持流式断言的 Java 测试框架 Hamcrest:一个匹配器库 Mockito:一个 java mock 框架 JSONassert:一个针对 JSON 的断言库 JsonPath:JSON XPath 库 我们只需要在项目中引入 spring-boot-start-test依赖包,就可以对数据库、Mock、 Web 等各种情况进行测试。 Spring Boot Test 中包含了我们需要使用的各种测试场景,满足我们日常项目的测试需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值