Spring Boot+MySql 登录注册

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;


@Repository
public interface UserDao extends CrudRepository<UserEntity,Long>{
	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=8082

spring.datasource.url = jdbc:mysql://localhost:3306/flask1
spring.datasource.username = root
spring.datasource.password = 123
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy\uFF01
spring.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\u5B58
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=utf-8
spring.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
			
		
	


注册页面:


<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Titletitle>
head>
<body>
<div class="web_login">
    <form name="form2" id="regUser" accept-charset="utf-8"  action="/front/addregister" method="post">
        <ul class="reg_form" id="reg-ul">
            <div id="userCue" class="cue">快速注册请注意格式div>
            <li>
                <label for="username"  class="input-tips2">用户名:label>
                <div class="inputOuter2">
                    <input type="text" id="username" name="username" maxlength="16" class="inputstyle2"/>
                div>
            li>
            <li>
                <label for="password" class="input-tips2">密码:label>
                <div class="inputOuter2">
                    <input type="password" id="password"  name="password" maxlength="16" class="inputstyle2"/>
                div>
            li>
            <li>
                <label for="password2" class="input-tips2">确认密码:label>
                <div class="inputOuter2">
                    <input type="password" id="password2" name="password2" maxlength="16" class="inputstyle2" />
                div>
            li>
            <li>
                <div class="inputArea">
                    <input type="submit" id="reg"  style="margin-top:10px;margin-left:85px;" class="button_blue" value="同意协议并注册"/> <a href="#" class="zcxy" target="_blank">注册协议a>
                div>
            li><div class="cl">div>
        ul>
    form>
div>
body>
html>

登录页面:


<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title heretitle>
head>
<body>
  
      <form action="/front/addlogin" name="loginfrom" accept-charset="utf-8" method="post">  
        <label class="label-tips" for="u">账号:label>  
        <input type="text" id="u" name="username" class="inputstyle"/>  
        <div>  
            <label class="lable-tips" for="password">密码:label>  
            <input type="password" id="password" name="password" class="inputstyle" />  
        div>  
        <input type="submit" name="登录"/>  
        <a href="register.html" class="zcxy" target="_blank">注册a>  
    form>  
  
body>
html>

你可能感兴趣的:(Spring学习,spring)