第一步 :创建Spring-boot项目
第二步选择项目所需要的jar包:
第三步: 完成项目创建之后,配置 application.yml文件 (我是将application.properties 重构成 application.yml 两者功能一样的 )
application.yml代码
#指定端口号 server: port: 8888 #配置mysql数据源 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/boos?serverTimezone=Asia/Shanghai username: root password: root #配置模板引擎 thymleft 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.userboot.userTest.pojo
第四步:创建项目 所需要的 pojo层 dao(mapper)层 service层 以及 conterller层
第五步: dao层(mapper)接口(增删改查)
service层 跟dao层接口一样
实现service接口:(简单看一下代码 )
mapper 接口的sql语句:
最后就是 controller层代码实现:
最后就是新建 html文件了;
index.html代码:
<!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" border="1">
<thead>
<tr>
<th>id</th>
<th>账号</th>
<th>真实姓名</th>
<th>编号</th>
<th>性别</th>
<th>年龄</th>
<th>电话号码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="users : ${all}">
<th scope="row" th:text="${users.id}"></th>
<td th:text="${users.user}"></td>
<td th:text="${users.name}"></td>
<td th:text="${users.jiese}"></td>
<td th:text="${users.sex}"></td>
<td th:text="${users.age}"></td>
<td th:text="${users.phone}"></td>
<td >
<a th:href="@{'/getById/'+${users.id}}">修改</a>
<a th:href="@{'/delete/'+${users.id}}">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="form-group">
<div class="col-sm-2 control-label">
<a href="add.html" th:href="@{/add.html}" class="btn btn-info">add</a>
</div>
</div>
</body>
</html>
add.html 代码
<!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="user"
id="user" /></td>
</tr>
<tr>
<td width="30%" >真实姓名:</td>
<td width="70%"><input type="text" name="name"
id="name" /></td>
</tr>
<tr>
<td width="30%" >编号:</td>
<td width="70%" ><input type="text" name="jiese"
id="jiese" /></td>
</tr>
<tr>
<td width="30%" >性别:</td>
<td width="70%" ><input type="text" name="sex"
id="sex" /></td>
</tr>
<tr>
<td width="30%" >年龄:</td>
<td width="70%" ><input type="text" name="age"
id="age" /></td>
</tr>
<tr>
<td width="30%" >电话号码:</td>
<td width="70%" ><input type="text" name="phone"
id="phone" /></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.html代码
<!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="user"
id="user" /></td>
</tr>
<tr>
<td width="30%" >真实姓名:</td>
<td width="70%"><input type="text" name="name"
id="name" /></td>
</tr>
<tr>
<td width="30%" >编号:</td>
<td width="70%" ><input type="text" name="jiese"
id="jiese" /></td>
</tr>
<tr>
<td width="30%" >性别:</td>
<td width="70%" ><input type="text" name="sex"
id="sex" /></td>
</tr>
<tr>
<td width="30%" >年龄:</td>
<td width="70%" ><input type="text" name="age"
id="age" /></td>
</tr>
<tr>
<td width="30%" >电话号码:</td>
<td width="70%" ><input type="text" name="phone"
id="phone" /></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>
最后 展示一下成功 运行页面:
在浏览器中输入:
大概流程就是这样,可能些许地方不够细节,但是没懂的小伙伴可以私聊我哦,虽然我也是个小菜鸟。