博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot+MySql 登录注册
阅读量:2442 次
发布时间:2019-05-10

本文共 5785 字,大约阅读时间需要 19 分钟。

Spring Boot+MySql 登录注册

实体类:

package com.example.demo;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * 实体类 * @author linziyu * */@Entity(name="table_user")public class UserEntity {    @Id    @GeneratedValue    private Long id;    private String username;    private String password;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

数据操作:

package com.example.demo;import org.springframework.data.repository.CrudRepository;import org.springframework.stereotype.Repository;@Repositorypublic interface UserDao extends CrudRepository
{ public UserEntity findByUsernameAndPassword(String username,String password);}

控制层:

package com.example.demo;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/front/*")public class IndexController {    @Autowired    private UserDao userDao;    @RequestMapping("/index")    public String index() {        return "index";    }    @RequestMapping("/register")    public String register(){        return "register";    }    @RequestMapping("/addregister")    public String register(HttpServletRequest request){        String username = request.getParameter("username");        String password = request.getParameter("password");        String password2 = request.getParameter("password2");        if (password.equals(password2)){            UserEntity userEntity = new UserEntity();            userEntity.setUsername(username);            userEntity.setPassword(password);            userDao.save(userEntity);            return "index";        }else {            return "register";        }    }        @RequestMapping("/login")      public String login(){          return "login";      }          @RequestMapping("/addlogin")      public String login(HttpServletRequest request){          String username = request.getParameter("username");          String password = request.getParameter("password");          UserEntity userEntity = userDao.findByUsernameAndPassword(username,password);          String str = "";          if (userEntity !=null){              str = "index";          }else {              str = "login";          }          return str;      }  }

配置文件:

server.port=8082spring.datasource.url = jdbc:mysql://localhost:3306/flask1spring.datasource.username = rootspring.datasource.password = 123spring.datasource.driverClassName = com.mysql.jdbc.Driver# Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sql queryspring.jpa.show-sql = true# Hibernate ddl auto (create, create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategy\uFF01spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy# stripped before adding them to the entity manager)spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect#\u5F00\u53D1\u65F6\u5EFA\u8BAE\u5173\u95ED\u7F13\u5B58spring.thymeleaf.cache=falsespring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=utf-8spring.thymeleaf.content-type=text/html

Pom.xml:

4.0.0
com.example
login
0.0.1-SNAPSHOT
jar
login
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.7.RELEASE
UTF-8
UTF-8
1.7
org.springframework.boot
spring-boot-devtools
true
true
org.springframework.boot
spring-boot-starter
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-maven-plugin

注册页面:

    
Title

登录页面:

Insert title here
注册

转载地址:http://wxcqb.baihongyu.com/

你可能感兴趣的文章
练习3.34
查看>>
oracle加减操作
查看>>
dp乱写3:环形区间dp(数字游戏)
查看>>
【Beta阶段】启程会议——第零次Scrum Meeting!
查看>>
Apple Tree
查看>>
使用GITHub
查看>>
codeforces 456 D. A Lot of Games(字典数+博弈+思维+树形dp)
查看>>
Python3自然语言(NLTK)——语言大数据
查看>>
JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(四):自定义T4模板快速生成页面...
查看>>
百度地图的使用
查看>>
第7周进度条
查看>>
BurpSuite学习第六节--Intruder
查看>>
python基础知识
查看>>
PHP变量入门教程(1)基础
查看>>
VMware 关闭虚拟机 Ubuntu 12 的 3D 效果,提高性能
查看>>
数据库文件导出
查看>>
Python字典遍历的几种方法
查看>>
js原生实现三级联动下拉菜单
查看>>
[COGS 2066]七十和十七
查看>>
JS 中对变量类型的五种判断方法
查看>>