Spring boot 与 mybatis整合
依赖包引入
整个pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itsource</groupId>
<artifactId>springboot-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-parent</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
目录结构
数据源配置及mybatis映射配置
application.yml
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
datasource:
driverClassName: com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/spring-boot-demo?useUnicode=true&characterEncoding=utf-8
username : root
password : admin
#Mybatis配置
mybatis:
typeAliasesPackage: cn.itsource.model
mapperLocations: classpath:cn/itsource/dao/mapper/*.xml
configLocation: classpath:mybatisConfig.xml
#日志
logging:
level:
cn:
itsource: DEBUG
mybatisConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="cn.itsource.model" />
</typeAliases>
<plugins>
<!-- 打印完整sql语句 mysad -->
<plugin interceptor="org.kd.intercepts.MybatisAutoSql" />
</plugins>
</configuration>
代码实现
User.java
package cn.itsource.model;
public class User {
private Integer id;
private String name;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
** UserDao.java**
package cn.itsource.dao;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import cn.itsource.model.User;
//@Mapper
public interface UserDao {
//查询所有
@Select("select * from t_user")
public List<User> queryAll();
//getByid
@Select("select * from t_user where id=#{id}")
public User getById(Integer id);
// 保存
public void saveUser(User user);
// 保存
@Insert("insert into t_user(name,pwd) values(#{name},#{pwd})")
public void saveUser2(User user);
@Select("select * from t_user where name like '%${name}%' ")
public void like(User user);
// 查询
public List<User> query(User user);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itsource.dao.UserDao">
<insert id="saveUser">
insert into t_user(name,pwd) values(#{name},#{pwd})
</insert>
<select id="query" resultType="user">
select * from t_user where 1=1
<if test="name!=null and name!='' ">
and name =#{name}
</if>
<if test="pwd!=null and pwd!='' ">
and pwd =#{pwd}
</if>
</select>
</mapper>
UserService.java
package cn.itsource.service;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.github.pagehelper.PageInfo;
import cn.itsource.model.User;
public interface UserService {
//查询所有
public List<User> queryAll();
public PageInfo<User> queryPage(Integer pageNo,Integer pageSize);
}
UserServiceImpl.java
package cn.itsource.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.itsource.dao.UserDao;
import cn.itsource.model.User;
import cn.itsource.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserDao userDao;
@Override
public List<User> queryAll() {
return userDao.queryAll();
}
@Override
public PageInfo<User> queryPage(Integer pageNo, Integer pageSize) {
PageHelper.startPage(pageNo,pageSize);
List<User> list = userDao.queryAll();
return new PageInfo<>(list);
}
}
测试
package test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.itsource.RunApp;
import cn.itsource.dao.UserDao;
import cn.itsource.model.User;
import cn.itsource.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=RunApp.class)
public class Springbootmybatis_Test {
@Autowired
UserService userService;
@Autowired
UserDao userDao;
@Test
public void testQuaryALL() throws Exception {
System.out.println(userService.queryAll().size());
}
@Test
public void testGetById() throws Exception {
System.out.println(userDao.getById(1).getName());
}
// ${} #{}
@Test
public void testsave() throws Exception {
User user=new User();
user.setName("得到");
user.setPwd("xxx");
userDao.saveUser2(user);
}
@Test
public void testLike() throws Exception {
User user=new User();
user.setName("张");
userDao.like(user);
}
@Test
public void testQuery() throws Exception {
User user=new User();
user.setName("张");
user.setPwd("xxxx");
List<User> query = userDao.query(user);
for (User user2 : query) {
System.out.println(user2.getName());
}
}
@Test
public void testQueryPage() throws Exception {
PageInfo<User> queryPage = userService.queryPage(1, 2);
for (User u : queryPage.getList()) {
System.out.println(u.getName());
}
}
@Test
public void testQueryPage2() throws Exception {
PageHelper.startPage(1, 2);
List<User> list1 = userService.queryAll();
List<User> list2 = userService.queryAll();
System.out.println(list1.size());
System.out.println(list2.size());
}
}