第一步:创建一个SpringBoot项目
第二步导入pom依赖和配置application.yml文件
pom.xml依赖
.
// 导入maven依赖
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
// application.yml配置文件
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/club?serverTimezone=Asia/Shanghai
username: root
password: 123456
thymeleaf:
mode: HTML5
cache: false
suffix: .html
prefix: classpath:/templates/
main:
allow-bean-definition-overriding: true
mybatis:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.example.springbootdemo2.entity
logging:
level:
com.example.springbootdemo2.mapper: trace
第三步 SpringBoot 项目架构

实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Clubs {
private Integer cid;
private String cname;
private String city;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Players {
private Integer pid;
private String pname;
private String birthday;
private Integer height;
private Integer weight;
private String position;
private Integer cid;
private String cname;
}
创建Mapper接口和Mappe.xml
@Mapper
public interface PlayersMapper {
List<Players> findAll();
int add(Players players);
int delete(@Param("pid") Integer pid);
Players findById(@Param("pid") Integer pid);
int update(Players players);
}
<?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.example.springbootdemo2.mapper.PlayersMapper">
<select id="findAll" resultType="players">
select * from players p,clubs c where p.cid=c.cid
</select>
<insert id="add" parameterType="players">
insert into players(pname,birthday,height,weight,position,cid) values(#{pname},#{birthday},#{height},#{weight},#{position},#{cid})
</insert>
<delete id="delete">
delete from players where pid=#{pid}
</delete>
<select id="findById" resultType="players">
select * from players p,clubs c where p.cid=c.cid and p.pid=#{pid}
</select>
<update id="update" parameterType="players">
update players
<set>
<if test="pname != null">pname=#{pname},</if>
<if test="birthday != null">birthday=#{birthday},</if>
<if test="height != null">height=#{height},</if>
<if test="weight != null">weight=#{weight},</if>
<if test="position != null">position=#{position},</if>
<if test="cid != null">cid=#{cid},</if>
</set>
where pid = #{pid}
</update>
</mapper>
@Mapper
public interface ClubsMapper {
List<Clubs> findAlls();
}
<?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.example.springbootdemo2.mapper.ClubsMapper">
<select id="findAlls" resultType="clubs">
select * from clubs
</select>
</mapper>
Service层 和ServiceImpl实现层
public interface PlayersService {
List<Players> findAll();
int add(Players players);
int delete(@Param("pid") Integer pid);
Players findById(@Param("pid") Integer pid);
int update(Players players);
}
@Service
public class PlayersServiceImpl implements PlayersService{
@Autowired
private PlayersMapper playersMapper;
@Override
public List<Players> findAll() {
return playersMapper.findAll();
}
@Override
public int add(Players players) {
return playersMapper.add(players);
}
@Override
public int delete(Integer pid) {
return playersMapper.delete(pid);
}
@Override
public Players findById(Integer pid) {
return playersMapper.findById(pid);
}
@Override
public int update(Players players) {
return playersMapper.update(players);
}
}
public interface ClubsService {
List<Clubs> findAlls();
}
@Service
public class ClubsServiceImpl implements ClubsService {
@Autowired
private ClubsMapper clubsMapper;
@Override
public List<Clubs> findAlls() {
return clubsMapper.findAlls();
}
}
Controller层
@Controller
public class PlayersController {
@Autowired
private PlayersServiceImpl playersService;
@Autowired
private ClubsServiceImpl clubsService;
@RequestMapping("/findAll")
public String findAll(Model model){
List<Players> ListPlays = playersService.findAll();
model.addAttribute("ListPlays",ListPlays);
return "index";
}
@RequestMapping("/toadd")
public String toadd(Model model){
List<Clubs> ListClubs = clubsService.findAlls();
model.addAttribute("ListClubs",ListClubs);
return "add";
}
@RequestMapping("/add")
public String add(Players players){
int add = playersService.add(players);
if(add>0){
return "redirect:/findAll";
}else {
return "add";
}
}
@RequestMapping("/findById/{pid}")
public String findById(@PathVariable("pid") Integer pid , Model model){
Players plays = playersService.findById(pid);
model.addAttribute("plays",plays);
List<Clubs> ListClubs = clubsService.findAlls();
model.addAttribute("ListClubs",ListClubs);
return "update.html";
}
@RequestMapping("/delete/{pid}")
public String add(@PathVariable("pid") Integer pid) {
int delete = playersService.delete(pid);
if(delete>0){
return "redirect:/findAll";
}else {
return "add";
}
}
@RequestMapping("/update")
public String update(Players players){
int update = playersService.update(players);
if(update>0){
return "redirect:/findAll";
}else {
return "update";
}
}
}
第四步 thymleft页面
index界面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body class="container">
<h1>球员列表</h1>
<br/>
<div class="with:88%">
<table class="table table-hover">
<thead>
<tr>
<th>球员编号</th>
<th>球员名称</th>
<th>出生时间(yyyy-MM-dd)</th>
<th>球员身高(单位:cm)</th>
<th>球员体重(单位:kg)</th>
<th>球员位置</th>
<th>所属球队</th>
<th>相关操作</th>
</tr>
</thead>
<tbody>
<tr th:each="play : ${ListPlays}">
<th scope="row" th:text="${play.pid}"></th>
<td th:text="${play.pname}"></td>
<td th:text="${play.birthday}"></td>
<td th:text="${play.height}"></td>
<td th:text="${play.weight}"></td>
<td th:text="${play.position}"></td>
<td th:text="${play.cname}"></td>
<td >
<a th:href="@{'/findById/'+${play.pid}}">修改</a>
<a th:href="@{'/delete/'+${play.pid}}">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">
<a href="add.html" th:href="@{'/toadd'}" class="btn btn-info">增加</a>
</div>
</div>
</body>
</html>
add界面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body class="container">
<h1>新增球员信息</h1>
<br/>
<form action="/add" method="get" id="form1">
<table width="50%">
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td width="30%" >球员姓名:</td>
<td width="70%" ><input type="text" name="pname"
id="pname" /></td>
</tr>
<tr>
<td width="30%" >出生时间:</td>
<td width="70%"><input type="text" name="birthday"
id="birthday" /></td>
</tr>
<tr>
<td width="30%" >球员身高:</td>
<td width="70%" ><input type="text" name="height"
id="height" /></td>
</tr>
<tr>
<td width="30%" >球员体重:</td>
<td width="70%" ><input type="text" name="weight"
id="weight" /></td>
</tr>
<tr>
<td width="30%" >球员位置:</td>
<td width="70%" >
<input type="radio" name="position" value="控球后卫">控球后卫
<input type="radio" name="position" value="得分后卫">得分后卫
<input type="radio" name="position" value="小前锋">小前锋
<input type="radio" name="position" value="大前锋">大前锋
<input type="radio" name="position" value="中锋">中锋
</td>
</tr>
<tr>
<td width="30%" >所属球队:</td>
<td width="70%" >
<select name="cid" id="cid">
<option value="">--请选择--</option>
<option th:each="club : ${ListClubs}" th:value="${club.cid}" th:text="${club.cname}"></option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" id="btn2" value="保存"/>
<input type="reset" id="wrap-clera" value="重置"/>
<a th:href="@{/index.html}"><input type="button" id="btn1" value="返回"/></a>
</td>
</tr>
</table>
</form>
</body>
</html>
update界面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body class="container">
<h1>修改球员信息</h1>
<br/>
<form action="/update" method="get" id="form1">
<table width="50%">
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td width="30%" >id:</td>
<td width="70%" ><input type="text" name="pid"
id="pid" th:value="${plays.pid}"/></td>
</tr>
<tr>
<td width="30%" >球员姓名:</td>
<td width="70%" ><input type="text" name="pname"
id="pname" th:value="${plays.pname}" /></td>
</tr>
<tr>
<td width="30%" >出生时间:</td>
<td width="70%"><input type="text" name="birthday"
id="birthday" th:value="${plays.birthday}" /></td>
</tr>
<tr>
<td width="30%" >球员身高:</td>
<td width="70%" ><input type="text" name="height"
id="height" th:value="${plays.height}" /></td>
</tr>
<tr>
<td width="30%" >球员体重:</td>
<td width="70%" ><input type="text" name="weight"
id="weight" th:value="${plays.weight}" /></td>
</tr>
<tr>
<td width="30%" >球员位置:</td>
<td width="70%" >
<input type="radio" name="position" value="控球后卫">控球后卫
<input type="radio" name="position" value="得分后卫">得分后卫
<input type="radio" name="position" value="小前锋">小前锋
<input type="radio" name="position" value="大前锋">大前锋
<input type="radio" name="position" value="中锋">中锋
</td>
</tr>
<tr>
<td width="30%" >所属球队:</td>
<td width="70%" >
<select name="cid" id="cid">
<option value="">--请选择--</option>
<option selected="selected" th:each="club : ${ListClubs}" th:value="${club.cid}" th:text="${club.cname}"></option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" id="btn2" value="保存"/>
<input type="reset" id="wrap-clera" value="重置"/>
<a th:href="@{/index.html}"><input type="button" id="btn1" value="返回"/></a>
</td>
</tr>
</table>
</form>
</body>
</html>