Spring Boot系列(十) 持久层框架--Mybatis

在Spring Boot中集成MyBatis,可以选用基于注解的方式,也可以选择xml文件配置的方式。在这里使用基于注解的方式进行集成。
  1. 引入mybatis-spring-boot-starter
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>


2. 创建Mapper.java文件及mapper.xml文件
自动扫描Mapper.java文件的方式有两种:
1)再Spring Boot的启动类上增加@MapperScan注解
2)在每个Mapper.java接口类中上增加@Mapper注解
使用第一种方法,需要指定包路径,例如:@MapperScan(“com.ft.turorial.spring.boot.mapper”),但是有个缺点,它不支持@MapperScan(“com.ft...mapper.*”)这种方式,所以推荐第二种方式。
UserMapper.java

/**
 * Mapper接口的sql语句可以通过在方法上增加注解,也可以将sql语句配置在 mapper.xml文件中
 * 
 * Mapper接口的扫描,可以在接口类上增加@Mapper,也可以在启动类上增加@MapperScan
 * @author ft
 *
 */
@Mapper
public interface UserMapper {

    User insertSelective(User user);
    User insert(User user);
    @Select("select * from user where id= #{id,jdbcType=INTEGER} ")
    User findOne(int id);
    List<User> findAll();
    User findLikeName(String name);

    int deleteByPrimaryKey(int id);

    int updateByPrimaryKeySelective(User user);
    int updateByPrimaryKey(User user);

}

userMapper.xml(在resource目录下建立\MATA-INF\mybatis\mappers文件夹来存放,可以自定义)

<?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.ft.turorial.spring.boot.mapper.UserMapper" >
  <cache />
  <resultMap id="User" type="com.ft.turorial.spring.boot.domain.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, birthday
  </sql>
  <!-- <select id="findOne" resultMap="User" parameterType="java.lang.Integer">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select> -->
  <select id="findAll" resultMap="User" resultType="list">
    select 
    <include refid="Base_Column_List" />
    from user
  </select>
  <select id="findLikeName" resultMap="User" parameterType="string" resultType="list">
    select 
    <include refid="Base_Column_List" />
    from user
    where name like concat('%',#{name,jdbcType=VARCHAR},'%')
  </select>

  <insert id="insert" parameterType="com.ft.turorial.spring.boot.domain.User" useGeneratedKeys="true" keyProperty="id">
    insert into user (id, name, birthday)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE})
  </insert>

  <insert id="insertSelective" parameterType="com.ft.turorial.spring.boot.domain.User" useGeneratedKeys="true" keyProperty="id">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="birthday != null" >
        birthday,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null" >
        #{birthday,jdbcType=DATE},
      </if>
    </trim>
  </insert>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where ID = #{id,jdbcType=INTEGER}
  </delete>
  <update id="updateByPrimaryKeySelective" parameterType="User" >
    update user
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null" >
        birthday = #{birthday,jdbcType=DATE},
      </if>
    </set>
    where ID = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.ft.turorial.spring.boot.domain.User" >
    update user
    set name = #{name,jdbcType=VARCHAR},
        birthday = #{birthday,jdbcType=DATE}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

从上面代码可以看出,mapper.xml可以仅仅作为字段映射来用,sql语句可以通过Mybatis的注解(@Select,@Insert、@Delete、@Update)写在Mapper.java接口中。


3. 指定映射文件(xml)的位置及自动扫描实体包名

在application.properties中配置如下信息:

# mybatis
#   mapper.xml path
mybatis.mapperLocations=classpath*:/META-INF/mybatis/mappers/*.xml
#   entity/domain autoscan package
mybatis.type-aliases-package=com.ft.turorial.spring.boot
#   use auto-mapping    after you can remove @ResultMap on mapper of interface
mybatis.configuration.map-underscore-to-camel-case=true


4. 其他

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值