整合MyBatis和JPA
- SpringCloud使用版本
- SpringBoot:2.0.6.RELEASE
- SpringCloud:Finchley.SR2
- 需要的依賴pom
- SpringBoot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
- SpringCloud
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
- 整合MyBatis用到的pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql 使用的是5.8的mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--druid 1.1.0版本有bug,建議升級1.1.10-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
- 整合JPA的pom
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql 使用mysql8數(shù)據(jù)庫-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
- 配置文件
- 整合mybatis的配置文件
spring:
application:
# 項(xiàng)目名稱
name: product
# 數(shù)據(jù)源配置 最好加上編碼集和時(shí)區(qū)(使用的端口是3307)
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3307/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
# 注冊eureka服務(wù)的地址
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
# 項(xiàng)目使用的端口
server:
port: 8004
# mybatis配置
mybatis:
mapper-locations: classpath*:mappings/**/*.xml
configuration:
map-underscore-to-camel-case: true
- 整合JPA配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3307/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
jpa:
show-sql: true
- JPA代碼實(shí)現(xiàn)-片段
-
XXXRepository 繼承 JpaRepostory 類