zoukankan      html  css  js  c++  java
  • 09_springboot增加jpa支持

    1.创建数据库

    create database school;
    

    2.创建表

    use school;
    create table student(
    	stuId int(16) NOT NULL AUTO_INCREMENT,
    	name varchar(30),
    	sex boolean,
    	PRIMARY KEY(stuId)
    )DEFAULT CHARSET=UTF8;
    

    3.增加数据

    insert into student values(null,'张三', 1);
    insert into student values(null,'李四', 0);
    insert into student values(null,'王五', 0);
    insert into student values(null,'赵六', 1);
    

    4.配置文件application.properties增加mysql配置

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/school?characterEncoding=UTF-8&serverTimezone=GMT
    spring.datasource.username=root
    spring.datasource.password=admin
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #自动更新表结构
    spring.jpa.properties.hibernate.hbm2ddl.auto=update
    

    5.pom.xml增加对jap和mysql支持

    <!-- mysql支持-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
    
    <!-- jpa支持-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    

    6.创建学生student实体类

    package com.example.demo_zhang.pojo;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "student")
    public class Student {
    
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "stuid")
      private long stuId;
    
      @Column(name = "name")
      private String name;
    
      @Column(name = "sex")
      private long sex;
    
    
      public long getStuId() {
        return stuId;
      }
    
      public void setStuId(long stuId) {
        this.stuId = stuId;
      }
    
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
    
      public long getSex() {
        return sex;
      }
    
      public void setSex(long sex) {
        this.sex = sex;
      }
    
    }
    

    7.StudentDAO

    package com.example.demo_zhang.dao;
    
    import com.example.demo_zhang.pojo.Student;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface StudentDAO extends JpaRepository<Student, Integer> {
    }
    

    8.StudentController

    package com.example.demo_zhang.web;
    
    import com.example.demo_zhang.dao.StudentDAO;
    import com.example.demo_zhang.pojo.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.List;
    
    @Controller
    public class StudentController {
        @Autowired StudentDAO studentDAO;
    
        @RequestMapping("/listStudent")
        public String listStudent(Model m)throws Exception{
            List<Student> stu = studentDAO.findAll();
    
    
    
            m.addAttribute("stu", stu);
            return "listStudent";
        }
    }
    
    

    9.listStudent.jsp

    <%--
      Created by IntelliJ IDEA.
      User: 10992
      Date: 2020/11/10
      Time: 9:57
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
        <table align="center" border="1", cellspacing="0">
            <tr>
                <td>stuId</td>
                <td>name</td>
                <td>sex</td>
            </tr>
            <c:forEach items="${stu}" var="s" varStatus="st">
                <tr>
                    <td>${s.stuId}</td>
                    <td>${s.name}</td>
                    <td>${s.sex}</td>
                </tr>
            </c:forEach>
        </table>
    </head>
    <body>
    </body>
    </html>
    

    10.重启测试,http://127.0.0.1:8080/listStudent

    image-20201110133827887

    11.遇到的坑

    1)mysql中字段名stuid原来写作stuId,jpa查出来都是重复的

  • 相关阅读:
    Jmeter 脚本录制
    Scrapy 爬虫模拟登陆的3种策略
    Scrapy Shell
    Ipython
    XPath helper
    python3 接口测试数据驱动之操作mysql数据库
    Pandas 基础(17)
    Pandas 基础(16)
    在 Laravel 项目中使用 Elasticsearch 做引擎,scout 全文搜索(小白出品, 绝对白话)
    Pandas 基础(15)
  • 原文地址:https://www.cnblogs.com/NaoDaiYouDianDa/p/13992421.html
Copyright © 2011-2022 走看看