spring 整合spring data jpa
一、导入依赖
<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(); } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Redis 集群方法
配置Redis 为了方便管理,我们将统一的配置放到 cluster.conf 文件中,别的文件引入该文件: bind127.0.0.1 protected-modeyes port6370 tcp-backlog511 timeout0 tcp-keepalive0 loglevelnotice logfile"./redis.log" databases100 save9001 save30010 save6010000 stop-writes-on-bgsave-erroryes rdbcompressionyes rdbchecksumyes dbfilenamemaster.rdb dir./ #slaveof10.1.1.1056379 #masterauthadmin123 #将次Redis实例中启用RedisCluster支持。否则,实例通常作为独立实例启动 cluster-enabledyes #这是集群中的节点能够失联的最大时间,超过这个时间,该节点就会被认为故障。 cluster-node-timeout15000 #此配置文件不能人工编辑,它是集群节点自动维护的文...
- 下一篇
zabbix实战--Php-fpm监控详解
参考视频教程: http://www.roncoo.com/course/view/fb3050a5b34b42f39ccad83ebebc89c1 龙果运维平台开源地址:https://github.com/roncoo/roncoo-cmdb Php-fpm和nginx一样内建了一个状态页,对于想了解php-fpm的状态以及监控php-fpm非常有帮助。下面我们先来了解一下详情也的情况: 一、打开php-fpm详情页面: Vim php-fpm.conf pm.status_path = /13/status 二、修改nginx配置文件,打开/13/status的监控页面: server { listen 80; server_name www.checkweb.com; location / { root /home/www/web/bbs.chekcwev.com; include fastcgi_params; fastcgi_pass unix:/tmp/php-fpm.sock; fastcgi_param SCRIPT_FILENAME $doc...
相关文章
文章评论
共有0条评论来说两句吧...