先看一下项目结构
这是打开后的页面效果

首先application.properties的配置
server.port=8012 spring.datasource.url=jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=xxxx spring.datasource.password=xxxx spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 mybatis.mapper-locations= classpath:mapper/*.xml spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8 spring.thymeleaf.prefix=classpath:/templates/
讯享网
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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.uokos.silence</groupId> <artifactId>honny</artifactId> <version>0.0.1-SNAPSHOT</version> <name>honny</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-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--引入thymeleaf依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.jdom</groupId> <artifactId>jdom</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </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> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-core</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Controller控制层
package com.uokos.silence.honny.controller; import com.uokos.silence.honny.dao.FunUsers; import com.uokos.silence.honny.service.IFunUsersService; import com.uokos.silence.honny.service.impl.FunUsersImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.Date; import java.util.List; @Controller @RequestMapping("/funUser") public class FunUserController { @Autowired private IFunUsersService iFunUsers; private final Logger logger = LoggerFactory.getLogger(FunUsersImpl.class); @GetMapping("/all") public String all(Model model) { List<FunUsers> users = iFunUsers.getAll(); model.addAttribute("users", users); return "user/allUser"; } @GetMapping("/getOne") public String getOne(Integer id){ FunUsers usr = iFunUsers.selectByPrimaryKey(id); return usr.toString(); } / * @Description: 根据id 删除 */ @RequestMapping(value = "delete/{userId}") public ModelAndView delete(@PathVariable Integer userId) { iFunUsers.deleteByPrimaryKey(userId); ModelAndView mav = new ModelAndView("redirect:/funUser/all"); return mav; } / * 添加用户 * @param funUsers * @return */ @RequestMapping("addUser") public ModelAndView AddUser(FunUsers funUsers) { funUsers.setAddTime(new Date()); iFunUsers.insertSelective(funUsers); ModelAndView mav = new ModelAndView("redirect:/funUser/all"); return mav; } @RequestMapping("add") public ModelAndView Add1() { return new ModelAndView("/user/add"); } @RequestMapping("edit") public String edit(Integer id ,Model model){ FunUsers userInfo = iFunUsers.selectByPrimaryKey(id); model.addAttribute("userInfo",userInfo); return "user/edit"; } / * 更新用户 * @param * @return */ @RequestMapping("updateUsers") public ModelAndView updateUser(FunUsers users) { users.setUpdateTime(new Date()); iFunUsers.updateByPrimaryKey(users); ModelAndView mav = new ModelAndView("redirect:/funUser/all"); return mav; } }
DAO层(我这边可能字段有点多)
讯享网package com.uokos.silence.honny.dao; import jdk.nashorn.internal.runtime.logging.Logger; import lombok.Data; import lombok.Getter; import lombok.Setter; import java.io.Serializable; import java.util.Date; @Data public class FunUsers implements Serializable { private Integer id; / * 用户名称 */ private String username; / * 用户密码 */ private String password; / * 1男 0女 */ private Byte gender; / * 最近一次登录时间 */ private Date lastLoginTime; / * 最近一次登录IP地址 */ private String lastLoginIp; / * 0 普通用户,1 VIP用户,2 高级VIP用户 */ private Byte userLevel; / * 用户昵称或网络名称 */ private String nickname; / * 用户手机号码 */ private String mobile; / * 用户头像图片 */ private String avatar; / * 微信登录openid */ private String weixinOpenid; / * 微信登录会话KEY */ private String sessionKey; / * 0 可用, 1 禁用, 2 注销 */ private Byte status; / * 创建时间 */ private Date addTime; / * 更新时间 */ private Date updateTime; / * 逻辑删除 */ private Boolean deleted; private static final long serialVersionUID = 1L; } }
Mapper
package com.uokos.silence.honny.mapper; import com.uokos.silence.honny.dao.FunUsers; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface FunUsersMapper { int deleteByPrimaryKey(Integer id); int insertSelective(FunUsers record); FunUsers selectByPrimaryKey(Integer id); int updateByPrimaryKey(FunUsers users); List<FunUsers> getAll(); } }
Service
讯享网package com.uokos.silence.honny.service; import com.uokos.silence.honny.dao.FunUsers; import java.util.List; public interface IFunUsersService { int deleteByPrimaryKey(Integer id); int insertSelective(FunUsers record); FunUsers selectByPrimaryKey(Integer id); int updateByPrimaryKey(FunUsers users); List<FunUsers> getAll(); }
Impl
package com.uokos.silence.honny.service.impl; import com.uokos.silence.honny.dao.FunUsers; import com.uokos.silence.honny.mapper.FunUsersMapper; import com.uokos.silence.honny.service.IFunUsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class FunUsersImpl implements IFunUsersService { @Autowired private FunUsersMapper funUsersMapper; @Override public int deleteByPrimaryKey(Integer id) { return funUsersMapper.deleteByPrimaryKey(id); } @Override public int insertSelective(FunUsers record) { return funUsersMapper.insertSelective(record); } @Override public FunUsers selectByPrimaryKey(Integer id) { return funUsersMapper.selectByPrimaryKey(id); } @Override public int updateByPrimaryKey(FunUsers users) { return funUsersMapper.updateByPrimaryKey(users); } @Override public List<FunUsers> getAll() { return funUsersMapper.getAll(); } }
Mapper.xml
(这边有点多,自己用Mybatis生成器生成就行! 源码在下面)
前端页面
add.html

