Spring - JPA/Hibernate 整合xml配置,自动扫描实体
2017-03-27 11:01
429 查看
0.WEB-INF结构:
WEB-INF
- web.xml
- tickets-servlet.xml
- business-config.xml
完成后:将数据库相关的配置单独列出xml文件,便于管理。在entities包内的实体(Entity)、仓库(Repository)类注解都会被自动注入。
1.新建business-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- Notice the URL about jpa, it is easy to be ignored. -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.tickets"/>
<!-- About database, JPA and Hibernate config -->
<!-- Set dataSource with JPA -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/tickets?createDatabaseIfNotExist=true&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!-- Set entityManagerFactory-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- Set the entities package to scan -->
<property name="packagesToScan" value="com.tickets.business.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
</property>
<!-- Set Hibernate config into jpaProperties-->
<property name="jpaProperties">
<props>
<!-- Hibernate config -->
<!-- Dialet could be DIY -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Enable the repositories scan -->
<jpa:repositories base-package="com.tickets.business.entities" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="exceptionTranslation"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
</bean>
<!-- End of database config -->
</beans>
2.引入到servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.tickets"/>
<mvc:resources mapping="/imges/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="Messages" />
</bean>
<!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
<!-- resource resolution infrastructure, which is highly recommended. -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<!-- HTML is the default value, added here for the sake of clarity. -->
<property name="templateMode" value="HTML" />
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<property name="cacheable" value="true" />
</bean>
<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
<!-- enables Spring's own MessageSource message resolution mechanisms. -->
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<!-- Enabling the SpringEL compiler with Spring 4.2.4 or newer can speed up -->
<!-- execution in most scenarios, but might be incompatible with specific -->
<!-- cases when expressions in one template are reused across different data -->
<!-- ypes, so this flag is "false" by default for safer backwards -->
<!-- compatibility. -->
<property name="enableSpringELCompiler" value="true" />
</bean>
<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<!-- Import the config of jpa and hibernate -->
<import resource="business-config.xml"/>
</beans>
WEB-INF
- web.xml
- tickets-servlet.xml
- business-config.xml
完成后:将数据库相关的配置单独列出xml文件,便于管理。在entities包内的实体(Entity)、仓库(Repository)类注解都会被自动注入。
1.新建business-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- Notice the URL about jpa, it is easy to be ignored. -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.tickets"/>
<!-- About database, JPA and Hibernate config -->
<!-- Set dataSource with JPA -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/tickets?createDatabaseIfNotExist=true&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!-- Set entityManagerFactory-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- Set the entities package to scan -->
<property name="packagesToScan" value="com.tickets.business.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
</property>
<!-- Set Hibernate config into jpaProperties-->
<property name="jpaProperties">
<props>
<!-- Hibernate config -->
<!-- Dialet could be DIY -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Enable the repositories scan -->
<jpa:repositories base-package="com.tickets.business.entities" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="exceptionTranslation"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
</bean>
<!-- End of database config -->
</beans>
2.引入到servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.tickets"/>
<mvc:resources mapping="/imges/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="Messages" />
</bean>
<!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
<!-- resource resolution infrastructure, which is highly recommended. -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<!-- HTML is the default value, added here for the sake of clarity. -->
<property name="templateMode" value="HTML" />
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<property name="cacheable" value="true" />
</bean>
<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
<!-- enables Spring's own MessageSource message resolution mechanisms. -->
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<!-- Enabling the SpringEL compiler with Spring 4.2.4 or newer can speed up -->
<!-- execution in most scenarios, but might be incompatible with specific -->
<!-- cases when expressions in one template are reused across different data -->
<!-- ypes, so this flag is "false" by default for safer backwards -->
<!-- compatibility. -->
<property name="enableSpringELCompiler" value="true" />
</bean>
<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<!-- Import the config of jpa and hibernate -->
<import resource="business-config.xml"/>
</beans>
相关文章推荐
- Hibernate与Spring整合,配置文件applicationContext.xml
- Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。
- 在Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
- 在Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(一)整合配置
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(一)整合配置
- Spring 整合 hibernate的 applicationContext.xml 数据源配置
- Spring与Hibernate的整合applicationContext.xml中配置
- spring与jpa整合 简化persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库
- struts2,hibernate,spring三大框架整合的注册程序(xml 配置,mysql数据库)
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学整合配置
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(一)整合配置
- springmvc,spring,hibernate整合-使用packagesToScan扫描实体
- Spring 3.1 + JPA 2.0 (Hibernate 4) + MySQL 配置 移走persistence.xml。
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring与hibernate整合applicatContext.xml配置,得到sessionFactory
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(一)整合配置
- SpringMVC+Apache Shiro+JPA(hibernate)整合配置
- 关于搭建restful web service + springMVC + ibatis/hibernate的整合和XML配置意思和ssh+springMVC框架搭建
- Spring整合Hibernate配置(使用hibernate.cfg.xml文件)