springboot整合JPA写一个登入注册页面

  1. 创建工程 需要导入的部件
  2. application.yml配置文件
    spring:
      devtools:
        restart:
          enabled: false
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
        hikari: # springboot 2.0 整合了hikari ,据说这是目前性能最好的java数据库连接池
          username: root
          password: 123
      jpa:
        hibernate:
          ddl-auto: update  # 第一次建表create  后面用update,要不然每次重启都会新建表
        show-sql: true
    

    注意:如果mysql-connector-java用的6.0以上的,你的driver配置驱动是名字为com.mysql.cj.jdbc.Driver ,并且url:地址后面要加 ?serverTimezone=UTC

  3. pom.xml配置文件

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.xyz</groupId>
        <artifactId>springboot-jpa</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>springboot-jpa</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</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>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    

     

  4. 实体类User

    package cn.xyz.springbootjpa.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class User {
        @Id
        String username;
        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;
        }
    }
    

     需要添加@Entity注解声明为实体类 如果表名与实体类名字不一致需要使用@Table(name = "tbl_user")声明实体类对应的表名 @Id为主键声明

  5. UserDao持久层

    package cn.xyz.springbootjpa.dao;
    
    import cn.xyz.springbootjpa.entity.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface UserDao extends JpaRepository<User,Integer> {
        //查询用户通过密码和姓名
        public User getByPasswordAndUsername(String username,String password);
    
    }
    

      可以通过继承JpaRepository来实现对数据库的crud操作,如果需要添加对应的操作,只需写一个对应的抽象方法即可,但是方法命名有规范.

  6. service服务层

    package cn.xyz.springbootjpa.service;
    
    import cn.xyz.springbootjpa.dao.UserDao;
    import cn.xyz.springbootjpa.entity.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl{
        @Autowired
        UserDao userDao;
    
        public User getUser(String username, String password) {
           return userDao.getByPasswordAndUsername(username,password);
    
        }
    
        public void insertUser(User user) {
               userDao.save(user);
        }
    }
    

     

  7. UserController 控制层

    package cn.xyz.springbootjpa.controller;
    
    import cn.xyz.springbootjpa.entity.User;
    import cn.xyz.springbootjpa.service.UserServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.Map;
    
    @Controller
    public class UserController {
    
        @Autowired
        UserServiceImpl userServiceImpl;
        @RequestMapping("/login")
        public String login(){
            return "login";
        }
    
        @RequestMapping("doLogin")
        public String doLogin(User user, Map<String,Object> map){
          User user1 =  userServiceImpl.getUser(user.getPassword(),user.getUsername());
           if(user1 == null){
               map.put("msg","登入失败");
           }else{
               map.put("msg","登入成功");
           }
           return "success";
        }
        @RequestMapping("/regist")
        public String regist(){
            return "regist";
        }
    
        @RequestMapping("doRegist")
        public String doRegist(User user, Map<String,Object> map){
           userServiceImpl.insertUser(user);
             map.put("msg","注册成功");
            return "success";
        }
    }
    

     

  8. 登入注册页面login.html

    <head>
        <meta charset="UTF-8" />
        <title>springboot登录</title>
        <style type="text/css">
            ul li{
                list-style: none;
            }
        </style>
    </head>
    <body>
    <div >
        <form name="form" accept-charset="utf-8"  action="/doLogin">
            <ul >
                <li>
                    <label  class="input-tips2">用户名:</label>
                    <div class="inputOuter2">
                        <input type="text" id="username" name="username" maxlength="16" />
                    </div>
                </li>
                <li>
                    <label class="input-tips2">密码:</label>
                    <div >
                        <input type="password" id="password"  name="password" maxlength="16"/>
                    </div>
                </li>
                <li>
                    <div >
                        <input type="submit" value="登录"/>
                          <a href="/regist">注册</a>
                    </div>
                </li>
            </ul>
        </form>
    </div>
    </body>  

     注册页面regist.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
     <form name="form" accept-charset="utf-8"  action="/doRegist">
            <ul >
                <li>
                    <label  class="input-tips2">用户名:</label>
                    <div class="inputOuter2">
                        <input type="text" id="username" name="username" maxlength="16" />
                    </div>
                </li>
                <li>
                    <label class="input-tips2">密码:</label>
                    <div >
                        <input type="password" id="password"  name="password" maxlength="16"/>
                    </div>
                </li>
                <li>
                    <div >
                        <input type="submit" value="确定"/>
                     
                    </div>
                </li>
            </ul>
        </form>
    </body>
    </html>

    信息页面success.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <p th:text="${msg}"></p>
    </body>
    </html>

    注意对应的前端页面在templates中

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值