您的位置:首页 > 编程语言 > Java开发

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

2016-09-03 17:00 686 查看
在Spring Boot中集成MyBatis,可以选用基于注解的方式,也可以选择xml文件配置的方式。在这里使用基于注解的方式进行集成。


引入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. 其他
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringBoot Mybatis
相关文章推荐