springboot+jpa快速搭建web项目

1. 设计表

在这里插入图片描述

idea左面的实体类必须连接一下。[需要idea连接上数据库]
在这里插入图片描述
在这里插入图片描述

2. 实体类

package com.biturd.logintest.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @Program: login-test
 * @Description: User
 * @Author: Biturd
 * @Date: 2020-08-10 20:31
 */
@Entity
@Table(name="user")
public class User {
    @Id
    @Column(name="id")
    public String id;

    @Column(name="username")
    public String username;

    @Column(name = "password")
    public String password;

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

3. JPA接口

package com.biturd.logintest.dao;

import com.biturd.logintest.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @Program: login-test
 * @Description: UserDao
 * @Author: Biturd
 * @Date: 2020-08-10 20:31
 */
public interface UserDao extends JpaRepository<User,String> {
    User findByUsername(String username);
}

4. 上层调用数据库层

@CrossOrigin
@Controller
public class LoginController {

    @Autowired
    UserDao userDao;
}

5. 关于servlet几个东西的使用

import com.biturd.logintest.dao.UserDao;
import com.biturd.logintest.entity.User;
import com.biturd.logintest.util.DesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Program: login-test
 * @Description: LoginController
 * @Author: Biturd
 * @Date: 2020-08-10 20:31
 */
@CrossOrigin
@Controller
public class LoginController {

    @Autowired
    UserDao userDao;

    @RequestMapping(value = "index",method = RequestMethod.GET)
    public String others(HttpSession session, HttpServletRequest request) {
        Object sessionUser = session.getAttribute("username");
        if (sessionUser == null) {
            return "redirect:/my_login.html";
        }
        return "user_home";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(User user, HttpSession session, HttpServletRequest request, HttpServletResponse resp) throws Exception {
        String password = null;
        User login = userDao.findByUsername(user.username);
        if (login != null) {  // 要不会 报异常
            password = login.password; // 从数据库取出的密码
        }

//        String try_password = user.password;  // 用户表单输入的密码
        System.out.println("username"+ user.username +"表单加密后的密码" + user.password);
        String try_password = DesUtils.decrypt(user.password);  // 用户表单输入的密码
        System.out.println("username"+ user.username+"表单解密后的密码" + try_password);
        if (!try_password.equals(password)) {  // 密码错误
            return "404";
        }
        Cookie unameCookie = new Cookie("username", login.username);
        resp.addCookie(unameCookie);
        session.setAttribute("username", user.username);
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
        return "user_home";
    }

    // 用于测试数据库连接
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test() {
        System.out.println(userDao.findByUsername("123").toString());
        return "redirect:/my_login.html";
    }
}

<?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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.biturd</groupId>
    <artifactId>login-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>login-test</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页