《Java从入门到放弃》框架入门篇:SpringBoot+mybatis使用注解方式实现mapper
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了《Java从入门到放弃》框架入门篇:SpringBoot+mybatis使用注解方式实现mapper,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4600字,纯文字阅读大概需要7分钟。
内容图文
![《Java从入门到放弃》框架入门篇:SpringBoot+mybatis使用注解方式实现mapper](/upload/InfoBanner/zyjiaocheng/790/49f370f0e383432c8afbb8c5b09cbfc9.jpg)
上一篇说到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标注结果集引用
内容总结
以上是互联网集市为您收集整理的《Java从入门到放弃》框架入门篇:SpringBoot+mybatis使用注解方式实现mapper全部内容,希望文章能够帮你解决《Java从入门到放弃》框架入门篇:SpringBoot+mybatis使用注解方式实现mapper所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。