讯享网<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
table {border-collapse: collapse; font-size: 14px;
width: 80%; margin: auto}
table, th, td {border: 0px solid darkslategray;padding: 10px}
.btgn{
font-family: "Microsoft YaHei UI";
font-size: 16px;
color: #255e95;
background-color: #99CCFF;
text-align: center;
}
.ccc{
font-family: "Microsoft YaHei UI";
font-size: 16px;
color: #255e95;
background-color: #ff7c76;
text-align: center;
}
tr:nth-child(2n){
background-color:#FFCC99;
}
tr:nth-child(2n+1) {
background-color: #99CC99;
}
.c{
width: 200px;
}
</style>
</head>
<body>
<span class="span" style="color: darkslategray; font-size: 30px" >添加页面!</span>
<body>
<div style="margin:122px auto; width:392px">
<form action="/funUser/addUser" method="post" class="c" th:object="${user}">
<!-- <form action="AddUser" method="get">-->
<tr><th class="ccc">用户名:</th> <input name="username"class="btn" th:value="${username}"/><tr/> <br/>
<tr><th class="btgn">密码:</th> <input name="password" class="btn"th:value="${password}"/> </tr><br/>
<tr><th class="ccc">昵称:</th> <input name="nickname"class="btn" th:value="${nickname}"/> </tr><br/>
<tr><th class="ccc">手机号:</th> <input name="mobile"class="btn" th:value="${mobile}"/> </tr><br/>
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
allUser.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
table {border-collapse: collapse; font-size: 14px;
width: 80%; margin: auto}
table, th, td {border: 0px solid darkslategray;padding: 10px}
.btgn{
font-family: "Microsoft YaHei UI";
font-size: 16px;
color: #255e95;
background-color: #99CCFF;
text-align: center;
}
tr:nth-child(2n){
background-color: #fffdf5;
}
tr:nth-child(2n+1) {
background-color: #f2fbfb;
}
</style>
</head>
<body>
<div style="text-align: center">
<span class="span" style="color: darkslategray; font-size: 30px" >用户查询</span>
<hr/>
<table class="list">
<tr>
<th class="btgn">姓名</th>
<th class="btgn">密码</th>
<th class="btgn">昵称</th>
<th class="btgn">手机号码</th>
<th class="btgn">添加时间</th>
<th class="btgn">修改时间</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.nickname}"></td>
<td th:text="${user.mobile}"></td>
<td th:text="${#dates.format(user.addTime,'yyyy-MM-dd HH:mm:ss')}"></td>
<td th:text="${#dates.format(user.updateTime,'yyyy-MM-dd HH:mm:ss')}"></td>
<!--<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30</td>-->
<!--<td th:text="${user.phone}">1</td>-->
<td><a th:href="@{'/funUser/edit?id='+${user.id}}"><input type="submit" value="修改用户"></a></td>
<td><a th:href="@{'/funUser/delete/'+${user.id}}"><input type="submit" value="删除用户"></a></td>
</tr>
<a href="add"><input type="submit" value="新增用户"></a>
</table>
</div>
</body>
</html>
edit.html
讯享网<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<style type="text/css">
table {border-collapse: collapse; font-size: 14px;
width: 80%; margin: auto}
table, th, td {border: 0px solid darkslategray;padding: 10px}
.btgn{
font-family: "Microsoft YaHei UI";
font-size: 16px;
color: #255e95;
background-color: #99CCFF;
text-align: center;
}
.ccc{
font-family: "Microsoft YaHei UI";
font-size: 16px;
color: #255e95;
background-color: #ff7c76;
text-align: center;
}
tr:nth-child(2n){
background-color:#FFCC99;
}
tr:nth-child(2n+1) {
background-color: #99CC99;
}
.c{
width: 200px;
}
</style>
</head>
<body>
<span class="span" style="color: darkslategray; font-size: 30px" >修改页面!!!!</span>
<body>
<div style="margin:122px auto; width:392px">
<form action="/funUser/updateUsers" method="post" class="c" th:value="userInfo">
<!-- <form action="AddUser" method="get">-->
<tr hidden><th class="ccc"></th> <input hidden name="id"class="btn" th:value="${userInfo.id}"/><tr/> <br/>
<tr><th class="ccc">用户名:</th> <input name="username"class="btn" th:value="${userInfo.username}"/><tr/> <br/>
<tr><th class="btgn">密码:</th> <input name="password" class="btn"th:value="${userInfo.password}"/> </tr><br/>
<tr><th class="ccc">昵称:</th> <input name="nickname"class="btn" th:value="${userInfo.nickname}"/> </tr><br/>
<tr><th class="ccc">手机号:</th> <input name="mobile"class="btn" th:value="${userInfo.mobile}"/> </tr><br/>
<button type="submit">提交啊</button>
</form>
</div>
</body>
</html>
以下是源码:不过配置的是mysql的,需要sqlServer的只需要修改application.properties以及pom.xml的依赖就行
点击下载源码
提取码:5mmd
记得点赞哟!~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/62691.html