Springboot+mybatis+thymeleaf实现登录注册,增删改查本⽂重在实现理解,过滤器,业务,逻辑需求,样式请⽆视。。
项⽬结构如下
1.idea新建Spring boot项⽬,在pom中加上thymeleaf和mybatis⽀持。l代码如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</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.sql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=⽤户名
spring.datasource.password=密码
spring.jpa.showSql=true
mybatis:
mybatis.mapper-locations=mybatis/*.xml
2.2在启动类上加上扫描的Dao包
package com.jz.table;
batis.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作为表名了,请注意⼀下
4.开始写代码
entity:实体代码
1.Admin实体类
package com.ity;
public class Admin {
private Integer id;
private String name;
private Integer password;
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.ity;
public class UserInfo {
private Integer id;
private String name;
private Integer age;
private String sex;
public UserInfo() {
jpa mybatis}
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.ity.Admin;
public interface AdminDao {
//登录判断
Admin login(Admin admin);
//注册
package com.jz.table.dao;
import com.ity.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中直接引⼊dao
l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.jz.table.dao.AdminDao">
<select id="login" parameterType="com.ity.Admin" resultType="com.ity.Admin">
select name,job FROM admin WHERE name = #{name} AND password = #{password}
</select>
<insert id="addAdmin" parameterType="com.ity.Admin">
INSERT INTO admin (name,password,job) VALUES (#{name},#{password},#{job});
</insert>
</mapper>
l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.jz.table.dao.UserDao">
<select id="findall" resultType="com.ity.UserInfo">
select * from userinfo
</select>
<insert id="adduser" parameterType="com.ity.UserInfo">
INSERT INTO userinfo(name,age,sex) VALUES (#{name},#{age},#{sex})
</insert>
<select id="findByid" parameterType="java.lang.Integer" resultType="com.ity.UserInfo">
SELECT * FROM userinfo where id = #{id}
</select>
<update id="updateUser" parameterType="com.ity.UserInfo">
update userinfo SET name=#{name },age =#{age},sex=#{sex} WHERE id = #{id}
</update>
<delete id="delUser" 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="">
<head>
<meta charset="UTF-8">
<title>操作成功提⽰页</title>
</head>
<body>
<h1>操作成功</h1>
<a href="/index"> 返回⾸页</a>
</body>
</html>
2.false.html
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta charset="UTF-8">
<title>操作失败提⽰页</title>
<script th:src="@{/js/jquery-1.8.0.min.js}"></script>
</head>
<body>
<h1>操作失败,请检查数据重试</h1>
<input onclick="(-1)" type="button" value="返回">
<!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 id="name" name="name" required="required"><br><br>
请输⼊密码<input id="password" name="password" type="password" placeholder="仅⽀持正整数" required="required"><br><br> <input type="submit" value="登录"> <button> <a href="/goregister">注册</a></button>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta charset="UTF-8">
<title>账号注册</title>
<script th:src="@{/js/jquery-1.8.0.min.js}"></script>
</head>
<body>
<h2>账号注册</h2>
请输⼊姓名:<input type="text" id="name"/><br><br>
请输⼊密码:<input type="password" id="password" placeholder="仅⽀持整数" /><br><br>
请确认密码:<input type="password" id="passwordTwo" placeholder="仅⽀持整数"/><br><br>
请选择⾓⾊:<select id="job" >
<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 ="/login";
} else {
alert("注册失败,请检查数据重试");
}
})
}else {
alert("两次密码不⼀致!");
}
}
</script>
</html>
@Controller
public 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);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论