springboot-jpa|hibernate|springdata Dear 丶 2022-05-14 15:46 288阅读 0赞 # 一、Concept # **JPA** java persistence API 通过5.0的注解或者xml描述对象到关系表的映射关系,并将运行期的实体对象持久化到数据库中 **Hibernate** jpa是一种规范,hibernate是它的实现 对jdbc轻量级封装,将pojo与数据库表建立映射关系,全自动orm框架 **springData** 简化对数据库访问,支持云服务的开源框架 **springDataJpa** 是springData的一个模块, 简化jpa的写法,除了curd外,还有分页、排序功能,只需要实现一个接口就可以curd操作 # 二、springboot+jpa # ### 1.add pom dependencies ### <!-- 添加MySQL数据库驱动依赖包. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 添加Spring-data-jpa依赖. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ### 2.Add database configuration information in application.properties ### ######################################################## ###datasource config ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 ######################################################## ### Java Persistence Api -- Spring jpa config ######################################################## # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy #[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy] spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect ### 3.Entity class ### @Entity//标注实体类 @Id//主键 @GeneratedValue(Strategy=GenerationType.AUTO)//主键生成策略 ### 4.Reponsitory interface ### ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_27_color_FFFFFF_t_70] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_27_color_FFFFFF_t_70 1] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_27_color_FFFFFF_t_70 2] //最顶层空接口Reponstory public interface XxxReponsitory extends CurdReponsitory<Xxx,Integer>{ //已经继承了curd接口的所有方法 //可以两种方法自定义查询 //1.查询方法以find,get,read开头 public Xxx findByXxxName(String xxxName); //2.JPQL语句 类似hql @Query("from Xxx where xxxName = :na); public Xxx findByXxxName2(@Params("na" String xxxName); } in service @Autowired private XxxReponsitory xxxReponsitory; @Transaction//进行事务绑定 xxxReponsitory.save(Xxx); xxxReponsitory.delete(1) List<Xxx> list = xxxReponsitory.findAll(); # 三、springboot + jdbcTemplate # ### 1.add pom dependencies ### <!-- 添加MySQL数据库驱动依赖包. --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 添加Spring-data-jdbc依赖. --> <!-- 当然,如何已经加入jpa,则不用引入jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> ### 2.Method of use ### @Reponsitory public class DemoDao{ @Resource private JdbcTemplate jdbcTemplate; public Demo getById(long id){ String sql = "select * from Demo where id=?"; RowMapper<Demo> rowMapper = new BeanPropertyRowMapper<Demo>(Demo.class); return jdbcTemplate.queryForObject(sql,rowMapper); } } -------------------- ### springboot+mybatis|自动生成mapper ### 依赖 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70][] ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70 1][] 自动生成代码generator-详细:[https://www.imooc.com/video/18334][https_www.imooc.com_video_18334] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_27_color_FFFFFF_t_70]: /images/20220503/a8f764f156b040aaa7d8fe298a159ec3.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_27_color_FFFFFF_t_70 1]: /images/20220503/479e0fedb68145d4a4e77ba1d80739cb.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_27_color_FFFFFF_t_70 2]: /images/20220503/3c1d1eae273c49c39274b8d6d80d70ab.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70]: /images/20220503/97eb966db523470eba1bba17c6d8134b.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzYyNjc3_size_16_color_FFFFFF_t_70 1]: /images/20220503/c8364f72001245acbc55ff0fdfea16c0.png [https_www.imooc.com_video_18334]: https://www.imooc.com/video/18334
还没有评论,来说两句吧...