滴水穿石。
今天开始练习SpringData.慢慢学会做记录
1.创建一个javaProject.
2.创建applicationContext.xml配置文件
下一步选择命名空间信息
3.创建数据库连接信息文件
注意:配置信息项后不能有空格
4.配置applicationContext.xml
4.1 配置数据源
<!-- 1.配置数据源 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <!-- 其他... --> </bean>
4.2 测试数据库连接(数据源配置是否正常)
public class SpringDataTest { private ApplicationContext ctx = null; { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testDataSource() throws SQLException{ DataSource dataSource = ctx.getBean(DataSource.class); System.out.println(dataSource.getConnection()); } }
测试结果
打印出连接信息
数据源测试OK.
4.3.整合JPA
<!-- 2.配置JPA 的 EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="jpaVendorAdapter"><!-- jpa实现产品的适配器 --> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <property name="packagesToScan" value="com.cfb.learn"></property> <property name="jpaProperties"> <props> <!-- 二级缓存相关 --> <!-- <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="net.sf.ehcache.configurationResourceName">ehcache-hibernate.xml</prop> --> <!-- 生成的数据表的列的映射策略 --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <!-- hibernate 基本属性 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>
编写实体类
4.4单元测试
不需要写任何东西,因为执行代码的时候要运行Spring容器,执行 EntityManagerFactory
执行测试
查看数据库已经生成对应表结构
JPA整合测试通过
4.5 配置事务
<!-- 3.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 4.配置支持注解的事务 --> <tx:annotation-driven transaction-manager="transactionManager"/>
4.6 整合SpringData
加入JPA的命名空间
<!-- base-package 扫描 Respository Bean 所在的 package --> <jpa:repositories base-package="com.cfb.learn" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
编写 PersonRepository
/* Repository<要处理的实体类, 主键类型> */ public interface PersonRepository extends Repository<Person, Integer>{ // Person getByName(String name); }
编写测试代码
@Test public void helloPersonTest(){ PersonRepository pRepository = ctx.getBean(PersonRepository.class); Person person = pRepository.getByName("jack"); System.out.println(person.toString()); }
运行结果
INFO: HHH000232: Schema update complete Hibernate: select person0_.id as id1_0_, person0_.age as age2_0_, person0_.brith as brith3_0_, person0_.email as email4_0_, person0_.name as name5_0_ from jpa_persons person0_ where person0_.name=? Person [id=1, name=jack, age=23, email=55@qqq.com, brith=2017-07-17 11:37:07.0]
测试通过
—————————————————————————————————————————————————————————————————————————
无论如何,心平气和。