Spring+SpringMVC+SpringDataJpa

首先导入所有jar(hibernate(hibernate-jpamodelgen-4.3.11.Final.jar),spring,springmvc,druid,ehcache,mysql,aspectj,spring data)

web.xlm


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssjdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
     
	<filter>
		<description>字符集过滤器description>
		<filter-name>encodingFilterfilter-name>
		<filter-class>
		    org.springframework.web.filter.CharacterEncodingFilter
		filter-class>
		<init-param>
			<description>字符集编码description>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
	filter>
	<filter-mapping>
		<filter-name>encodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:spring.xmlparam-value>
	context-param>
	<listener>
	    <listener-class>
	        org.springframework.web.context.ContextLoaderListener
	    listener-class>
	listener>
	
	
	<servlet>
		<servlet-name>dispatcherServletservlet-name>
		<servlet-class>
		    org.springframework.web.servlet.DispatcherServlet
		servlet-class>
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc.xmlparam-value>
		init-param>
		
		<load-on-startup>1load-on-startup>
	servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServletservlet-name>
		<url-pattern>/url-pattern>
	servlet-mapping>
	
	
	
	<filter>
		<filter-name>HiddenHttpMethodFilterfilter-name>
		<filter-class>
		     org.springframework.web.filter.HiddenHttpMethodFilter
		filter-class>
	filter>
	<filter-mapping>
	    <filter-name>HiddenHttpMethodFilterfilter-name>
	    <url-pattern>/*url-pattern>
	filter-mapping>
	
   <servlet>
      <servlet-name>DruidStatViewservlet-name>
      <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
  servlet>
  <servlet-mapping>
      <servlet-name>DruidStatViewservlet-name>
      <url-pattern>/druid/*url-pattern>
  servlet-mapping>
	
	
  <filter>
    <filter-name>noSessionFilterfilter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>noSessionFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

spring.xml


<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: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-4.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

	
	
	<context:component-scan base-package="com.znsd.sss">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	context:component-scan>
	
	
	
	
	
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
     <property name="url" value="jdbc:mysql://localhost:3306/vote" />
     <property name="username" value="root"/>
     <property name="password" value="100" />

     <property name="filters" value="stat" />

     <property name="maxActive" value="20" />
     <property name="initialSize" value="1" />
     <property name="maxWait" value="60000" />
     <property name="minIdle" value="1" />

     <property name="timeBetweenEvictionRunsMillis" value="60000" />
     <property name="minEvictableIdleTimeMillis" value="300000" />

     <property name="testWhileIdle" value="true" />
     <property name="testOnBorrow" value="false" />
     <property name="testOnReturn" value="false" />

     <property name="poolPreparedStatements" value="true" />
     <property name="maxOpenPreparedStatements" value="20" />

     <property name="asyncInit" value="true" />
 bean>
	
	
	<bean id="entityManagerFactory"
	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		
		<property name="dataSource" ref="dataSource">property>
		
		<property name="jpaVendorAdapter">
		<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">bean>
		property>
		
		<property name="packagesToScan" value="com.znsd.sss">property>
		
		<property name="jpaProperties">
		<props>
		
		
		
		<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategyprop>
		
		<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialectprop>
		<prop key="hibernate.show_sql">trueprop>
		<prop key="hibernate.format_sql">trueprop>
		<prop key="hibernate.hbm2ddl.auto">updateprop>
		props>
		property>
	bean>
	
	
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	   <property name="entityManagerFactory" ref="entityManagerFactory">property>
	bean>
	
	
	<tx:annotation-driven transaction-manager="transactionManager" />
    
    
	<jpa:repositories base-package="com.znsd.sss.Dao"
	entity-manager-factory-ref="entityManagerFactory">jpa:repositories>
    
beans>

springmvc.xml


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		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-4.2.xsd">

	
	<context:component-scan base-package="com.znsd.sss" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	context:component-scan>

	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	bean>
	
	
	<mvc:default-servlet-handler />

	
	<mvc:annotation-driven />
beans>

db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/vote
jdbc.user=root
jdbc.password=100

你可能感兴趣的:(spring,springMVC,spring,DataJpa)