1.前言
Springboot最近可谓是非常的火,本人也在项目中尝到了甜头。之前一直使用Springboot+JPA,用了一段时间发现JPA不是太灵活,也有可能是我不精通JPA,总之为了多学学Springboot我决定尝试一下Springboot+MyBatis+JPA三项集成,集成过程中遇到了很多问题,但最后总算是集成成功了,现在记录一下方法。
1.1 如何使用MyBatis Generator自动生成xxxMapper.java接口以及xxxMapper.xml文件
以前用过SpringMVC,知道写xxxMapper.java接口以及xxxMapper.xml文件的辛苦,这次集成最先想到的就是先解决如何使用如何使用MyBatis Generator自动生成这些文件的问题。
先扔出MyBatis Generator的官网->请戳这里
我使用的Maven集成插件的方式,IDE使用的是IDEA
1.1.1 创建项目
20180126110629.png
20180126110757.png
20180126110812.png
1.1.2 修改pom.xml
添加了Druid依赖和MyBatis Generator插件
其他依赖请自行添加
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.study.springboot
mybatis
1.0
war
demo
springboot+mybatis+jpa
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.1.5
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
Generate MyBatis Artifacts
generate
1.1.3 配置generatorConfiguration
generatorConfig.xml放在resource文件夹下
常用配置已经注释了,请自行查看
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
location="C:\Users\Semi\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root"
password="123456"/>
targetProject="E:\WorkSpace\study\springboot\mybatis\src\main\java">
targetProject="E:\WorkSpace\study\springboot\mybatis\src\main\resources">
targetProject="E:\WorkSpace\study\springboot\mybatis\src\main\java">
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
1.1.4 生成xxxMapper.java接口以及xxxMapper.xml文件
20180126111603.png
注意,如果存放xxxMapper.java和xxxMapper.xml文件的包不存在会报错
1.1.5 修改自动生成的实体类
对于model包下的实体类需要添加基于JPA的注解,否则JPA接口无法引用
例如对于User.java实体类需要进行如下修改
package com.study.springboot.mybatis.model;
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "user_name")
private String userName;
@Column(name = "passwd")
private String passwd;
@Column(name = "nick_name")
private String nickName;
@Column(name = "telephone")
private String telephone;
@Column(name = "email")
private String email;
@Column(name = "authority")
private Integer authority;
@Column(name = "department")
private String department;
@Column(name = "create_at")
private String createAt;
@Column(name = "update_at")
private String updateAt;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd == null ? null : passwd.trim();
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName == null ? null : nickName.trim();
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone == null ? null : telephone.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public Integer getAuthority() {
return authority;
}
public void setAuthority(Integer authority) {
this.authority = authority;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department == null ? null : department.trim();
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt == null ? null : createAt.trim();
}
public String getUpdateAt() {
return updateAt;
}
public void setUpdateAt(String updateAt) {
this.updateAt = updateAt == null ? null : updateAt.trim();
}
}
1.1.6 添加xxxJPA接口,例如UserJPA.java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.io.Serializable;
public interface UserJPA extends
Serializable,
JpaRepository,
JpaSpecificationExecutor {
}
1.1.7 测试
测试之前需要配置application.yml(你的有能是application.properties,改一下后缀名即可,我比较喜欢.yml文件的简洁)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
mybatis:
# 重要配置
type-aliases-package: com.study.springboot.mybatis.model
mapper-locations: classpath:mapper/*.xml
对于每一个xxxMapper.java接口,需要使用@Component注解
自动生成的xxxMapper.java接口是不带@Component注解的
@Component
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
在入口类中需要添加@MapperScan("com.study.springboot.mybatis.mapper")注解
@SpringBootApplication
@MapperScan("com.study.springboot.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
打开测试类,为测试类添加@MapperScan("com.study.springboot.mybatis.mapper")注解
@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan("com.study.springboot.mybatis.mapper")
public class MybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserJPA userJPA;
@Test
public void contextLoads() {
System.out.println(userMapper.selectByPrimaryKey(11).getUserName());
System.out.println(userJPA.findOne(11).getUserName());
}
}
20180126110153.png
最后如果出现
java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSession
的错误,请检查本地Maven仓库的配置,有可能是没有Maven没有下载需要引用的包