介绍
Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷
特征
- 强大的存储库和自定义对象映射抽象
- 从存储库方法名称中进行动态查询导出
- 实现域基类提供基本属性
- 支持透明审核(创建,最后更改)
- 集成自定义存储库代码的可能性
- Easy Spring通过JavaConfig和自定义XML命名空间进行集成
- 与Spring MVC控制器进行高级集成
- 跨店存储的实验支持
1.导入maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mr</groupId>
<artifactId>springdata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springdata</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.properties配置文件
#配置数据源
spring.datasource.username=zxf
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/car
#自动创建表
#表创建策略
spring.jpa.hibernate.ddl-auto=update
3.dao层继承CrudRepository<实体类,主键类型>
public interface StuRepository extends CrudRepository<Student,Integer> {
}
4.实体类
@Entity
@Data
public class Student {
@Id
//当Generation Type被选为AUTO时,从5.0版开始,Hibernate使用SequenceStyleGenerator不管数据库
//AUTO 在MySql的情况下,Hibernate使用表模拟一个序列hibernate_sequence
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer stuId;
private String stuName;
private Integer stuAge;
}
5.serviceImpl
@Service
@Transactional //在启动类上添加 @EnableTransactionManagement管理,也可以不添加
public class StuServiceImpl implements StuService {
@Autowired
private StuRepository stuRepository;
@Override
public void add(Student s) {
stuRepository.save(s);
/*System.out.println(1/0);*/
}
@Transactional(readOnly = true)
@Override
public Iterable<Student> query() {
Iterable<Student> all = stuRepository.findAll();
return all;
}
@Override
public void del(Integer id) {
stuRepository.deleteById(id);
}
@Override
public void update(Student stu) {
stuRepository.save(stu);
}
}
6. controller层
/**
* 保存(有ID修改,没有则保存)
* @return
*/
@RequestMapping("save")
public String save(){
Student s = new Student();
s.setStuAge(19);
s.setStuName("xbb");
stuService.add(s);
return "success";
}
/**
* 查询
* @return
*/
@RequestMapping("query")
public Iterable<Student> query(){
Iterable<Student> list = stuService.query();
return list;
}
/**
* 删除
* @return
*/
@RequestMapping("del/{id}")//Spring会对@PathVariable注解的变量进行自动赋值,当然你也可以指定@PathVariable使用哪一个URL中的变量:
public String delStu(@PathVariable("id") Integer id){
stuService.del(id);
return "success";
}
/**
* 修改
* @param id
* @return
*/
@RequestMapping("update/{id}/{name}/{age}") //传递多个参数
public String update(@PathVariable Integer id,@PathVariable String name,@PathVariable Integer age){
Student stu = new Student();
stu.setStuId(id);
stu.setStuName(name);
stu.setStuAge(age);
stuService.update(stu);
return "success";
}