现在mysql里建了一个数据库毕业设计资料管理系统bs里面有如下表 学生表 create table student (sno varchar(10) primary key,sname varchar(20),spassword varbinary(20) not null); 管理员表 create table manager (mno varchar(10) primary key,mname varchar(20),mpassword varbinary(20) not null); 学生毕业设计表 create table gradu_design (gid int(10) AUTO_INCREMENT primary key,sno varchar(10),job_book TEXT,open_report TEXT,interim_report TEXT,week_report TEXT,gradu_paper TEXT,comment TEXT,FOREIGN KEY(sno) REFERENCES student(sno))AUTO_INCREMENT = 1; 管理员任务发布表 create table task (tid int(10) AUTO_INCREMENT primary key,mno int(10),title TEXT,content TEXT,subtime date,FOREIGN KEY(mno) REFERENCES manager(mno))AUTO_INCREMENT = 1; 请用idea里entity包中编写相应实体类和在dao包、bean包编写相应的东西,主要想实现以下功能管理员能发布任务,能查看学生毕设里的所有如(job_book、开题报告open_report、中期报告interim_report、周报week_report、毕业论文gradu_paper、文献翻译document),管理员有能对学生类实现增删改查的权限
时间: 2024-02-03 09:02:17 浏览: 104
学生成绩管理系统-数据库设计资料.pdf
首先,在 IDEA 中创建一个 Maven 项目,并在 pom.xml 文件中添加以下依赖项:
```xml
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```
然后,在 `src/main/java` 目录下创建以下包:
- `com.example.bs.entity`:用于存放实体类。
- `com.example.bs.dao`:用于存放数据访问层(DAO)接口。
- `com.example.bs.bean`:用于存放业务逻辑层(Service)接口以及控制层(Controller)类。
在 `com.example.bs.entity` 包下创建以下实体类:
Student.java
```java
package com.example.bs.entity;
import javax.persistence.*;
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name = "sno", length = 10)
private String sno;
@Column(name = "sname", length = 20)
private String sname;
@Column(name = "spassword", length = 20)
private byte[] spassword;
// 省略 getter 和 setter 方法
}
```
Manager.java
```java
package com.example.bs.entity;
import javax.persistence.*;
@Entity
@Table(name = "manager")
public class Manager {
@Id
@Column(name = "mno", length = 10)
private String mno;
@Column(name = "mname", length = 20)
private String mname;
@Column(name = "mpassword", length = 20)
private byte[] mpassword;
// 省略 getter 和 setter 方法
}
```
GraduDesign.java
```java
package com.example.bs.entity;
import javax.persistence.*;
@Entity
@Table(name = "gradu_design")
public class GraduDesign {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "gid", length = 10)
private Integer gid;
@Column(name = "job_book", columnDefinition = "TEXT")
private String jobBook;
@Column(name = "open_report", columnDefinition = "TEXT")
private String openReport;
@Column(name = "interim_report", columnDefinition = "TEXT")
private String interimReport;
@Column(name = "week_report", columnDefinition = "TEXT")
private String weekReport;
@Column(name = "gradu_paper", columnDefinition = "TEXT")
private String graduPaper;
@Column(name = "comment", columnDefinition = "TEXT")
private String comment;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sno", referencedColumnName = "sno")
private Student student;
// 省略 getter 和 setter 方法
}
```
Task.java
```java
package com.example.bs.entity;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "task")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "tid", length = 10)
private Integer tid;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mno", referencedColumnName = "mno")
private Manager manager;
@Column(name = "title", columnDefinition = "TEXT")
private String title;
@Column(name = "content", columnDefinition = "TEXT")
private String content;
@Column(name = "subtime")
private Date subtime;
// 省略 getter 和 setter 方法
}
```
在 `com.example.bs.dao` 包下创建以下 DAO 接口:
StudentDao.java
```java
package com.example.bs.dao;
import com.example.bs.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentDao extends JpaRepository<Student, String> {
}
```
ManagerDao.java
```java
package com.example.bs.dao;
import com.example.bs.entity.Manager;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ManagerDao extends JpaRepository<Manager, String> {
}
```
GraduDesignDao.java
```java
package com.example.bs.dao;
import com.example.bs.entity.GraduDesign;
import org.springframework.data.jpa.repository.JpaRepository;
public interface GraduDesignDao extends JpaRepository<GraduDesign, Integer> {
}
```
TaskDao.java
```java
package com.example.bs.dao;
import com.example.bs.entity.Task;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TaskDao extends JpaRepository<Task, Integer> {
}
```
最后,在 `com.example.bs.bean` 包下创建以下类:
GraduDesignService.java
```java
package com.example.bs.bean;
import com.example.bs.dao.GraduDesignDao;
import com.example.bs.entity.GraduDesign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class GraduDesignService {
@Autowired
private GraduDesignDao graduDesignDao;
public List<GraduDesign> findAll() {
return graduDesignDao.findAll();
}
public GraduDesign getById(Integer gid) {
return graduDesignDao.getById(gid);
}
public void saveOrUpdate(GraduDesign graduDesign) {
graduDesignDao.save(graduDesign);
}
public void deleteById(Integer gid) {
graduDesignDao.deleteById(gid);
}
}
```
TaskService.java
```java
package com.example.bs.bean;
import com.example.bs.dao.TaskDao;
import com.example.bs.entity.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class TaskService {
@Autowired
private TaskDao taskDao;
public List<Task> findAll() {
return taskDao.findAll();
}
public Task getById(Integer tid) {
return taskDao.getById(tid);
}
public void saveOrUpdate(Task task) {
taskDao.save(task);
}
public void deleteById(Integer tid) {
taskDao.deleteById(tid);
}
}
```
StudentService.java
```java
package com.example.bs.bean;
import com.example.bs.dao.StudentDao;
import com.example.bs.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class StudentService {
@Autowired
private StudentDao studentDao;
public List<Student> findAll() {
return studentDao.findAll();
}
public Student getById(String sno) {
return studentDao.getById(sno);
}
public void saveOrUpdate(Student student) {
studentDao.save(student);
}
public void deleteById(String sno) {
studentDao.deleteById(sno);
}
}
```
ManagerService.java
```java
package com.example.bs.bean;
import com.example.bs.dao.ManagerDao;
import com.example.bs.entity.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class ManagerService {
@Autowired
private ManagerDao managerDao;
public List<Manager> findAll() {
return managerDao.findAll();
}
public Manager getById(String mno) {
return managerDao.getById(mno);
}
public void saveOrUpdate(Manager manager) {
managerDao.save(manager);
}
public void deleteById(String mno) {
managerDao.deleteById(mno);
}
}
```
在以上代码中,每个实体类对应一个 DAO 接口和一个 Service 类,其中 Service 类封装了常用的业务逻辑方法,如查询、保存、更新和删除等。控制层(Controller)类可以直接调用这些方法来实现相应的功能。
阅读全文