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.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置文件
(1)application.yml (切换环境)
spring:
profiles:
active: dev
(2)application-dev.yml(开发环境)
#开发环境
server:
port: 8081
spring:
datasource:
username: root
password:
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapper/*.xml
jpa:
show-sql: true
hibernate:
ddl-auto: update
thymeleaf:
cache: false #开发时关闭缓存,不然没法看到实时页面
prefix: classpath:/templates/
check-template-location: true #check-tempate-location: 检查模板路径是否存在
suffix: .html
encoding: UTF-8
mode: HTML5
servlet:
content-type: text/html
(3)application-prod.yml(生产环境)
#生产环境
server:
port: 80
spring:
datasource:
username: root
password:
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapper/*.xml
jpa:
show-sql: true
hibernate:
ddl-auto: update
thymeleaf:
cache: false #开发时关闭缓存,不然没法看到实时页面
prefix: classpath:/templates/
check-template-location: true #check-tempate-location: 检查模板路径是否存在
suffix: .html
encoding: UTF-8
mode: HTML5
servlet:
content-type: text/html
3、后台功能代码
(1)实体类
package com.example.entity;
import java.io.Serializable;
public class TUser implements Serializable {
private Integer id;
private String name;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
(2)dao层
package com.example.mapper;
import com.example.entity.TUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;
public interface TUserMapper {
@Select("select * from user")
@Results({
@Result(property = "username", column = "username", jdbcType = JdbcType.VARCHAR),
@Result(property = "password", column = "password")
})
List<TUser> findAll();
}
(3)业务层(service)
package com.example.service.impl;
import com.example.entity.TUser;
import com.example.mapper.TUserMapper;
import com.example.service.TUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class TUserServiceImpl implements TUserService {
@Resource
private TUserMapper tUserMapper;
@Override
public List<TUser> findAll() {
return tUserMapper.findAll();
}
}
package com.example.service;
import com.example.entity.TUser;
import java.util.List;
public interface TUserService {
List<TUser> findAll();
}
Controller层:
package com.example.demo.controller;
import com.example.demo.entity.TUser;
import com.example.demo.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class TUserController {
@Autowired
TUserService tUserService;
@RequestMapping("/")
public String listUI(){
return "redirect:/list";
}
@RequestMapping("/list")
public String hello(Model model){
List<TUser> all = tUserService.findAll();
model.addAttribute("all",all);
return "index";
}
}
2、前端页面
页面一般存在在 templates 目录下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>yyshop</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}">
<link rel="stylesheet" th:src="@{/js/jquery-3.1.1.min.js}">
</head>
<body class="container">
<!--<img th:src="@{/img/bg.jpg}">-->
<br>
<h1>Information <small>Student List</small></h1>
<br>
<div class="alert alert-danger alert-dismissible" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
Paging function to be developed...
</div>
<br>
<div class="panel panel-default">
<div class="panel-heading">Student</div>
<table class="table table-striped">
<THEAD>
<tr>
<th>ID</th>
<th>Name</th>
<th>Password</th>
<th>Phone</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</THEAD>
<TBODY>
<tr th:each = "user:${all}">
<td th:text="${user.id}" ></td>
<td th:text="${user.name}" ></td>
<!-- <td th:text="${user.password}" ></td>
<td th:text="${user.phone}" ></td>
<td ><a th:href="${'/edit/'+user.userId}">edit</a></td>
<td><a th:href="${'/del/'+user.userId}" >delete</a></td>-->
</tr>
</TBODY>
</table>
</div>
<a class="btn btn-info" href="/toAdd" role="button">Add</a>
<a class="btn btn-info" href="/seach" role="button">Seach</a>
</body>
</html>