最近本人有一门实验课数据库设计实验,与同组人讨论决定采用springboot+vue框架来实现,因此开始学习springboot,给自己的学习历程做一些笔记。
pom.xml配置如下,引入依赖项,idea使用maven依赖项感觉还是容易的
<?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.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</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-data-rest</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--用于编译jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.5</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
</project>
然后是配置application.properties文件
#Mysql属性配置文件,Spring-boot系统配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=152233
#配置自动建表:updata:没有表新建,有表更新操作,控制台显示建表语句
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp
#静态文件访问配置
spring.mvc.static-path-pattern=/static/**
配置完就可以开始学习了
当然还要了解到springboot的项目结构
首先是启动类,放于源码目录下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
创建实体目录,在其目录下创建实体
设置表的名称和主键等,这里的主键是stdnum,采用的是自增长的方式(现在还没了解该如何自定义主键,在之后学习中应该会学到)
package com.entity;
import javax.persistence.*;
@Entity
@Table(name = "ss")
public class person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer stdnum;
@Column(length = 10)
private String stdname;
public person(){}
public Integer getStdnum(){
return stdnum;
}
public String getStdname(){
return stdname;
}
public void setStdname(String stdname){
this.stdname = stdname;
}
public void setStdnum(Integer stdnum){
this.stdnum = stdnum;
}
}
Dao层主要是用于与数据库进行交互。
网上介绍如下:DAO层会调用entity层,DAO中会定义实际使用到的方法,比如增删改查。DAO 层的数据源和数据库连接的参数都是在配置文件中进行配置的,配置文件一般在同层的XML文件夹中。数据持久化操作就是指,把数据放到持久化的介质中,同时提供增删改查操作。
定义如下:
public interface persondao extends JpaRepository<person,Integer>{
}
然后在service层下完成逻辑实现
package com.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.jdbc.core.JdbcTemplate;
import com.Dao.persondao;
import com.entity.person;
import java.util.List;
import java.util.Optional;
@Service
public class personservice {
@Autowired
private persondao pd;
public void addperson(person person){
pd.save(person);
}
public void remove(Integer id){
pd.deleteById(id);
}
public void update(person person){
pd.save(person);
}
public Optional<person> getbyid(Integer id){
return pd.findById(id);
}
public Iterable<person> getlist(){
return pd.findAll();
}
}
试着在Test类中测试
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
class DemoApplicationTests {
@Autowired
private persondao pd;
@Test
public void testadd() {
person person = new person();
person.setStdnum(1);
person.setStdname("sss");
person re = pd.save(person);
System.out.println("创建学生成功,学生名为"+re.getStdname());
System.out.println("hellp");
}
@Test
public void getlist() {
Iterable<person> p= pd.findAll();
for(person tmp : p) {
System.out.println(tmp.getStdnum().toString() + ":" + tmp.getStdname() );
}
}
@Test
public void getlistbyid() {
Optional<person> p = pd.findById(5);
person tmp = p.get();
System.out.println(tmp.getStdnum()+":"+tmp.getStdname());
}
@Test
public void update() {
person person = new person();
person.setStdname("lyl");
person re = pd.save(person);
}
@Test
public void delete() {
pd.deleteById(4);
pd.deleteById(5);
System.out.println("删除成功");
}
@Test
void contextLoads() {
}
}
controller层负责前后端交互,接受前端请求,调用service层,接收service层返回的数据,最后返回具体的页面和数据到客户端。
写一个controller来实现前端显示数据库内容
@Controller
public class hello {
@Autowired
private persondao info;
@RequestMapping("/input")
@ResponseBody
public List queryAll() {
return info.findAll();
}
@RequestMapping("/showtables")
public String hello(Model model) {
model.addAttribute("info",queryAll());
return "show-tables";
}
}
html页面代码:
<!DOCTYPE html>
<html lang="en" xmln="http//www.thymeleaf.org" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</div>
<div class="row">
<div class="col-md-12">
<div class="alert alert-success">
<table border="2" width="200" align="center">
<tr>
<td>学生学号</td>
<td>学生姓名</td>
</tr>
<tr th:each ="i:${info}">
<td th:text = "${i.stdnum}"></td>
<td th:text = "${i.stdname}"></td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
结果如下: