三、JPA和SpringData集成快速入门

上节说到用JPA和Spring的集成使用到了DAO层的概念,在这里将会用repository来代替,你会发现以声明接口的方式去进行CRUD是多么的简便,废话少说开始代码实战吧,\(^o^)/~

一、万里长征始于新建一个Web项目SpringData01,把相应的jar包放到/WebContent/WEB-INF/lib目录下

antlr-2.7.7.jarc3p0-0.9.2.1.jarcom.springsource.net.sf.cglib-2.2.0.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.aspectj.weaver-1.6.8.RELEASE.jarcommons-logging-1.1.3.jardom4j-1.6.1.jarhibernate-c3p0-4.2.4.Final.jarhibernate-commons-annotations-4.0.2.Final.jarhibernate-core-4.2.4.Final.jarhibernate-entitymanager-4.2.4.Final.jarhibernate-jpa-2.0-api-1.0.1.Final.jarjavassist-3.15.0-GA.jarjboss-logging-3.1.0.GA.jarjboss-transaction-api_1.1_spec-1.0.1.Final.jarmchange-commons-java-0.2.3.4.jarmysql-connector-java-5.1.7-bin.jarslf4j-api-1.6.1.jarspring-aop-4.0.0.RELEASE.jarspring-aspects-4.0.0.RELEASE.jarspring-beans-4.0.0.RELEASE.jarspring-context-4.0.0.RELEASE.jarspring-core-4.0.0.RELEASE.jarspring-data-commons-1.6.2.RELEASE.jarspring-data-jpa-1.4.2.RELEASE.jarspring-expression-4.0.0.RELEASE.jarspring-jdbc-4.0.0.RELEASE.jarspring-orm-4.0.0.RELEASE.jarspring-tx-4.0.0.RELEASE.jarspring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jar

二、配置web.xml不可以忘记哈

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="" xmlns="" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpirngData01</display-name><!– needed for ContextLoaderListener –><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:application.xml</param-value></context-param><!– Bootstraps the root web application context before servlet initialization –><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

三、配置application.xml实乃重中之重<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xmlns:context=""xmlns:jpa=""xmlns:tx=""xmlns:repository=""xsi:schemaLocation=" http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!–配置扫描包 –> <context:component-scan base-package="com.tan.springdata"/> <!–加载本地properties文件 –> <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><property name="minPoolSize" value="${jdbc.minPoolSize}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean><!–配置实体管理工厂类(容器管理类) –> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!–数据源依赖于上面声明 –><property name="dataSource" ref="dataSource"></property><!–实体包扫描 –> <property name="packagesToScan" value="com.tan.springdata.entities"></property> <!–JPA提供商 –> <property name="jpaVendorAdapter"><bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <!–配置JPA实现者Hibernate的一些基本属性 –> <property name="jpaProperties"> <props><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><!– *配置自动生成数据表列名的策略. 例如: lastName – last_name –><prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> </props></property> </bean> <!– 定义 Spring JPA 事务管理程序 –> <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean><!– 配置我们自己的repository并添加依赖 –> <jpa:repositories base-package="com.tan.springdata.repositories"entity-manager-factory-ref="entityManagerFactory"transaction-manager-ref="jpaTransactionManager"/><!–声明注解驱动 –> <tx:annotation-driven transaction-manager="jpaTransactionManager"/> </beans>

四、上面用到了db.properties

jdbc.user=rootjdbc.password=123456jdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///jpa2jdbc.minPoolSize=5jdbc.maxPoolSize=10

五、还是得整Entity,不然没法继续,,我将毫无保留全部粘出。

Person.java

人之所以有一张嘴,而有两只耳朵,原因是听的要比说的多一倍。

三、JPA和SpringData集成快速入门

相关文章:

你感兴趣的文章:

标签云: