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. 其他
相关文章推荐
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- Mybatis 系列(2)——SSM(spring mvc+spring+Mybatis)框架整合
- Spring Boot系列(十五) 安全框架Apache Shiro(二)缓存-EhCache
- Spring Boot系列之六 以注解方式整合MyBatis
- 2.SpringBoot系列-SpringBoot整合MyBatis
- SpringBoot入门系列:第六篇 mybatis
- MyEclipse中搭建spring-boot+mybatis+freemarker框架
- Spring Boot系列之七 以xml整合MyBatis
- Spring Boot干货系列:(九)数据存储篇-SQL关系型数据库之MyBatis的使用
- J2EE项目系列(四)--SSM框架构建积分系统和基本商品检索系统(Spring+SpringMVC+MyBatis)(3)Ajax使用详解(级联列表)以及企业级报表Excel导入导出实现
- 【微框架】之一:从零开始,轻松搞定SpringCloud微服务系列--开山篇(spring boot 小demo)
- SSM系列2:简易详细的(Spring +Springmvc+Mybatis)框架整合搭建
- Spring Boot干货系列:(七)默认日志框架配置
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service
- Spring Boot系列(十五) 安全框架Apache Shiro(一)基本功能
- SSM系列1:简易详细的(Spring +Springmvc+Mybatis)框架整合搭建
- Spring Boot系列教程十:Spring boot集成MyBatis
- IDEA搭建springboot+mybatis 的web框架附日志管理
- Spring+SpringMVC+Mybatis整合系列(三)SSM框架整合