Spring Boot系列(十) 持久层框架--Mybatis
来源:互联网 发布:关于大数据的书籍推荐 编辑:程序博客网 时间:2024/05/21 09:37
在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 * */@Mapperpublic 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 pathmybatis.mapperLocations=classpath*:/META-INF/mybatis/mappers/*.xml# entity/domain autoscan packagemybatis.type-aliases-package=com.ft.turorial.spring.boot# use auto-mapping after you can remove @ResultMap on mapper of interfacemybatis.configuration.map-underscore-to-camel-case=true
’
4. 其他
1 0
- Spring Boot系列(十) 持久层框架--Mybatis
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- 持久层框架---MyBatis
- 持久层框架--mybatis
- MyBatis 持久层框架
- Spring Boot系列教程十:Spring boot集成MyBatis
- spring整合使用持久层框架mybatis(二)
- 持久层框架之MyBatis
- spring-boot 集成jpa 持久层
- Java数据持久层框架 MyBatis
- J2EE持久层框架mybatis配置文件写法
- J2EE持久层框架mybatis配置文件写法
- Mybatis持久层框架(详细讲解)
- MyBatis持久层框架使用总结
- 持久层框架:Mybatis快速入门
- 毕设-使用持久层框架Mybatis
- spring+mybatis构建持久层应用
- Spring Boot系列之十 日志配置
- C/C++
- LeakCanary:检测所有的oom
- 文章标题PHP中的加密方式
- 部分电脑ubuntu系统无线wifi断网
- unity-www下载
- Spring Boot系列(十) 持久层框架--Mybatis
- string 基本字符序列容器
- unix/linux学习笔记:chapter1
- Android6.0 反编译systemUI
- Netty HTTP 文件下载例子
- 用对象来实现闭包
- lua元方法的简单介绍
- DLL隐式与显式调用浅析
- Spring-Hibernate