




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第1SpringDataJPASpringDataJPASpringDataJPA是Spring基于ORM框架、JPAJPA开发者用极简的代码即可实现对数据库的和操作。它提供了包括增删改查等在内的常用功能,且易于扩展!学习并使用SpringDataJPA可以极大提高开发效率!SpringDataJPADAOCRUDSpringDataJPASpringDataJpa极大简化了数据库层代码。如何简化的呢?使用了SpringDataJpa,dao层中只需要写接口,就自动具有了增删改查、分页查询等方法。SpringDataJPAJPAhibernateJPA是一套规范,内部是有接口和抽象类组成的。hibernate是一套成ORM框架,而且API编程,意味着站在更高的角度上看待问题(面向接口编程)SpringDataJPASpringJPAJPA规范下的专门第2SpringDataJPA的快速SpringDataJPACRUDSpringDataJPASpringDataJPASpringDataJPASpringSpringDataJPAJPA的服务hibernatespring相关坐标,hibernate坐标,数据库驱动坐标等junit<!--springbeg--<!--springend--<!--hibernatebeg--<!--hibernateend--<!--c3p0beg--<!--c3p0end--<!--logend--<!--logend--elbeg使用springdatajpa<!--elend--SpringDataJPA<?xmlversion="1.0"encoding="UTF-<beans 1.dataSource配置数据库连接池<beanid="dataSource" <propertyname="driverClass"value="com.mysql.jdbc.Driver"<propertyname="jdbcUrl"value="jdbc:mysql://localhost:3306/jpa"<propertyname="user"value="root"<propertyname="password"value="111111"2.配置entityManagerFactory<beanid="entityManagerFactory"<propertyname="dataSource"ref="dataSource"<propertyname="packagesToScan" .entity"<property<beanclass="org.hibernate.jpa.HibernatePersistenceProvider"<property<propertyname="generateDdl"value="false"<propertyname="database"value="MYSQL"<property <propertyname="showSql"value="true"<property<bean3.事务管理器JPA事务管理器<beanid="transactionManager"<propertyname="entityManagerFactory"ref="entityManagerFactory"整合springdatajpa<jpa:repositoriesbase- <!--4.txAdvice--<tx:adviceid="txAdvice"transaction-<tx:methodname="save*"<tx:methodname="insert*"<tx:methodname="update*"<tx:methodname="delete*"<tx:methodname="get*"read-<tx:methodname="find*"read-<tx:methodname="*"<!--5.aop--<aop:pointcutid="pointcut"expression="execution(* .service.*.*(..))"/><aop:advisoradvice-ref="txAdvice"pointcut-ref="pointcut"<<ponent-scanbase-packagepackageimportimportimportjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;import***@Entity ")//publicclassCustomer @GeneratedValue(strategy=GenerationType.IDENTITY配置主键的生成策略@Column(name="cust_id")//指定和表中cust_id字段的映射关系privateLong")//privateStringprivateStringprivateStringprivateStringprivateStringprivateStringpublicLonggetCustId()return}publicvoidsetCustId(LongcustId)this.custId=}publicStringgetCustName()return}publicvoidsetCustName(StringcustName)this.custName=}publicStringgetCustSource()return}publicvoidsetCustSource(StringcustSource)this.custSource=}publicStringgetCustIndustry()return}publicvoidsetCustIndustry(StringcustIndustry)this.custIndustry=}publicStringgetCustLevel()return}publicvoidsetCustLevel(StringcustLevel)this.custLevel=}publicStringgetCustAddress()return}publicvoidsetCustAddress(StringcustAddress)thisthis.custAddress=}publicStringgetCustPhone()return}publicvoidsetCustPhone(StringcustPhone)this.custPhone=}}SpringDataJPASpringDataJPADaopackagepackageimportimportimportimport }SpringDataJPADaoCRUDSpringDataJPASpringDataJPADao层接DaoCRUD操作publicclassCustomerDaoTestprivateCustomerDaopublicvoidtestSave(){CustomercnewCustomer(); }***publicvoidtestUpdate()Customercustomer= }publicvoidtestDelete(){}publicvoidtestFindById()Customercustomer=customerDao.findOne(2l);}}SpringDataJPASpringDataJPAJpaRepositoryJpaSpecificationExecutor接口,这样就可以使用这些接口中定义的方法,但是这些方法都只是一些,没有具体的实现方式,那么在SpringDataJPA中它又是怎么实现的呢?SpringDataJPAdebugSpringDataJPA 子类的实现过customerDao 生成的一个对 对象中方法调用的分JdkDynamicAopinvokecustomerDao对象生JdkDynamicAop生成的动态对象当中,而这个动态对象就是通过SimpleJpaRepository的源码分析,定位到了findOneSpringDataJPA完整的调用过程SpringDataJPA中接口定义JpaRepositoryJpaRepository接口后,我们就可以使用接口中定义的方法进行查SpringDataJPA提供的查询方法已经可以解决大部分的应用场景,但是对于某些业务来publicpublicinterfaceCustomerDaoextendsJpaRepository<Customer,Long>,JpaSpecificationExecutor<Customer>{@Query(value="frompublicList<Customer>@Query(value="fromCustomerwherecustName=publicCustomerfindCustomer(String}此外,也可以通过使用@Query来执行一个更新操作,为此,我们需要在使用@Query的同时,用@Modifying来将该操作标识为修改查询,这样框架最终会生成一个更新的操作,而非查询@Query(value="update@Query(value="updateCustomersetcustName=?1wherecustId=?2")publicvoidupdateCustomer(StringcustName,LongSQLSpringDataJPAsql*nativeQuery使用本地sql@Query(value="select*frompublicvoid顾名思义,方法命名规则查询就是根据方法的名字,就能创建查询。只需要按照SpringJPA提供的方法命名规则定义方法的名称,就可以完成查询工作。SpringDataJPA在程序执行SpringDataJPAfindBy开头,涉及条件查询时,条件的属性publicCustomerfindByCustName(String…wherex.lastname=andx.firstname=…wherex.lastname=?1x.firstname=…wherex.firstname=…wherebetween?1and…wherex.age<…wherex.age⇐…wherex.age>…wherex.age>=…wherex.startDate>…wherex.startDate<…wherex.ageis…wherex.agenot…wherex.firstnamelikelike…wherex.firstnamelike?1(parameterb
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 传统食品工业化生产2025年智能工厂改造项目进度控制报告
- 社渚镇民宅转让合同范本
- 灌溉项目合同协议书范本
- 碎石机械销售合同协议书
- 机动车销售服务合同范本
- 汽修厂多人合伙协议合同
- 湖南文理学院合作协议书
- 电动车出租合作合同范本
- 烘焙店工作合同范本模板
- 物业创意园租房合同范本
- 二升三数学综合练习 暑假每日一练60天
- 01-低血糖症科普知识讲座
- 2025年新疆维吾尔自治区生产建设兵团中考语文真题(解析版)
- (高清版)DB11∕T 509-2025 房屋建筑修缮工程定案和施工质量验收规程
- 初级电工考试题及答案2025
- 2025年广西中考地理试题(含答案)
- GB/T 12008.2-2010塑料聚醚多元醇第2部分:规格
- 既有住宅适老化改造项目可行性研究报告书
- 公司网银盾交接单
- 施工人员安全教育培训课件
- 建筑施工项目成本费用分析手册
评论
0/150
提交评论