上一篇博客写了如何初始化一个简单的Spring Boot项目,这次详细记录一下如何连接数据库并实现增删改查基本操作。
我使用的是MySQL 5.5+Navicat,MySQL量级比较轻,当然微软的SQL Server也挺好的。
默认生成的项目会带有一个application.properties供用户来配置相关信息,这里可以将其修改为application.yml,你问为什么?
yml文件天然树状结构,便于阅读;需要写的东西比properties少得多。
不足之处就是冒号后面要加空格,否则会报错,而且缩进很严格!!!
详细信息请阅读下面的参考资料。
//application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mydbsys
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
3306是MySQL的端口,mydbsys是数据库名称,数据库肯定是要存在的,不存在还怎么连接。
和Java语言很相似,属性、set、get内容写进去就行,有几点JPA的注解要注意一下。
@ManyToOne指的是多个学生对应一个班级,其他常用注解看参考资料。
//Grade.java
package com.studentsys.stusys.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Grade {
@Id
@GeneratedValue
private Integer GID;
private String GrandName;
public Grade() {
}
public Integer getGID() {
return GID;
}
public void setGID(Integer GID) {
this.GID = GID;
}
public String getGrandName() {
return GrandName;
}
public void setGrandName(String grandName) {
GrandName = grandName;
}
}
//Student.java
package com.studentsys.stusys.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
@Id
@GeneratedValue
private Integer stuID;
private String name;
private String phone;
private String password;
//private Integer GID;
@ManyToOne
private Grade grade;
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public Student() {
}
public Integer getStuID() {
return stuID;
}
public void setStuID(Integer stuID) {
this.stuID = stuID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Student(Integer stuID, String name, String phone, String password) {
this.stuID = stuID;
this.name = name;
this.phone = phone;
this.password = password;
}
}
继承一下JPA的库就可以,很多东西都已经定义好了,有特殊需要再利用@Override重写父类函数就可以了。
//GradeRepository.java
package com.studentsys.stusys.repository;
import com.studentsys.stusys.entity.Grade;
import org.springframework.data.jpa.repository.JpaRepository;
public interface GradeRepository extends JpaRepository {
}
//StudentRepository.java
package com.studentsys.stusys.repository;
import com.studentsys.stusys.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository {
}
(别跟我学,一定要把数据服务层接口service和实现接口serviceImpl分开,将项目模块化)
@Autowired
//GradeService.java
package com.studentsys.stusys.service;
import com.studentsys.stusys.entity.Grade;
import java.util.List;
import java.util.Optional;
public interface GradeService {
List getAllGrade();
Optional getGradeByID(Integer gid);
}
//GradeServiceImpl.java
package com.studentsys.stusys.service;
import com.studentsys.stusys.entity.Grade;
import com.studentsys.stusys.repository.GradeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class GradeServiceImpl implements GradeService {
@Autowired
private GradeRepository gradeRepository;
@Override
public List getAllGrade() {
return gradeRepository.findAll();
}
@Override
public Optional getGradeByID(Integer gid) {
return gradeRepository.findById(gid);
}
}
//StudentService.java
package com.studentsys.stusys.service;
import com.studentsys.stusys.entity.Student;
import java.util.List;
import java.util.Optional;
public interface StudentService {
List getAllStudent();
Student addStudent(Student student);
Optional findByID(Integer id);
Student updateStudent(Student student);
}
//StudentServiceImpl.java
package com.studentsys.stusys.service;
import com.studentsys.stusys.entity.Student;
import com.studentsys.stusys.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public List getAllStudent() {
return studentRepository.findAll();
}
@Override
public Student addStudent(Student student) {
return studentRepository.save(student);
}
@Override
public Optional findByID(Integer id) {
return studentRepository.findById(id);
}
@Override
public Student updateStudent(Student student) {
return studentRepository.save(student);
}
}
//StudentController.java
package com.studentsys.stusys.cotroller;
import com.studentsys.stusys.entity.Grade;
import com.studentsys.stusys.entity.Student;
import com.studentsys.stusys.service.GradeService;
import com.studentsys.stusys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/")
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private GradeService gradeService;
/**
* 查看所有学生信息
* @param model
* @return
*/
@GetMapping
public String index(Model model){
List list = studentService.getAllStudent();
model.addAttribute("stus",list);
return "index";
}
/**
* 添加学生信息功能
* @param name
* @param password
* @param phone
* @return
*/
@PostMapping("/add")
public String input(Student student){
Optional grade = gradeService.getGradeByID(student.getGrade().getGID()); ;
student.setGrade(grade.get());
studentService.addStudent(student);
return "redirect:/";
}
@GetMapping("/ks")
public String ks(){
return "KSbaoM";
}
/**
* 显示添加页面
* @return
*/
@GetMapping("/add")
public String addPage(Model model){
model.addAttribute("allGrade",gradeService.getAllGrade());
return "addstu";
}
@GetMapping("/update/{id}")
public String updatePage(@PathVariable("id") Integer id,Model model){
Optional student = studentService.findByID(id);
model.addAttribute("stu",student.get());
model.addAttribute("allGrade",gradeService.getAllGrade());
return "updatestu";
}
/**
* 修改学生信息
* @param stuID
* @param name
* @param password
* @param phone
* @return String
*/
@PostMapping("/update")
public String updateData(Student student){
Optional grade = gradeService.getGradeByID(student.getGrade().getGID()); ;
student.setGrade(grade.get());
studentService.addStudent(student);
return "redirect:/";
}
}
懒得写了,这种静态页面找个模版随便改改就ok了,主要是要注意themeleaf的使用。
Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
首先在最开始的标签里要这样写<html xmlns:th="http://www.thymeleaf.org">,之后就是有一些标签需要再修改,具体的可以看参考资料。
参考资料:
application.properties与application.yml之间的区别
JPA之常用 基本注解
(一)Themeleaf用法——Themeleaf简介
themeleaf基本语法
posted @ 2018-07-07 08:56 朕蹲厕唱忐忑 阅读(...) 评论(...) 编辑 收藏