包结构
导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.1.RELEASE</version>
<scope>compile</scope>
</dependency>
创建实体
@Entity
public class Dog {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int dogid;
private String dogname;
private String kind;
private int age;
private int sex;
}
创建DogDao
public interface DogDao extends CrudRepository<Dog, Integer> {
}
创建DogService
public interface DogService {
//新增数据
public Dog insertGrade(Dog dog);
//修改数据
public Dog updataGrade(Dog dog);
//删除数据
public void deleteGrade(Integer id);
//查询数据
public Iterable<Dog> getAllGrade();
}
创建DogSetviceimpl
@Service
public class DogSetviceimpl implements DogService {
@Resource
private DogDao dogDao;
@Override
public Dog insertGrade(Dog dog) {
return dogDao.save(dog);
}
@Override
public Dog updataGrade(Dog dog) {
return dogDao.save(dog);
}
@Override
public void deleteGrade(Integer id) {
dogDao.deleteById(id);
}
@Override
public Iterable<Dog> getAllGrade() {
return dogDao.findAll();
}
}
创建JpaController
@RestController
@RequestMapping("/DaoController")
public class JpaController {
@Resource
private DogService dogService;
//添加
@RequestMapping("/insertDog")
public Dog insertGrade(){
Dog dog=new Dog();
dog.setDogname("海绵宝宝");
dog.setAge(1);
return dogService.insertGrade(dog);
}
//修改
@RequestMapping("/updateDog")
public Dog updateDao(){
Dog dog=new Dog();
dog.setDogid(1);
dog.setDogname("海绵宝宝puls");
return dogService.updataGrade(dog);
}
//删除
@RequestMapping("/deleteDog")
public void deleteDog(){
dogService.deleteGrade(1);
}
//查询
@RequestMapping("/getAllDog")
public Iterable<Dog> getAllDog(){
return dogService.getAllGrade();
}
创建application.yml
##数据库四大连接参数
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springbootjpa
username: root
password: root
##jpa表的生成策略
jpa:
hibernate:
ddl-auto: update