前期关联文章:
Spring Boot上传文件Spring Boot全局异常处理、全局数据、参数预处理
Spring Boot整合Servlet、Filter和Listener
MyBatis支持定制化SQL、存储过程以及高级映射,几乎省略了JDBC操作所有的手动设置参数以及获取结果集。在传统的SSM项目中需要一堆的xml配置,而在Spring Boot项目中,MyBatis官方提供了一套自动化配置,实现了开箱即用。下面就来说说Spring Boot与MyBatis的整合使用。
添加Maven依赖
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>版本号</version></dependency>
指定Mybatis xml文件路径
mybatis: mapper-locations: classpath:mapping/*.xm
创建Mapper
import java.util.List;import com.springboot.data.dto.Student;import com.springboot.data.dto.StudentBean;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface StudentMapper {int add(Student student);int update(Student student);int delete(Long id);Student find(Long id);List<StudentBean> findAll();}
通过@Mapper注解将类标注为MyBatis中的数据访问层。
创建Mapper对应的xml StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.springboot.data.mapper.StudentMapper"><insert id="add" parameterType="com.springboot.data.dto.Student">insert into springboot_student (name, sex, age) values (#{name},#{sex},#{age})</insert><update id="update" parameterType="com.springboot.data.dto.Student">update springboot_student set name = #{name},sex = #{sex},age = #{age} where id = #{id}</update><delete id="delete" parameterType="java.lang.Long">delete from springboot_student where id = #{id}</delete><select id="find" parameterType="java.lang.Long" resultType="com.springboot.data.dto.Student">select * from springboot_student where id = #{id}</select><resultMap id="studentBean" type="com.springboot.data.dto.StudentBean"><result property="id" column="id"/><result property="name" column="name"/></resultMap><select id="findAll" resultMap="studentBean">select * from springboot_student</select></mapper>
各个不同方法,标签不同,这个相信大家都能看出来。id需与对应Mapper中的方法名一致,parameterType指定参数类型,resultType指定返回值类型,比如当然我们可以通过自定义resultMap来达到想要的结果集,比如findAll方法。
service
import java.util.List;import com.springboot.data.dto.Student;import com.springboot.data.dto.StudentBean;import com.springboot.data.mapper.StudentMapper;import com.springboot.data.service.StudentService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/*** @author tanglei* @date 2020/1/10*/@Service("studentService")public class StudentServiceImpl implements StudentService {@AutowiredStudentMapper studentMapper;@Overridepublic boolean addByMyBatis(Student student) {return studentMapper.add(student) > 0;}@Overridepublic boolean updateByMyBatis(Student student) {return studentMapper.update(student) > 0;}@Overridepublic boolean deleteByMyBatis(Long id) {return studentMapper.delete(id) > 0;}@Overridepublic Student findByMyBatis(Long id) {return studentMapper.find(id);}@Overridepublic List<StudentBean> findByMyBatis() {return studentMapper.findAll();}}
- 项目结构

搬运不易,如果觉得文章还可以可点击文末右下角在看,让你的朋友可以看到,长按识别二维码关注转发一波:

可以点击在看,让你的朋友看到哦!
文章转载自代码小搬运,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




