04 )SpringBoot 和 MyBatis 整合
阿新 • • 發佈:2018-12-17
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層的相關方法去實現資料庫操作
關注公眾號,將會有更多精彩每天送達:
公眾號內有精彩內容,可以提現