Spring Boot结合JPA实现对数据库的添加操作

建项目过程见(用IDEA创建Spring项目实现对数据库的增删改查操作)这篇文章

引入依赖


            mysql
            mysql-connector-java
            8.0.15
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

编写配置文件application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update

 整体结构如图

Spring Boot结合JPA实现对数据库的添加操作_第1张图片

 Student实体类根据自己的数据库编写


@Entity
@Table(name="students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    private  String age;
    private String sex;
    private String name;
    private  String id_card;
    private String student_class;
    public Student(){

    }

    public Student(Integer id, String age, String sex, String name, String id_card, String student_class) {
        this.id = id;
        this.age = age;
        this.sex = sex;
        this.name = name;
        this.id_card = id_card;
        this.student_class = student_class;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId_card() {
        return id_card;
    }

    public void setId_card(String id_card) {
        this.id_card = id_card;
    }

    public String getStudent_class() {
        return student_class;
    }

    public void setStudent_class(String student_class) {
        this.student_class = student_class;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", name='" + name + '\'' +
                ", id_card='" + id_card + '\'' +
                ", student_class='" + student_class + '\'' +
                '}';
    }
}

 dao层


@Repository
public interface StudentDao extends CrudRepository {
    
}

service层


@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    public  void  add(Student student){
        studentDao.save(student);
    }
   
}

 controller层


@Controller
@RequestMapping("/student")
public class Hello {
    @Autowired
    private StudentService studentService;
    @RequestMapping("/add")
    @ResponseBody
    public String add(){
        Student student=new Student();
        student.setId(111);
        student.setAge("23");
        student.setName("姜阳");
        student.setSex("女");
        student.setId_card("13100010011");
        student.setStudent_class("计算机2班");
        studentService.add(student);
        return "OK";
    }
   
}

运行http://localhost:8080/student/add出现ok后刷新数据库就可以看到添加的数据了

你可能感兴趣的:(spring,boot,spring,java,intellij,idea)