本帖最后由 梦想飞翔的猪 于 2018-7-23 14:33 编辑
【石家庄校区】Spring Boot整合MyBatis
前言:这段时间用springboot感觉确实挺好用的,很大程度上的简化了开发,由其是它的自动化配置,让大大的提高了开发效率,现在我们要让SpringBoot与MyBatis一起工作。下面就来详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射。 1.0 创建项目 1.1 首先创建springboot工程,点击file ——> new ——>project ——>Spring Initializr
1.2 点击下一步,按如下操作就好 1.3
1.4
1.5
1.6 操作完成后对应的pom文件如下(由于忘记选中mysql,需要手动添加mysql的maven依赖。) [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 [url=http://maven.apache.org/xsd/maven-4.0.0.xsd]http://maven.apache.org/xsd/maven-4.0.0.xsd[/url]">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.7 修改application.yml 文件,添加数据库连接信息 [XML] 纯文本查看 复制代码 spring:
datasource:
url: jdbc:mysql://localhost:3306/book
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver 同其他Spring Boot工程一样,简单且简洁的的完成了基本配置,下面看看如何在这个基础下轻松方便的使用MyBatis访问数据库。 2.0 使用mybatis 2.1 创建数据库表book [SQL] 纯文本查看 复制代码 CREATE TABLE `book` (
`bookid` int(11) NOT NULL AUTO_INCREMENT,
`bookname` varchar(50) NOT NULL,
`state` int(11) NOT NULL,
PRIMARY KEY (`bookid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; 2.2 创建对应实体 [Java] 纯文本查看 复制代码 public class Book {
private Integer bookid;
private String bookname;
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
@Override
public String toString() {
return "Book{" +
"bookid=" + bookid +
", bookname='" + bookname + '\'' +
'}';
}
} 2.3 创建BookMapper [Java] 纯文本查看 复制代码 import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.xixilover.entity.Book;
import java.util.List;
@Mapper
public interface BookDaoMapper {
@Select("select bookid,bookname from book")
List<Book> getList();
} 2.4 创建单元测试 [Java] 纯文本查看 复制代码 package cn.itcast.springbootmybatis;
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.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringbootMybatisApplication.class)
public class UserDaoMapperTest {
@Autowired
private BookDaoMapper bookDaoMapper;
@Test
public void testGetList(){
List<Book> list = bookDaoMapper.getList();
System.out.println(list);
}
} 3.0 测试结果如下 [Java] 纯文本查看 复制代码 [Book{bookid=1, bookname='java'}, Book{bookid=2, bookname='c'}, Book{bookid=3, bookname='c#'}, Book{bookid=4, bookname='php'}, Book{bookid=5, bookname='js'}]
|