Spring整合系列: idea SpringBoot+Mybatis+Thymeleaf
所需的框架:
- 主要框架:SpringBoot、SpringMVC、Mybatis、Thymeleaf
- 輔助框架:mysql驅動、Druid數據連接池、Json(阿里)、Lombok
構建項目:
- 使用開源項目Spring Initializr生成我們所需要的項目基本架構。
- 進入 https://start.spring.io/ ,並生成項目下載到本地。
- 解壓下載下來的項目,我這裡解壓到Demo目錄下。
- 使用IDEA打開項目
- 選擇自動導入
- 之後添加我們未能夠在項目構建時所添加到框架,Druid數據連接池、Json(阿里)等。打開Pom.xml文件,在Mvnrepository裡搜索我們要添加的框架。Pom.xml文件(Ps:個人習慣將框架分類,)
<?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>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.moonlit</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- 阿里Json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 因為SpringBoot已經幫我們整合了Spring跟SpringMVC,所以我們比不需要自己去編寫那些配置文件,只需要提供數據庫連接池。
#DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/testssm?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=qwe123
#Mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
#編碼
server.tomcat.uri-encoding=UTF-8
#devtools
spring.thymeleaf.cache=true
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
- 項目文件分佈圖
- 配置entity 直接上代碼 User.java
package com.moonlit.SpringBootDemo.entity;
import lombok.Data;
import org.springframework.stereotype.Repository;
/**
* @Author: Joshua_Zheng
* @Date: 2019/5/13 15:08
* @Email: [email protected]
* @Version 1.0
*/
@Data
@Repository
public class User {
private int id;
private String name;
}
- 配置業務邏輯層UserService.java與UserServiceImpl.java
package com.moonlit.SpringBootDemo.service;
import com.moonlit.SpringBootDemo.entity.User;
import java.util.List;
/**
* @Author: Joshua_Zheng
* @Date: 2019/5/13 15:10
* @Email: [email protected]
* @Version 1.0
*/
public interface UserService {
/**
* 查詢所有用戶
* @return
*/
List<User> listUsers();
}
package com.moonlit.SpringBootDemo.service.impl;
import com.moonlit.SpringBootDemo.dao.UserDao;
import com.moonlit.SpringBootDemo.entity.User;
import com.moonlit.SpringBootDemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author: Joshua_Zheng
* @Date: 2019/5/13 15:10
* @Email: [email protected]
* @Version 1.0
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> listUsers() {
return this.userDao.listUsers();
}
}
- 數據訪問層:UserDao.java,UserDaoMapper.xml
package com.moonlit.SpringBootDemo.dao;
import com.moonlit.SpringBootDemo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author: Joshua_Zheng
* @Date: 2019/5/13 15:12
* @Email: [email protected]
* @Version 1.0
*/
@Mapper
@Repository
public interface UserDao {
/**
* 查詢所有用戶
*
* @return
*/
List<User> listUsers();
}
<?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.moonlit.SpringBootDemo.dao.UserDao">
<!-- 结果集映射处理-->
<resultMap id="User" type="com.moonlit.SpringBootDemo.entity.User">
<result column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
<select id="listUsers" resultMap="User">
select * from user;
</select>
</mapper>
- 配置Controller層:UserController.java
package com.moonlit.SpringBootDemo.controller;
import com.alibaba.fastjson.JSON;
import com.moonlit.SpringBootDemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
* @Author: Joshua_Zheng
* @Date: 2019/5/13 15:18
* @Email: [email protected]
* @Version 1.0
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* 1.直接發送請求
*
* @return
*/
@RequestMapping("/list")
@ResponseBody
private String listUsers() {
String users = JSON.toJSONString(this.userService.listUsers());
return users;
}
/**
* 訪問靜態頁面
*
* @return
*/
@RequestMapping("/lo")
public String login() {
/**
* 因為使用了Thymeleaf。它默認就檢索“/templates/”文件下的資源,
* 如果要訪問static下的資源就需要重定向
*/
return "redirect:/login.html";
}
/**
* 訪問動態頁面,並請求數據
*
* @return
*/
@RequestMapping("/Hi")
public ModelAndView sayHello() {
ModelAndView modelAndView = new ModelAndView();
/**
* modelAndView.setViewName("hello1")
* 裡面的參數對應templates文件下的hello.html文件
*/
modelAndView.setViewName("Hello");
String users = JSON.toJSONString(this.userService.listUsers());
modelAndView.addObject("key", users);
return modelAndView;
}
}
- 開啟熱部署
- 配置SpringBoot啟動文件
package com.moonlit.SpringBootDemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.moonlit.SpringBootDemo.dao")
public class SpringBootDemoApplication {
/**
* 備註:
* EnableTransactionManagement 開啟事務管理
* MapperScan 掃描DAO層
*/
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
- 靜態資源頁面與動態資源頁面 login.html與Hello.html
靜態資源
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>靜態資源</title>
</head>
<body>
<h1>這是靜態資源</h1>
</body>
</html>
動態資源
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>動態資源</title>
</head>
<body>
<h2>這是動態資源</h2>
<span th:text="${key}"></span>
</body>
</html>