电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼

瀏覽:4日期:2023-08-27 11:31:31

本文重在實現理解,過濾器,業務,邏輯需求,樣式請無視。。

項目結構如下

Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼

1.idea新建Spring boot項目,在pom中加上thymeleaf和mybatis支持。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 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.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jz</groupId> <artifactId>table</artifactId> <version>0.0.1-SNAPSHOT</version> <name>table</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.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

2. 在項目resources目錄下新建mybatis文件夾,用于存放mybatis配置文件。 在 application.properties 中配置本地數據源和mybatis配置文件地址, application.properties代碼如下

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTCspring.datasource.username=用戶名spring.datasource.password=密碼spring.jpa.showSql=truemybatis: mybatis.type-aliases-package=com.jz.table.entity mybatis.mapper-locations=mybatis/*.xml

com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的,需要指定時區serverTimezone

2.2在啟動類上加上掃描的Dao包

package com.jz.table;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan('com.jz.table.dao')public class TableApplication { public static void main(String[] args) { SpringApplication.run(TableApplication.class, args); }}

3.數據庫建兩個表admin和userinfo用于登錄和操作用

2019.10.3 現在mysql不能用admin作為表名了,請注意一下

Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼

Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼

4.開始寫代碼

entity:實體代碼

1.Admin實體類

package com.jz.table.entity;public class Admin { private Integer id; private String name; private Integer password; private String job; public Admin() { } public Admin(Integer id, String name, Integer password, String job) { this.id = id; this.name = name; this.password = password; this.job = job; } 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 getPassword() { return password; } public void setPassword(Integer password) { this.password = password; } public String getJob() { return job; } public void setJob(String job) { this.job = job; }}

2.UserInfo實體類

package com.jz.table.entity;public class UserInfo { private Integer id; private String name; private Integer age; private String sex; public UserInfo() { } public UserInfo(Integer id, String name, Integer age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}

Dao層代碼1.AdminDao

package com.jz.table.dao;import com.jz.table.entity.Admin;public interface AdminDao { //登錄判斷 Admin login(Admin admin); //注冊 int addAdmin(Admin admin);}

2.UserDao

package com.jz.table.dao;import com.jz.table.entity.UserInfo;import java.util.List;public interface UserDao { //查 List<UserInfo> findall(); //增 int adduser(UserInfo user); //根據Id查,用于修改時頁面回顯數據 UserInfo findByid(Integer id); //修改 int updateUser(UserInfo user); //刪除 int delUser(Integer id);}

3.XML文件,因為沒有業務邏輯,service省了,controller中直接引入dao1.AdminMapper.xml

<?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.jz.table.dao.AdminDao'> <select parameterType='com.jz.table.entity.Admin' resultType='com.jz.table.entity.Admin'> select name,job FROM admin WHERE name = #{name} AND password = #{password} </select> <insert parameterType='com.jz.table.entity.Admin'> INSERT INTO admin (name,password,job) VALUES (#{name},#{password},#{job}); </insert></mapper>

2.UserMapper.xml

<?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.jz.table.dao.UserDao'> <select resultType='com.jz.table.entity.UserInfo'> select * from userinfo </select> <insert parameterType='com.jz.table.entity.UserInfo'> INSERT INTO userinfo(name,age,sex) VALUES (#{name},#{age},#{sex}) </insert> <select parameterType='java.lang.Integer' resultType='com.jz.table.entity.UserInfo'> SELECT * FROM userinfo where id = #{id} </select> <update parameterType='com.jz.table.entity.UserInfo'> update userinfo SET name=#{name },age =#{age},sex=#{sex} WHERE id = #{id} </update> <delete parameterType='java.lang.Integer'> DELETE from userinfo WHERE id = #{id} </delete></mapper>

4.頁面,在templates文件夾下新建public和user文件夾用來存放公共頁面和user操作頁面public文件夾下新建成功、失敗提示頁1.success.html

<!DOCTYPE html><!--引入thymeleaf--><html lang='en' xmlns:th='http://www.thymeleaf.org'> <head> <meta charset='UTF-8'> <title>操作成功提示頁</title></head><body><h1>操作成功</h1><a href='http://www.hdgsjgj.cn/index' rel='external nofollow' rel='external nofollow' > 返回首頁</a></body></html>

2.false.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <title>操作失敗提示頁</title> <script th:src='http://www.hdgsjgj.cn/bcjs/@{/js/jquery-1.8.0.min.js}'></script></head><body><h1>操作失敗,請檢查數據重試</h1><input onclick='history.go(-1)' type='button' value='返回'></body></html>

4.2在templates文件夾下新建login和register頁面作為登錄和注冊頁面1.login.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>首頁</title> <style> /*a標簽去下劃線和點擊不變色,div內容居中*/ a{ text-decoration: none; color: #333333; } #idiv{text-align: center;border-radius: 20px; width: 300px; height: 350px; margin: auto; position: absolute; top: 0; left: 0; right: 0; bottom: 0;} </style></head><body><div id='idiv'> <form action='/gologin' method='post'> 請輸入姓名<input name='name' required='required'><br><br> 請輸入密碼<input name='password' type='password' placeholder='僅支持正整數' required='required'><br><br> <input type='submit' value='登錄'> <button> <a href='http://www.hdgsjgj.cn/goregister' rel='external nofollow' >注冊</a></button> </form></div></body></html>

2.register.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <title>賬號注冊</title> <script th:src='http://www.hdgsjgj.cn/bcjs/@{/js/jquery-1.8.0.min.js}'></script></head><body><h2>賬號注冊</h2>請輸入姓名:<input type='text' /><br><br>請輸入密碼:<input type='password' placeholder='僅支持整數' /><br><br>請確認密碼:<input type='password' placeholder='僅支持整數'/><br><br>請選擇角色:<select style='width: 173px'> <option value='管理員'>管理員</option> </select><br><br><button onclick='register()'>注冊</button></body><script> function register() { var name = $('#name').val(); var password1 = $('#password').val(); var password2 = $('#passwordTwo').val(); var job = $('#job').val(); if (Number(password1) == Number(password2)){ $.post('/register',{name:name,password:password1,job:job},function (res) { if (res ==true){ alert('注冊成功'); window.location.href ='http://www.hdgsjgj.cn/login'; } else { alert('注冊失敗,請檢查數據重試'); } }) }else { alert('兩次密碼不一致!'); } }</script></html>

3.controller中代碼

@Controllerpublic class TestController { @Resource private AdminDao ad; @Resource private UserDao ud; @RequestMapping('/login')//主頁 public String index(){ return 'login'; } @RequestMapping('/goregister')//去注冊頁面 public String goregister(){ return 'register'; } @RequestMapping('/register')//注冊 @ResponseBody public boolean register(Admin admin){ int i = ad.addAdmin(admin); if (i>0){ return true; }else { return false; } } @RequestMapping('/gologin')//登錄獲取用戶信息存到seccion public String gologin(Admin admin,HttpServletRequest request,Model model){ Admin aa = ad.login(admin); if (aa==null){ return 'public/false'; } HttpSession session = request.getSession(); session.setAttribute('aname',admin.getName()); session.setAttribute('apassword',admin.getPassword()); List<UserInfo> userlist = ud.findall(); model.addAttribute('admin',aa); model.addAttribute('alist',userlist); return 'user/index'; } @RequestMapping('/index')//從其他頁面操作后返回列表頁面(重復登錄) public String login(Admin admin,Model model,HttpServletRequest request){ HttpSession session = request.getSession(); admin.setName((String) session.getAttribute('aname')); admin.setPassword((Integer) session.getAttribute('apassword')); Admin aa = ad.login(admin); List<UserInfo> userlist = ud.findall(); model.addAttribute('admin',aa); model.addAttribute('alist',userlist); return 'user/index'; }}

4.3user文件夾下新建index,addUser,updateUser頁面,作為主頁,添加,修改頁面

1.index.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <title>首頁</title> <style>a{text-decoration:none}</style> <script th:src='http://www.hdgsjgj.cn/bcjs/@{/js/jquery-1.8.0.min.js}'></script></head><body>歡迎你 :<input th:value='${admin.name}' /><br><br><br><br><h2>人員信息維護</h2><table border='1'> <thead> <tr> <th>id</th> <th>姓名</th> <th>年齡</th> <th>性別</th> <th>操作</th> </tr> <tr th:each='user:${alist}'> <td th:text='${user.id}'></td> <td th:text='${user.name}'></td> <td th:text='${user.age}'></td> <td th:text='${user.sex}'></td> <td align='center'><a th:href='http://www.hdgsjgj.cn/bcjs/@{’/goupdate/’+${user.id}}' rel='external nofollow' >修改</a> <a th:href='http://www.hdgsjgj.cn/bcjs/@{’/godel/’+${user.id}}' rel='external nofollow' >刪除</a> </td> </tr> </thead></table><button><a href='http://www.hdgsjgj.cn/goadd' rel='external nofollow' >添加</a></button></body></html>

2.addUser.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <title>添加用戶</title> <script th:src='http://www.hdgsjgj.cn/bcjs/@{/js/jquery-1.8.0.min.js}'></script></head><body><h2>我是添加頁面</h2>請輸入姓名:<input name='name' type='text' /><br><br>請輸入年齡:<input name='age' type='text' /><br><br>請選擇性別:<select name='sex' style='width: 173px'> <option value='男'>男</option> <option value='女'>女</option> </select><br><br><button onclick='goadd()' name='sub' id='sub'>添加</button><button name='button' onclick='javascript:history.back(-1);'>返回</button></body><script> function goadd() { var name = $('#name').val(); var age = $('#age').val(); var sex = $('#sex').val(); $.post('/addUser',{name:name,age:age,sex:sex},function (res) { if (res==true){ alert('添加成功') window.location.href ='http://www.hdgsjgj.cn/index'; }else { alert('添加失敗,請檢查數據重試!'); } }) }</script></html>

3.updateUser.html

<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <title>修改用戶</title> <script th:src='http://www.hdgsjgj.cn/bcjs/@{/js/jquery-1.8.0.min.js}'></script></head><body><h2>這是修改頁面</h2><input type='hidden' th:value='${user.id}'><br><br>請輸入姓名:<input th:value='${user.name}'/><br><br>請輸入年齡:<input th:value='${user.age}'/><br><br>請選擇性別:<select style='width: 173px'> <option value='男'>男</option> <option value='女'>女</option> </select><br><br><button onclick='goupdate()'>修改</button> <button name='button' onclick='javascript:history.back(-1);'>返回</button></body><script> function goupdate() { var id=$('#id').val(); var name = $('#name').val(); var age = $('#age').val(); var sex = $('#sex').val(); $.post('/update',{id:id,name:name,age:age,sex:sex},function (res) { if (res==true) { alert('修改成功'); window.location.href='http://www.hdgsjgj.cn/index' rel='external nofollow' rel='external nofollow' ; }else { alert('修改失敗,請檢查數據重試!'); } }) }</script></html>

4.controller中代碼

@RequestMapping('/goadd')//去添加頁面public String goadd(){ return 'user/addUser';}@RequestMapping('/addUser')//添加信息@ResponseBodypublic boolean addUser(UserInfo user){ int i = ud.adduser(user); if (i>0){ return true; }else { return false; }}@RequestMapping('/goupdate/{id}')//去修改頁面,回顯數據public String goupdate(@PathVariable('id') int id,Model model){ UserInfo user = ud.findByid(id); model.addAttribute('user',user); return 'user/updateUser';}@RequestMapping('/update')//修改@ResponseBodypublic boolean updateUser(UserInfo user){ int i = ud.updateUser(user); if (i>0){ return true; }else { return false; }}@RequestMapping('/godel/{id}')//刪除public String delUser(@PathVariable('id') Integer id){ ud.delUser(id); return 'public/success';}

5.完整controller代碼

package com.jz.table.controller;import com.jz.table.dao.AdminDao;import com.jz.table.dao.UserDao;import com.jz.table.entity.Admin;import com.jz.table.entity.UserInfo;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.util.List;@Controllerpublic class TestController { @Resource private AdminDao ad; @Resource private UserDao ud; @RequestMapping('/login')//主頁 public String index(){ return 'login'; } @RequestMapping('/goregister')//去注冊頁面 public String goregister(){ return 'register'; } @RequestMapping('/register')//注冊 @ResponseBody public boolean register(Admin admin){ int i = ad.addAdmin(admin); if (i>0){ return true; }else { return false; } } @RequestMapping('/gologin')//登錄獲取用戶信息存到seccion public String gologin(Admin admin,HttpServletRequest request,Model model){ Admin aa = ad.login(admin); if (aa==null){ return 'public/false'; } HttpSession session = request.getSession(); session.setAttribute('aname',admin.getName()); session.setAttribute('apassword',admin.getPassword()); List<UserInfo> userlist = ud.findall(); model.addAttribute('admin',aa); model.addAttribute('alist',userlist); return 'user/index'; } @RequestMapping('/index')//從其他頁面操作后返回列表頁面(重復登錄) public String login(Admin admin,Model model,HttpServletRequest request){ HttpSession session = request.getSession(); admin.setName((String) session.getAttribute('aname')); admin.setPassword((Integer) session.getAttribute('apassword')); Admin aa = ad.login(admin); List<UserInfo> userlist = ud.findall(); model.addAttribute('admin',aa); model.addAttribute('alist',userlist); return 'user/index'; } @RequestMapping('/goadd')//去添加頁面 public String goadd(){ return 'user/addUser'; } @RequestMapping('/addUser')//添加信息 @ResponseBody public boolean addUser(UserInfo user){ int i = ud.adduser(user); if (i>0){ return true; }else { return false; } } @RequestMapping('/goupdate/{id}')//去修改頁面,回顯數據 public String goupdate(@PathVariable('id') int id,Model model){ UserInfo user = ud.findByid(id); model.addAttribute('user',user); return 'user/updateUser'; } @RequestMapping('/update')//修改 @ResponseBody public boolean updateUser(UserInfo user){ int i = ud.updateUser(user); if (i>0){ return true; }else { return false; } } @RequestMapping('/godel/{id}')//刪除 public String delUser(@PathVariable('id') Integer id){ ud.delUser(id); return 'public/success'; } }

效果如圖

Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼

到此這篇關于Spring boot+mybatis+thymeleaf 實現登錄注冊增刪改查功能的示例代碼的文章就介紹到這了,更多相關Spring boot mybatis thymeleaf 登錄注冊增刪改查內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 儿童乐园|游乐场|淘气堡招商加盟|室内儿童游乐园配套设备|生产厂家|开心哈乐儿童乐园 | LED显示屏_LED屏方案设计精准报价专业安装丨四川诺显科技 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 | 铸铁平台,大理石平台专业生产厂家_河北-北重机械 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 考勤系统_考勤管理系统_网络考勤软件_政企|集团|工厂复杂考勤工时统计排班管理系统_天时考勤 | 台式核磁共振仪,玻璃软化点测定仪,旋转高温粘度计,测温锥和测温块-上海麟文仪器 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 带压开孔_带压堵漏_带压封堵-菏泽金升管道工程有限公司 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 杭州火蝠电商_京东代运营_拼多多全托管代运营【天猫代运营】 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 智慧农业|农业物联网|现代农业物联网-托普云农物联网官方网站 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 无水硫酸铝,硫酸铝厂家-淄博双赢新材料科技有限公司 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 金属雕花板_厂家直销_价格低-山东慧诚建筑材料有限公司 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 泡沫消防车_水罐消防车_湖北江南专用特种汽车有限公司 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 新中天检测有限公司青岛分公司-山东|菏泽|济南|潍坊|泰安防雷检测验收 | 液压油缸-液压缸厂家价格,液压站系统-山东国立液压制造有限公司 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 煤粉取样器-射油器-便携式等速飞灰取样器-连灵动 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 |