一篇文章帶你掌握主流辦公框架——SpringBoot( 八 )

  1. 簡單測試
package com.itheima;import com.itheima.dao.BookDao;import com.itheima.domain.Book;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass Springboot08MybatisApplicationTests { @Autowired private BookDao bookDao; @Test void testGetById() {Book book = bookDao.getById(1);System.out.println(book); }}整合SSM我們SpringBoot的最后課程就是用來整合SSM
我們同樣采用和之前SSM案例整合的代碼對比來介紹SpringBoot的SSM整合
Spring整合SSM我們先給出之前SSM整合的大致框架:
一篇文章帶你掌握主流辦公框架——SpringBoot

文章插圖
我們來簡單介紹上述代碼的作用不做具體代碼展示了(如有需要可以查看之前文章SSM整合):
  • Config文件夾:各種技術的Java配置類
  • SpringMvcSupport:攔截器,用來控制相關頁面展示
  • controller文件夾:服務層
  • Code:狀態碼集合
  • ProjectExceptionAdvice:異常處理類
  • Result:返回內容集合
  • dao文件夾:數據層
  • domain文件夾:實現類
  • exception文件夾:異常類
  • service文件夾:業務層接口以及實現類
  • resources文件夾:相關配置文件(jdbc配置文件內容)
  • webapp文件夾:前端代碼
  • pom.xml:各種依靠坐標
SpringBoot整合SSM由于我們的SSM內容過多,我們針對上次的SSM案例進行整合,部分內容不做修改,我們僅介紹更改部分
下面讓我們開始運行SpringBoot開始整合:
  1. 創建項目(運用了web , Mybatis,mysql技術棧)

一篇文章帶你掌握主流辦公框架——SpringBoot

文章插圖
  1. 查看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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version><relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>springboot_09_ssm</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_09_ssm</name> <description>Demo project for Spring Boot</description> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--TODO 添加必要的依賴坐標--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency> </dependencies> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build></project>
  1. 設置相關數據源,端口等(yaml)
# TODO 配置數據源相關信息server:port: 80spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_dbusername: rootpassword: root
  1. 對dao數據層進行簡單修改(添加@Mapper)
// 我們前面有提起Config文件夾全部刪除,導致我們需要手動配置dao的數據層映射package com.itheima.dao;import com.itheima.domain.Book;import org.apache.ibatis.annotations.*;import java.util.List;// TODO 添加@Mapper@Mapperpublic interface BookDao {@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")public int save(Book book);@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")public int update(Book book);@Delete("delete from tbl_book where id = #{id}")public int delete(Integer id);@Select("select * from tbl_book where id = #{id}")public Book getById(Integer id);@Select("select * from tbl_book")public List<Book> getAll();}

推薦閱讀