赞
踩
建项目过程见(用IDEA创建Spring项目实现对数据库的增删改查操作)这篇文章
引入依赖
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.15</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
编写配置文件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
整体结构如图

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<Student,Long> {
-
- }
-
- @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后刷新数据库就可以看到添加的数据了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。