技术交流


springboot2.1整合JPA与mybatis

Sep 11, 2019 1:37:00 PM
136
1

JPAmybatis作为持久层框架各有各的优点,比如mybatis的灵活易用,JPA可以少写SQL,方便开发者快速开发。

 

springboot整合JPAmybatis

现在以查询一张学生表为例,用来验证springboot整合JPAmybatis的整合结果。学生表如下:

62154d2d-94d4-45de-a8ba-114264598deb

1、maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql链接工具 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>
<!-- jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--thymeleaf模板引擎 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

2、学生实体类

package work.chenchuan.study.mybatisjpa.po;

import javax.persistence.*;

@Entity
@Table(name = "student_test")
public class StudentPo {

    /**
     * 学生编号
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer studentId;

    /**
     * 学生姓名
     */
    private String studentName;

    /**
     * 年龄
     */
    private String age;


    @Override
    public String toString() {
        return "StudentPo{" +
                "studentId=" + studentId +
                ", studentName='" + studentName + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

3、yml配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1/test?characterEncodeing=utf-8&serverTimezone=GMT%2b8
    username: root
    password: 123
  jpa:
    show-sql: true #显示执行sql
mybatis:
  mapper-locations: classpath:mybatis/**/*.xml #mapper文件存放路径

4、整合并验证mybatis

1)、创建mapper接口StudentMapper

@Repository
@Mapper
public interface StudentMapper {

    /**
     * 查询所有学生
     *
     * @return
     */
    List<StudentPo> findAllStudent();
}

2)、根据yml配置文件创建在对应路径下创建mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.2//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="work.chenchuan.study.mybatisjpa.dao.mapper.StudentMapper">

    <!-- 查询所有学生 -->
    <select id="findAllStudent" resultType="work.chenchuan.study.mybatisjpa.po.StudentPo">
        SELECT
          student_id    AS studentId,
          student_name  AS studentName,
          age           AS age
        FROM student_test
    </select>

</mapper>

3)、测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootStudyApplicationTests {

    @Autowired
    private StudentMapper studentMapper;

    
    @Test
    public void findAllStudent() {
        System.out.println(studentMapper.findAllStudent());
    }
}

打印结果如下,mybatis整合成功:

[StudentPo{studentId=1, studentName='jack', age='12'}, StudentPo{studentId=2, studentName='tom', age='10'}]

5、整合并验证JPA

1)、创建jpa接口

public interface StudentJpaDao extends JpaRepository<StudentPo, Integer> {
}

2)、测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootStudyApplicationTests {

    @Autowired
    private StudentJpaDao studentJpaDao;


    @Test
    public void findOneStudent() {
        System.out.println(studentJpaDao.findById(1));
    }
}

打印结果如下,JPA整合成功:

[StudentPo{studentId=1, studentName='jack', age='12'}]

 

总结

本次是将JPAmybatis同时整合在一个项目里的,需要注意对于同一个实体,JPA的接口与mybatis的接口要分开单独创建才能同时使用,不然会抛异常。


如果你喜欢我的内容,就请打赏一下吧
微信
支付宝
温馨提示: 你的打赏金额会直接转入对方账户,不可退回。

评论专区


审核通过的评论(0)
暂无评论信息
个人名片

  欢迎来到“浩瀚星尘”的个人博客!
  首先,该博客用于分享本人的生活事迹与兴趣爱好; 此外,该博客的主要作用便是与广大的小伙伴一起分享探讨开发技术, 希望大家多多关照。

网名: 浩瀚星尘
城市: 重庆
工作: java