Play Spring MVC (iv)---Integrate JPA in spring MVC

Source: Internet
Author: User

About integrating JPA in spring MVC is based on my last article on Spring MVC Basic configuration, so let's take a look at my previous post: http://blog.csdn.net/u012116457/article/details/43528111

Next, you need to add some new files:

Jdbc.properties:

jdbc.driverclassname=com.mysql.jdbc.driverjdbc.url=jdbc\:mysql\://localhost\:3306/tmos?useunicode\=true& characterencoding\=utf-8&autoreconnect\=truejdbc.user=rootjdbc.password=rootjdbc.dialect= org.hibernate.dialect.mysql5dialectjdbc.show_sql=falsejdbc.format_sql=true#jdbc.dialect= Org.hibernate.dialect.mysqlinnodbdialectjdbc.initialpoolsize=2jdbc.maxpoolsize=200jdbc.batch_size= 20jdbc.hbm2ddl.auto=updatejdbc.query.substitutions=true 1, False 0, yes ' Y ', no ' N '

Jpaconfig.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" xmlns: Cache= "Http://www.springframework.org/schema/cache" xsi:schemalocation= "http://www.springframework.org/schema/b EANs http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/ Context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/ SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/SPRING-TX-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http:/ /wwW.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/data/repository h Ttp://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd http://www.springframework.org/s Chema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <context:annotation-config/ ><context:component-scan base-package= "module"/> <!--here depending on the project--><!--configuration Placeholder--><beanclass= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "><property name=" Location "value=" Classpath:/jdbc.properties "/></bean><!--data source--><!--<bean id=" DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "abstract=" false "lazy-init=" Default "autowire=" Def Ault "> <bean id=" dataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource "> <property name= "Driverclassname" value= "${jdbc.driverclassname}"/&Gt;<property name= "url" value= "${jdbc.url}"/><property name= "username" value= "${jdbc.user}"/>< Property name= "Password" value= "${jdbc.password}"/></bean><!--Configure a factory--<bean id= " Entitymanagerfactory "class=" Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "> <pro Perty name= "DataSource" ref= "DataSource"/> <property name= "Persistenceprovider" ref= "Persistenceprovider"/&        Gt <property name= "Jpavendoradapter" ref= "Jpavendoradapter"/> <property name= "JpaDialect" ref= "JpaDialect"/ > <property name= "jpaproperties" > <props> <prop key= "Hibernate.dialect" >${jdbc.dialect}</prop> <prop key= "Hibernate.hbm2ddl.auto" >${jdbc.hbm2ddl.auto}</prop> <! --Avoid duplicate printing of SQL--<prop key= "Hibernate.show_sql" >${jdbc.show_sql}</prop> <prop key= "   Hibernate.format_sql ">${jdbc.format_sql}</prop>         </props> </property> <property name= "Packagestoscan" ><list><valu e>module.entity</value> </list></property> </bean><bean id= "Persistenceprovider "class=" org.hibernate.ejb.HibernatePersistence "/> <bean id=" jpavendoradapter "class=" org    . springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "> <property name=" Database "value=" MYSQL "/>      </bean> <bean id= "Jpadialect" class= "Org.springframework.orm.jpa.vendor.HibernateJpaDialect"/> <jpa:repositories base-package= "Module.dao" entity-manager-factory-ref= "entitymanagerfactory" Trans action-manager-ref= "Txmanager"/> <!--<bean id= "CacheManager" class= "Org.springframework.cache.ehcach E.ehcachecachemanager "p:cache-manager-ref=" Ehcache "> </bean> <bean id=" Ehcache " Class= "Org.springframewoRk.cache.ehcache.EhCacheManagerFactoryBean "p:config-location=" Classpath:spring-ehcache.xml "/>-- <bean id= "Txmanager" class= "Org.springframework.orm.jpa.JpaTransactionManager" > <property name= "Entitymanagerfactory" ref= "entitymanagerfactory"/> </bean><tx:annotation-driven transaction-ma Nager= "Txmanager"/></beans>
This completes the integration of JPA in spring MVC,Here you can download the project, which contains some test code and the jar package used: http://download.csdn.net/detail/u012116457/8426211





Play Spring MVC (iv)---Integrate JPA in spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.