上一篇中我已经介绍了SpringBoot整合MVC框架,在这节中我主要是介绍SpringBoot整合Mybatis,在这里我会讲解在整合中可能会遇到的各种错误以及对应的解决方法。
1.打开Idea,点击File->然后右键 new project
选择对应的JDK版本,我这里选择的是JDK8,然后点击next。
这里是对应的项目配置,按照自己的需求填写,然后点击Next。
这里是勾选我们项目中要依赖的jar包,这里我们只需要对应mysql的jar跟mybatis的jar就可以了。然后点击next。
填写项目名称以及项目的保存路径,然后点击finish,完成之后需要等待一下,因为需要下载对应的依赖jar包。然后下载完之后,我们的项目架构就出来了,springboot为我们自动封装了数据源,我们只要resources目录下新建一个application.yml(官方推荐使用,因为更加的简介)或者是application.properties。
在此我贴上对应pom的配置解释:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.mqc
springboot-mybatis
0.0.1-SNAPSHOT
springboot-mybatis
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.0.9
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
如下图,我已经新建了我们最常见的web项目目录结构,但是可以看到,springboot默认新建的项目跟我们新建的包是在不同的层级的包,需要注意的,springboot启动类会默认扫描同层级下所有的包的类上的注解,我们这样子的项目结构springboot启动是扫描不到对应的注解,这个时候我们就有两种解决方法:
这里我们到启动类移到对应的层级上,然后添加对应的数据库配置。
mybatis:
#xml配置开发,对应mapper.xml配置文件路径
mapper-locations: classpath:mapper/*.xml
#数据库配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/joker_mqc?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 199507
druid:
validationQuery: SELECT 1
initialSize: 10
minIdle: 10
maxActive: 200
minEvictableIdleTimeMillis: 180000
testOnBorrow: false
testWhileIdle: true
removeAbandoned: true
removeAbandonedTimeout: 1800
logAbandoned: true
poolPreparedStatements: true
maxOpenPreparedStatements: 100
其实,到这里,SpringBoot整合简单的mybatis就已经搭建好了,不得不说springboot是真的强大,接下来我们就测试我们的整合结果吧。
首先我们新建一张表用于测试,这里我就提前新建好了一个user_info表:
创建我们的实体类:
package com.mqc.entrty;
import javax.print.DocFlavor;
/**
* @author maoqichuan
* @ClassName: UserInfo
* @description: TODO
* @date 2019-04-0315:14
**/
public class UserInfo {
private Integer id;
private String userName;
private String password;
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
创建对应的mapper类,注意我们给这个类加上了@mapper,标记着这是一个mapper类
package com.mqc.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper {
void save(@Param("username") String username,@Param("password") String password);
}
然后在resources/mapper下新建对应UserMapper.xml文件
INSERT INTO user_info(username,password) VALUES (#{username},#{password})
我们再新建一个service接口
package com.mqc.service;
/**
* @description: 用户业务逻辑类
* @author maoqichuan
* @date 2019-04-03 15:17
*/
public interface IUserService {
void save(String username,String password) throws Exception;
}
然后在新建对应的实现类,@Transactional标记这个类具有事务处理能力。
package com.mqc.service.impl;
import com.mqc.mapper.UserMapper;
import com.mqc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author maoqichuan
* @ClassName: UserServiceImpl
* @description: TODO
* @date 2019-04-0315:18
**/
@Service
@Transactional
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public void save(String username, String password) throws Exception {
userMapper.save(username,password);
}
}
然后在新建对应的controller
package com.mqc.web.controller;
import com.mqc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author maoqichuan
* @ClassName: UserController
* @description: 用户逻辑控制类
* @date 2019-04-0315:19
**/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/save")
public String save(String username,String password) throws Exception{
userService.save(username,password);
return "save success";
}
}
然后在我们的启动类上加上:@EnableTransactionManagement 开启事务处理能力
package com.mqc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement // 开启事务管理
//在对应的mapper类加上@mapper注解就不用去扫
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
到这里,整个测试代码已经写完了,让我们来启动一下验证一下我们的结果吧。
然后细心的朋友可能会发现,控制器中打印了那么一句话,到最后测试还不成功!!!到底是啥原因,让我们来看看。
其实整个原因我们要来看我们的pom文件,细心的朋友有没有发现我们的pom文件的每一个依赖都是没有对应的版本号的,那到底是引用哪一个版本呢?
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
看到这,我想大家已经知道原因了,这个相当于是我们继承的父项目,他的版本是2.1.3的,这对应我们mysql来说太高,解决方案只要把springboot的版本降低就可以了。
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
这时候我们再启动就一直正常了。
然后返回成功,这时候我们再去看看我们的数据库到底有没有插入成功。
到这里,springboot整合mybatis已经介绍完了,本篇介绍的还是比较简单的应用,下篇将会介绍springboot配置多数据源配置,希望哪里写的不好或者不对的请纠正,让我们在技术路上一路修行,一起进步!!加油。