您现在的位置是:首页 > 文章详情

spring 整合spring data jpa

日期:2018-11-29点击:576

一、导入依赖

<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> </dependency>

二、配置jpa

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="                     http://www.springframework.org/schema/beans                     http://www.springframework.org/schema/beans/spring-beans.xsd                     http://www.springframework.org/schema/tx                      http://www.springframework.org/schema/tx/spring-tx.xsd                     http://www.springframework.org/schema/aop                      http://www.springframework.org/schema/aop/spring-aop.xsd                     http://www.springframework.org/schema/context                           http://www.springframework.org/schema/context/spring-context.xsd                     http://www.springframework.org/schema/cache                      http://www.springframework.org/schema/cache/spring-cache.xsd                     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd                      http://www.springframework.org/schema/data/jpa      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <context:property-placeholder location="classpath:/config/config.properties" /> <!-- 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> </bean> <!-- Jpa Entity Manager 配置 --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 指定Jpa持久化实现厂商类,这里以Hibernate为例 --> <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property> <!-- 指定Entity实体类包路径 --> <property name="packagesToScan" value="com.web" /> <!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 --> <property name="jpaProperties"> <props> <!-- 命名规则 My_NAME->MyName --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.DefaultComponentSafeNamingStrategy</prop> <!-- 打印sql语句 --> <prop key="hibernate.show_sql">true</prop> <!-- 格式化sql语句 --> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <!-- 自动检查注解的实体和数据表,如果数据库不存在的标,会根据实体自动生成 --> <property name="generateDdl" value="false" /> <property name="database" value="HSQL" /> </bean> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <!-- 配置事务 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 配置支持基于注解的事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 重要配置:启用扫描并自动创建代理的功能 --> <jpa:repositories base-package="com.web" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory" /> </beans>

三、实现接口

(因为CrudRepository集成的是顶层接口Repository实现了简单的crud操作)

public interface UserInfoRepository extends CrudRepository<UserInfo, Long>{ }

四、注入使用

@Service public class UserInfoServiceImpl implements UserInfoService { @Autowired private UserInfoRepository repository; @Override public List<UserInfo> findAll() { return (List<UserInfo>) repository.findAll(); } }


原文链接:https://blog.roncoo.com/article/128456
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章