基於SpringBoot搭建SSM專案
阿新 • • 發佈:2018-12-14
在idea中我們先建立好maven專案,匯入好依賴包
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xt</groupId> <artifactId>ssmboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ssmboot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- druid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <!-- thymeleaf模版引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- springBoot web 啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mysql 驅動包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- springBoot測試包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis 與spirngboot整合 依賴包--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接下來,在src目錄下,新建如下目錄
controller dao pojo service
資料表
先在pojo中完成實體類的編寫
Book.java如下
package com.xt.pojo; import java.util.Date; /** * book 實體 * @author xz */ public class Book { private int bid; private String name; private Date credate; private String note; public Book() { } public Book(int bid, String name, Date credate, String note) { this.bid = bid; this.name = name; this.credate = credate; this.note = note; } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCredate() { return credate; } public void setCredate(Date credate) { this.credate = credate; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "Book{" + "bid=" + bid + ", name='" + name + '\'' + ", credate=" + credate + ", note='" + note + '\'' + '}'; } }
接下來編寫 dao層的mapper類
BookMapper.java中如下
package com.xt.dao;
import com.xt.pojo.Book;
import java.util.List;
/**
* book dao 操作
* @author xz
*/
public interface BookMapper {
List<Book> selectAllBooks();
}
再來編寫service層的
BookServiceI
package com.xt.service; import com.xt.pojo.Book; import java.util.List; /** * book業務層 * @author xz */ public interface BookServiceI { List<Book> getAllBook(); }
實現類
package com.xt.service.impl;
import com.xt.dao.BookMapper;
import com.xt.pojo.Book;
import com.xt.service.BookServiceI;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class BookServiceImpl implements BookServiceI {
@Resource
private BookMapper bookDao;
@Override
public List<Book> getAllBook() {
return bookDao.selectAllBooks();
}
}
最後編寫controller控制訪問層
BookController
package com.xt.controller;
import com.xt.pojo.Book;
import com.xt.service.BookServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* 控制層
* @author xz
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Resource
private BookServiceI bookServiceImpl;
/**
* 獲取所有圖書資訊,返回到list檢視
*/
@RequestMapping("/getAll")
public String list(Model model){
List<Book> list = bookServiceImpl.getAllBook();
model.addAttribute("list",list);
System.out.println(list);
return "list";
}
}
然後繼續編寫mapper檔案 此處我將mapper檔案放入到了resources目錄下
resouces下新建mapper資料夾,主要用來存放mapper檔案
BookMapper.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="com.xt.dao.BookMapper">
<select id="selectAllBooks" resultType="book">
select * from books
</select>
</mapper>
接下來我們還需要配置springBoot的配置檔案
在resouces目錄下的application.properties中 配置如下
#基礎配置
server.port=8080
#資料庫配置
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/booksystem
#配置指定的資料來源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#配置指定 mapper 別名所在的實體包
mybatis.type-aliases-package=com.xt.pojo
#Mybatis mapper檔案掃描
mybatis.mapper-locations=classpath*:mapper/*.xml
#指定 jpa的 database 型別
#spring.jpa.database=MYSQL
#開啟jpa的 sql顯示
#spring.jpa.show-sql=true
#指定dao的sql顯示
logging.level.com.xt.dao=debug
最後,只有list檢視沒有編寫,一般的 檢視頁面我們都放在resoueces下的templates目錄下
新建 list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>展示所有圖書列表</title>
</head>
<body>
<table align="center">
<tr align="center">
<td>ID</td>
<td>名稱</td>
<td>日期</td>
<td>描述</td>
</tr>
<tr th:each="book : ${list}" style="width: auto;" align="center">
<td th:text="${book.bid}"></td>
<td th:text="${book.name}"></td>
<td th:text="${#calendars.format(book.credate,'yyyy-MM-dd')}"></td>
<td th:text="${book.note}"></td>
</tr>
</table>
</body>
</html>
這裡用到的是 thymleaf模版
最終resouces目錄如下
編寫SpringBoot啟動器
package com.xt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.xt.dao")//掃描mapper
public class SSMApplication {
public static void main(String[] args) {
SpringApplication.run(SSMApplication.class,args);
}
}
啟動main方法,訪問頁面
到此 SpringBoot啟動SSM專案完畢