test库,student表,包含id,name,age,address字段。
要求:创建基于Maven的SpringBoot项目;创建基于SpringMVC+SpringData(JPA)的学生管理系统;功能:学生创建、学生信息展示、学生信息删除、学生信息修改。
(SpringData是Spring自己的ORM库; SpringData通过数据访问层的方法的名字来构建SQL语句;SpringData底层依然通过其它ORM库访问数据库,比如JPA;在application.properties文件中配置数据库的连接方式和参数。
基于 Maven 的 SpringBoot 项目并实现学生管理系统:
1. 使用 Maven 创建 SpringBoot 项目
首先,使用 Maven 创建一个新的 SpringBoot 项目。可以使用 Maven 命令行或者使用 IDE 中内置的 Maven 创建工具完成创建。
2. 添加 SpringMVC 和 SpringData 依赖
在 pom.xml 文件中添加 SpringMVC 和 SpringData 的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
上面的依赖中,SpringBoot 使用 `spring-boot-starter-web` 和 `spring-boot-starter-data-jpa` 来集成 SpringMVC 和 SpringData;使用 `h2` 来作为内存数据库存储数据。关于数据库的连接配置会在下一步中完成。
3. 配置数据库连接
在 application.properties 文件中配置数据库的连接方式和参数:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create
上述配置中,设置了使用 H2 数据库作为内存数据库,设置了数据库连接账号和密码,设置了 Hibernate 方言用于生成 SQL 语句,设置了 ddl-auto 属性来自动创建数据库表。
4. 创建实体类
创建一个名为 `Student` 的实体类,包含 `id`、`name`、`age`、`address` 四个属性:
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer age;
private String address;
// getter 和 setter 略
}
上述代码中,`@Entity` 注解表明该类为实体,在数据库中对应一张表;`@Id` 和 `@GeneratedValue` 注解表示该属性为主键,并指定自动生成方式为 `GenerationType.IDENTITY`。
5. 创建数据访问接口
创建一个名为 `StudentRepository` 的接口,并继承 `JpaRepository` 接口:
public interface StudentRepository extends JpaRepository<Student, Long> {
}
上述代码中,使用 `JpaRepository` 提供的默认方法来实现了基本的增删改查功能。
6. 创建 SpringMVC 控制器
创建一个名为 `StudentController` 的控制器类,包含学生创建、学生信息展示、学生信息删除、学生信息修改四个方法:
@RestController
@RequestMapping("/students")
public class StudentController {
private final StudentRepository studentRepository;
public StudentController(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
@GetMapping("/")
public List<Student> showAllStudents() {
return studentRepository.findAll();
}
@PostMapping("/")
public Student createStudent(@RequestBody Student student) {
return studentRepository.save(student);
}
@DeleteMapping("/{id}")
public void deleteStudent(@PathVariable Long id) {
studentRepository.deleteById(id);
}
@PutMapping("/{id}")
public Student updateStudent(@PathVariable Long id, @RequestBody Student student) {
student.setId(id);
return studentRepository.save(student);
}
}
上述代码中,使用 `@RestController` 和 `@RequestMapping` 注解对控制器进行标记,表示该控制器只处理 RESTful 风格的请求;使用 `@Autowired` 注解注入需要的依赖;实现了 showAllStudents、createStudent、deleteStudent、updateStudent 四个方法,分别用于展示所有学生、新建学生、删除学生、更新学生信息。
7. 启动程序
在 IDE 中启动程序,访问 http://localhost:8080/students/ 即可查看所有学生的信息;可以使用 POST 请求发送数据来添加新的学生,例如:
{
"name": "Tom",
"age": 18,
"address": "China"
}
也可以使用 DELETE 请求来删除某个学生的信息,例如:
DELETE http://localhost:8080/students/1
这条请求会删除 ID 为 1 的学生的信息。
最后,可以使用 PUT 请求来修改某个学生的信息,例如:
{
"id": 2,
"name": "Bob",
"age": 20,
"address": "USA"
}
这条请求会将 ID 为 2 的学生的信息更新为 Bob(姓名)、20(年龄)、USA(地址)。
通过以上步骤,可以创建了一个基于 SpringMVC+SpringData(JPA)的学生管理系统。
是搭建一个项目?你直接去找对应框架的项目,网上有很多吧,然后自己改一改看看有没有什么问题,这玩意一两句也说不清楚