springData概述及helloworld
一、SpringData概述
1. SpringData
- Spring的一个子项目。用于简化数据库的访问,支持NoSQL和关系数据存储,其主要目标是使数据库的访问变得方便快捷。
2. SpringData支持的NoSQL产品
- MongoDB(文档数据库)
- Neo4j(图形数据库)
- Redis(键/值存储)
- Hbase(列族数据库)
3. SpringData所支持的关系数据存储技术
- JDBC
- JPA
4. JPASpringData概述
- 致力于尖山数据访问层(DAO)的开发量。开发者唯一要做的就是声明持久层接口,其他的都交给SpringDataJPA来帮你完成
- SpringData实际上就是规范方法的名字
二、helloworld
1. 搭建环境
-
导依赖
<properties> <hibernate.version>5.0.7.Final</hibernate.version> <c3p0.version>0.9.5.4</c3p0.version> <mysql.version>5.1.28</mysql.version> <spring.version>5.2.0.RELEASE</spring.version> </properties> <dependencies> <!-- junit单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- springData --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.2.0.RELEASE</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.18.Final</version> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <!-- hibernate-ehcache --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>5.0.7.Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies>
-
编写配置文件(复制粘贴再改改)
-
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.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/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <context:property-placeholder location="classpath:database.properties"/> <context:component-scan base-package="cn.ann"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${mysql.driver}"/> <property name="jdbcUrl" value="${mysql.url}"/> <property name="user" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置JPA提供商适配器,可以通过内部 bean 的方式来配置 --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <!-- 配置实体类所在的包 --> <property name="packagesToScan" value="cn.ann.entity"/> <!-- 配置JPA基本属性,例如JPA实现产品的属性 --> <property name="jpaProperties"> <props> <!-- 配置orm基本属性 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <!-- 二级缓存相关 --> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory </prop> <prop key="hibernate.cache.use_query_cache">true</prop> </props> </property> </bean> <!-- 配置事务 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:advice id="txAdvice"> <tx:attributes> <!-- TODO --> <tx:method name="*"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置事务到切面 --> <aop:config> <!-- TODO --> <aop:pointcut id="txPc" expression="execution(* cn.ann.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/> </aop:config> <!-- 配置JPA --> <!-- base-package:各种Repository文件所在的包。其实就是dao层 --> <jpa:repositories base-package="cn.ann.dao" entity-manager-factory-ref="entityManagerFactory"/> </beans>
-
database.properties
mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai mysql.username=root mysql.password=密码
-
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="D:\\temp"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"/> </ehcache>
-
log4j.properties
log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
-
-
写实体类(省略getter和setter)
@Entity @Table(name = "t_person") public class Person implements Serializable { private static final long serialVersionUID = -5964437048712784999L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; private String gender; private String email; }
-
dao
public interface PersonDao extends Repository<Person, Long> { Person getByNameLike(String name); }
2. 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo {
@Resource(name = "personDao")
private PersonDao personDao;
@Test
public void Demo() {
System.out.println(personDao.getByNameLike("AA"));
}
}
本节代码:点击此处