SpringBoot04 SpringBoot 和 MyBatis 整合
1 所需的jar包
mysql驱动包:mysql-connector-java
数据库链接池:druid
mybatis对应jar包:mybatis-spring-boot-starter
分页查询对应jar包:pagehelper
<?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.springBoot</groupId> <artifactId>springBootProject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springBootProject</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.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> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jpa,类似于连接池--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--mybatis相关--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- alibaba的druid数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> <!--分页查询相关--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <!-- spring热部署--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
2 在数据库中创建一个表并在后台的实体层创建一个与之对应的实体类
package cn.springBoot.springBootProject.entity.test; /** * Teacher实体类 */ public class Teacher { private Integer id; private String name; private String password; public Teacher() { } public Teacher(Integer id, String name, String password) { this.id = id; this.name = name; this.password = password; } 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 getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
3 创建一个持久层接口
package cn.springBoot.springBootProject.dao.test; import cn.springBoot.springBootProject.entity.test.Teacher; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; import java.util.List; /** * Teacher实体类对应的持久层接口 */ @Repository public interface TeacherMapper { @Select("select * from sb_teacher") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "password", column = "password") }) List<Teacher> findAllTeacher(); }
4 在配置文件中配置数据库链接信息
# 服务配置 server: context-path: /devProject port: 88 # 数据库配置 spring: # datasource: # 配置数据库连接信息(利用默认的数据库链接池) # driver-class-name: com.mysql.jdbc.Driver # url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8 # username: dev # password: 182838 datasource: # 配置数据库链接信息(利用阿里巴巴提供的数据库连接池) name: test url: jdbc:mysql://127.0.0.1:3306/springboot username: dev password: 182838 # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 jpa: # 配置JPA hibernate: ddl-auto: update show-sql: true # 上传文件配置 http: multipart: # 配置文件上传大小 max-file-size: 50Mb max-request-size: 50Mb # 日志配置 logging: level: root: INFO org: # springframework: # web: DEBUG hibernate: ERROR file: e:\\demo\\demo.log mybatis: mapper-locations: classpath:mapper/*Mapper.xml
5 在启动中添加mapper类扫描路径
6 在dao层的接口中操作数据库,服务层调用dao层的相关方法去实现数据库操作
7 详情请参见这篇博客