目录
文件目录层次和等级:
java--------------controller
service
pojo=entity=实体类
mapper=dao=映射层
resources--------mapper【操作数据库层】
第二依赖:
<dependencies> <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>
第三配置文件:
新建application.yml文件
druid连接池和配置文件可以分开进行配置
在resources目录下新建yml文件,用于存放数据库连接需要的一些数据,也可以不建
直接将这些数据放在application.properties文件下,不过格式得转换
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/weixin?serverTimezone=GMT%2B8&useSSL=true username: root password: zhs03171812
4,在application.properties文件中加入:
#端口号 server.port=8080 #druid数据库连接池 type=com.alibaba.druid.pool.DruidDataSource #清除缓存 spring.thymeleaf.cache=false #配置mapper mybatis.mapper-locations=classpath:mapper/*.xml
第四创建文件:
5,在pojo下的新建类UserLogin
package com.example.weixin_01.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class UserLogin { private String username; private String password; }
6,新建数据库,名为weixin,名可以随意,不过要与application.yml文件中的一致
7,建表,名为userLogin
8,mapper层
新建UserLoginMapper接口
package com.example.weixin_01.mapper; import com.example.weixin_01.pojo.UserLogin; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserLoginMapper { //查询 public List<UserLogin> queryAll(); //添加数据 public int add(UserLogin userLogin); //根据用户名查询数据 public UserLogin queryByName(String username); }
9,在resources目录下新建mapper目录,并在这个目录下新建UserLoginMapper.xml文件,并在application.properties中注册该组件,前面已经注册
<?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.example.weixin_01.mapper.UserLoginMapper"> <select id="queryAll" resultType="com.example.weixin_01.pojo.UserLogin"> select * from userLogin </select> <insert id="add" parameterType="com.example.weixin_01.pojo.UserLogin"> insert into userLogin values (#{username},#{password}) </insert> <select id="queryByName" resultType="com.example.weixin_01.pojo.UserLogin"> select * from userLogin where username = #{username} </select> </mapper>
10,测试
在test文件中,先测试是否能联通数据库,可以自己在数据库中插入一个数据,然后测试时查询全部,看看能否将自己插入的数据查询出来,如果可以,则数据库连接这部分就没问题
package com.example.weixin_01; import com.example.weixin_01.mapper.UserLoginMapper; import com.example.weixin_01.pojo.UserLogin; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; import java.util.List; @SpringBootTest class Weixin01ApplicationTests { @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)); } }
11,services层
在services下新建接口UserLoginServicesI和类UserLoginServicesImpl
UserLoginServicesI接口:
package com.example.weixin_01.services; import com.example.weixin_01.pojo.UserLogin; import java.util.List; public interface UserLoginServicesI { //查询 public List<UserLogin> queryAll(); //添加数据 public int add(UserLogin userLogin); //根据用户名查询数据 public UserLogin queryByName(String username); }
UserLoginServicesImpl类
package com.example.weixin_01.services; import com.example.weixin_01.mapper.UserLoginMapper; import com.example.weixin_01.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); } }
12,conteoller层
编写MyController类
package com.example.weixin_01.controller; import com.example.weixin_01.pojo.UserLogin; import com.example.weixin_01.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"; } }
13,编写前端页面
将这些页面放在templates下面
随便编写了一下,没有用bootstrap美化一下。。
1,login.html:登录页面
<!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>
2,register.html:注册页面
<!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>
3,success.html:成功界面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>success</h1> </body> </html>
14,运行一下
网页输入:localhost:8080/toLogin
大概就这样。。。
15,输入用户名和密码
输入用户名和密码,点击登录,如果数据库中存在该账号,则跳转到success页面,
这里就实现了数据的查询,通过username方式查询
如果账号不存在,则显示用户名不存在,请先注册
16,注册
点击注册进入注册页面,这里主要实现了数据的增加
点击注册后
17,验证
查看数据库中的数据
张三已经注册成功,当然也可以实现登录了!