步骤一:建立一个springboot项目,然后在pom.xml中添加相关依赖
步骤二:在pom.xml文件中引入相关的依赖包,在application.properties(application.xml)中引入hibernat和mysql的相关的配置文件
(01)pom.xml
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 8.0.22 org.springframework.boot spring-boot-starter-thymeleaf com.alibaba fastjson 1.2.76 com.alibaba druid 1.1.14 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-devtools true true
(02)application.properties(application.xml)中
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.58.103:3306/activity?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root ##少了这个会自动建表失败 spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 server.port=8083 ################# ###Spring jpa ################## spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto = update ##自动建表通过以下策略来映射 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
步骤三:创建domain对象层,controller控制层,service业务层,数据交互层repository层
(01)domain对象层
package com.tjetc.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity //会在数据库生成表信息
public class Person {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)// 指定主键以及主键自增长
private Integer id;
private String name;
private String age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
(02)controller控制层
package com.tjetc.controller;
import com.tjetc.bean.Person;
import com.tjetc.service.PersonService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/person")
public class PersonController {
@Resource
private PersonService personService;
@RequestMapping("/save")
public String save() {
Person person=new Person();
person.setAge("18");
person.setName("张三");
personService.save(person);
return "save success";
}
@RequestMapping("/delete")
public String delete(int id) {
personService.delete(id);
return "delete success";
}
@RequestMapping("/findAll")
public Iterable findAll() {
Iterable persons= personService.findAll();
return persons;
}
}
(03)service业务层
package com.tjetc.service;
import com.tjetc.bean.Person;
import com.tjetc.repository.PersonRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.transaction.Transactional;
@Service
public class PersonService {
@Resource
private PersonRepository personRepository;
@Transactional
public void save(Person person) {
personRepository.save(person);
}
@Transactional
public void delete(int id) {
personRepository.deleteById(id);
}
@Transactional
public Iterable findAll() {
return personRepository.findAll();
}
}
(04)数据交互层repository层
package com.tjetc.repository; import com.tjetc.bean.Person; import org.springframework.data.repository.CrudRepository; public interface PersonRepository extends CrudRepository{ }
步骤四:启动springboot主类
步骤五:在页面访问,调用controller中的方法,然后,就可以实现,数据自动新增,删除,修改,简单的查询啦



