单调的增删改查让愈来愈多的程序员感到乏味,这时候就出现了不少优秀的框架,完成了对增删改查操做的封装,只须要简单配置,无需书写任何sql,就能够完成增删改查。这里比较推荐的是Spring Data Jpa。java
Spring Data JPA是Spring Data家族的一部分,能够轻松实现基于JPA的存储库。 此模块处理对基于JPA的数据访问层的加强支持。 它使构建使用数据访问技术的Spring驱动应用程序变得更加容易。mysql
咱们继续使用前两章用的数据库结构来进行演示。git
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
@Data @NoArgsConstructor @AllArgsConstructor @Entity public class Student implements Serializable { private static final long serialVersionUID = 6712540741269055064L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer studentId; private Integer age; private String name; private Integer sex; private Date createTime; private Integer status; }
@GeneratedValue是主键生成策略,Jpa自带的几种主键生成策略以下:程序员
主键生成策略扩展github
自定义主键生成器:redis
public class MyGenerator implements IdentifierGenerator { @Override public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException { return getId(); } public static long getId(){ return System.currentTimeMillis(); } }
而后在实体类作一下配置:spring
@Data @NoArgsConstructor @AllArgsConstructor @Entity public class Student implements Serializable { private static final long serialVersionUID = 6712540741269055064L; @Id @GenericGenerator(name="idGenerator",strategy = "com.javatrip.springdatajpa.entity.MyGenerator") @GeneratedValue(generator = "idGenerator") private Integer studentId; private Integer age; private String name; private Integer sex; private Date createTime; private Integer status; }
dao层接口实现JpaRepository,泛型选择pojo和其主键类型,就会自动实现简单的CRUD等接口,无需手动开发,就能快速进行调用。sql
public interface StudentRepository extends JpaRepository<Student,Integer> { /** * 根据年龄,名字模糊查询 * @return */ Student findByNameLikeAndAge(String name,int age); }
Jpa除了实现CRUD方法,还支持字段名模糊查询等各类不用手写sql的操做。数据库
@SpringBootTest class SpringDataJpaApplicationTests { @Autowired StudentRepository repository; @Test void contextLoads() { // 查询全部实体 List<Student> all = repository.findAll(); // 根据id查询实体类 Optional<Student> byId = repository.findById(100); // 根据id删除数据 repository.deleteById(100); // 插入一条数据 // 若是数据库存在该实体的主键,则更新,不然插入 Student student = new Student(); student.setAge(18); student.setName("Java旅途"); repository.save(student); repository.findByNameLikeAndAge("Java",18); } }
spring-data-jpa在外国程序员界很是广泛。相比其余两种方式,它不须要写sql就能够完成很是完善的数据操做,我也是比较推荐使用它做为orm框架。segmentfault
star
支持一下!spring-boot-route(一)Controller接收参数的几种方式
spring-boot-route(二)读取配置文件的几种方式
spring-boot-route(五)整合swagger生成接口文档
spring-boot-route(六)整合JApiDocs生成接口文档
spring-boot-route(七)整合jdbcTemplate操做数据库
spring-boot-route(八)整合mybatis操做数据库
spring-boot-route(九)整合JPA操做数据库
spring-boot-route(十一)数据库配置信息加密
spring-boot-route(十二)整合redis作为缓存
spring-boot-route(十三)整合RabbitMQ
spring-boot-route(十五)整合RocketMQ
spring-boot-route(十六)使用logback生产日志文件
spring-boot-route(十七)使用aop记录操做日志
spring-boot-route(十八)spring-boot-adtuator监控应用
spring-boot-route(十九)spring-boot-admin监控服务
spring-boot-route(二十)Spring Task实现简单定时任务
spring-boot-route(二十一)quartz实现动态定时任务
spring-boot-route(二十二)实现邮件发送功能
这个系列的文章都是工做中频繁用到的知识,学完这个系列,应付平常开发绰绰有余。若是还想了解其余内容,扫面下方二维码告诉我,我会进一步完善这个系列的文章!