一、搭建springboot项目
1.1、file ——> new ——> project——> spring initializr——> next——> next——> next——> finish
注意选择包依赖关系
二、springboot整合mybatis.mysql
2.1、整体结构
2.2、设置所需要的依赖
即pom.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<?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.4 . 2 </version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>springboot-web04</groupid> <artifactid>demo</artifactid> <version> 0.0 . 1 -snapshot</version> <name>demo</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-data-jdbc</artifactid> </dependency> <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>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </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-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.1 . 3 </version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version> 1.18 . 12 </version> <scope>provided</scope> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version> 1.2 . 1 </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>org.xmlunit</groupid> <artifactid>xmlunit-core</artifactid> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version> 3.4 . 6 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
2.3、设置application.yml文件与pplication.properties文件
在resources目录下新建yml文件,用于存放数据库连接需要的一些数据
1
2
3
4
5
6
|
spring: datasource: driver- class -name: com.mysql.cj.jdbc.driver url: jdbc:mysql: //localhost:3306/mybatis?servertimezone=gmt%2b8&usessl=true username: root //数据库 password: sm1208 //密码 |
在application.properties文件中加入
1
2
3
4
5
6
7
8
|
#端口号 server.port= 8080 #druid数据库连接池 type=com.alibaba.druid.pool.druiddatasource #清除缓存 spring.thymeleaf.cache= false #配置mapper mybatis.mapper-locations=classpath:mapper/*.xml |
2.4、在pojo下的新建类userlogin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package springbootweb04.demo.pojo; import lombok.allargsconstructor; import lombok.data; import lombok.noargsconstructor; @data @allargsconstructor @noargsconstructor public class userlogin { private string username; private string password; public string getusername() { return username; } } |
2.5、新建数据库,名为mybatis,创建用户表,名为userlogin,创建username、password字段
2.5.1、数据库名可以随意,不过要与application.yml文件中的一致
2.5.2、idea中连接数据库
database——> +——> data source——> mysql
2.6、mapper层
新建userloginmapper接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package springbootweb04.demo.mapper; import org.apache.ibatis.annotations.mapper; import org.springframework.stereotype.repository; import springbootweb04.demo.pojo.userlogin; import java.util.list; @mapper @repository public interface userloginmapper { //查询 public list<userlogin> queryall(); //添加数据 public int add(userlogin userlogin); //根据用户名查询数据 public userlogin querybyname(string username); } |
2.7、resources目录下的mapper目录
在resources目录下新建mapper目录,并在这个目录下新建userloginmapper.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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= "springbootweb04.demo.mapper.userloginmapper" > <select id= "queryall" resulttype= "springbootweb04.demo.pojo.userlogin" > select * from userlogin </select> <insert id= "add" parametertype= "springbootweb04.demo.pojo.userlogin" > insert into userlogin values (#{username},#{password}) </insert> <select id= "querybyname" resulttype= "springbootweb04.demo.pojo.userlogin" > select * from userlogin where username = #{username} </select> </mapper> |
2.8、测试
在test.java.springbootweb04.demo类中,测试是否能联通数据库,没有报错说明成功。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package springbootweb04.demo; import org.junit.jupiter.api.test; import org.springframework.boot.test.context.springboottest; import springbootweb04.demo.mapper.userloginmapper; import springbootweb04.demo.pojo.userlogin; import org.springframework.beans.factory.annotation.autowired; import javax.sql.datasource; import java.sql.connection; import java.sql.sqlexception; import java.util.list; @springboottest class demoapplicationtests { @autowired datasource datasource; @test void contextloads() throws sqlexception { system.out.println(datasource.getclass()); connection connection = datasource.getconnection(); system.out.println(connection); //template模板,拿来即用 connection.close(); } @autowired userloginmapper userloginmapper; @test public void totest(){ list<userlogin> userlogins = userloginmapper.queryall(); userlogins.foreach(e-> system.out.println(e)); } } |
2.9、services层
在services下新建接口userloginservicesi和类userloginservicesimpl
userloginservicesi接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package springbootweb04.demo.services; import springbootweb04.demo.pojo.userlogin; import java.util.list; public interface userloginservicesi { //查询 public list<userlogin> queryall(); //添加数据 public int add(userlogin userlogin); //根据用户名查询数据 public userlogin querybyname(string username); } |
userloginservicesimpl类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package springbootweb04.demo.services; import springbootweb04.demo.mapper.userloginmapper; import springbootweb04.demo.pojo.userlogin; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.list; @service public class userloginservicesimpl implements userloginservicesi { @autowired userloginmapper userloginmapper; @override public list<userlogin> queryall() { return userloginmapper.queryall(); } @override public int add(userlogin userlogin) { return userloginmapper.add(userlogin); } @override public userlogin querybyname(string username) { return userloginmapper.querybyname(username); } } |
2.a、conteoller层
编写mycontroller类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package springbootweb04.demo.controller; import springbootweb04.demo.pojo.userlogin; import springbootweb04.demo.services.userloginservicesimpl; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; @controller public class mycontroller { @autowired userloginservicesimpl userloginservicesimpl; @requestmapping ( "/tologin" ) public string tologin(){ return "login" ; } @requestmapping ( "/loginsuccess" ) public string loginsuccess(model model, userlogin userlogin){ //先查询看该用户名是否存在 userlogin userlogin1 = userloginservicesimpl.querybyname(userlogin.getusername()); if (userlogin1 != null ){ // 如果查询的用户不为空 system.out.println(userlogin1.tostring()); return "success" ; } else { //返回到登录页面 model.addattribute( "data" , "该用户不存在,请先注册" ); return "login" ; } } //登录界面 @requestmapping ( "/toregister" ) public string toregister(){ return "register" ; } @requestmapping ( "/registersuccess" ) public string toregistersuccess(model model,userlogin userlogin){ //将账号密码加入到数据库中 int add = userloginservicesimpl.add(userlogin); system.out.println( "数据插入成功!" ); model.addattribute( "data" , "注册成功,请登录!" ); return "login" ; } } |
三、编写前端页面
将以下三个页面放在templates下面
login.html:登录页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!doctype html> <html lang= "en" xmlns:th= "http://www.thymeleaf.org" > <head> <meta charset= "utf-8" > <title>title</title> </head> <body style= "background: aqua" > <div align= "center" > <br><br><h2>登录界面</h2><br><br> <span th:text= "${data}" style= "text-color:red;font-size: 10px" ></span> <form method= "get" action= "/loginsuccess" > 用户名:<input type= "text" name= "username" placeholder= "请输入用户名" required/><br><br> 密码:<input type= "text" name= "password" placeholder= "请输入密码" required/><br><br> <input type= "submit" value= "登录" > </form> <br> <form method= "get" action= "/toregister" > <input type= "submit" value= "注册" > </form> </div> </body> </html> |
register.html:注册页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!doctype html> <html lang= "en" xmlns:th= "http://www.thymeleaf.org" > <head> <meta charset= "utf-8" > <title>title</title> </head> <body style= "background: aqua" > <div align= "center" > <br><br>注册界面<br><br> <form method= "get" action= "/registersuccess" > 用户名:<input type= "text" name= "username" placeholder= "请输入用户名" required/><br><br> 密码:<input type= "text" name= "password" placeholder= "请输入密码" required/><br><br> 确认密码:<input type= "text" name= "password2" placeholder= "请输入密码" required/><br><br> <input type= "submit" value= "注册" > </form> </div> </body> </html> |
success.html:成功界面
1
2
3
4
5
6
7
8
9
10
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>title</title> </head> <body> </body> </html> |
四、运行测试
:localhost:8080/tologin
到此这篇关于idea下创建springboot+mybatis+mysql项目实现动态登录与注册功能的文章就介绍到这了,更多相关springboot+mybatis+mysql动态登录与注册内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_51808107/article/details/113834293