SpringMVC集成Hibernate的主要配置

来源:互联网 发布:阿里云服务器重启 编辑:程序博客网 时间:2024/05/18 01:53

主要的架包支持

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>SSHA</groupId>  <artifactId>SSHA</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>  <name/>  <description/>  <dependencies>    <dependency>      <groupId>org.apache.openejb</groupId>      <artifactId>javaee-api</artifactId>      <version>5.0-1</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>javax.faces</groupId>      <artifactId>jsf-api</artifactId>      <version>1.2_04</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>javax.servlet</groupId>      <artifactId>jstl</artifactId>      <version>1.2</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>javax.servlet.jsp</groupId>      <artifactId>jsp-api</artifactId>      <version>2.1</version>      <scope>provided</scope>    </dependency>    <dependency>      <groupId>javax.faces</groupId>      <artifactId>jsf-impl</artifactId>      <version>1.2_04</version>      <scope>provided</scope>    </dependency>        <!-- 自动加载mybatis架包 -->    <dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>3.2.8</version></dependency><!-- 加载本地ojdbc6.jar前面三个可以乱填,最好是配上,不然可能会报错scope   系统scope路径 --><dependency>  <groupId>oracle</groupId>  <artifactId>orcale</artifactId>  <version>3.2.8</version><scope>system</scope><systemPath>C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systemPath></dependency><dependency>  <groupId>log4j</groupId>  <artifactId>log4j</artifactId>  <version>1.2.17</version></dependency><dependency>  <groupId>cglib</groupId>  <artifactId>cglib</artifactId>  <version>3.2.4</version></dependency><dependency>  <groupId>org.mybatis.generator</groupId>  <artifactId>mybatis-generator-core</artifactId>  <version>1.3.3</version></dependency><dependency>  <groupId>org.mybatis.generator</groupId>  <artifactId>mybatis-generator-maven-plugin</artifactId>  <version>1.3.3</version></dependency><!-- 引入springmvc架包 --><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  <version>4.3.2.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-web</artifactId>  <version>4.3.2.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  <version>4.3.2.RELEASE</version></dependency><!-- 新的数据源架包dbcp --><dependency>  <groupId>commons-dbcp</groupId>  <artifactId>commons-dbcp</artifactId>  <version>1.4</version></dependency><!-- aspectjweaver 事务架包 --><dependency>  <groupId>org.aspectj</groupId>  <artifactId>com.springsource.org.aspectj.weaver</artifactId>  <version>1.6.8.RELEASE</version></dependency><!-- jackson架包 --><dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-annotations</artifactId>  <version>2.6.0</version></dependency><dependency>  <groupId>org.codehaus.jackson</groupId>  <artifactId>jackson-mapper-asl</artifactId>  <version>1.9.12</version></dependency><dependency>  <groupId>net.sf.json-lib</groupId>  <artifactId>json-lib</artifactId>  <version>2.3</version>  <classifier>jdk15</classifier></dependency><dependency>  <groupId>com.fasterxml.jackson.core</groupId>  <artifactId>jackson-core</artifactId>  <version>2.6.0</version></dependency><dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>         <version>2.6.0</version></dependency>        <dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-core</artifactId>  <version>4.1.9.Final</version></dependency><!-- hibernate主要的架包支持包括org.springframework.orm.hibernate4.LocalSessionFactoryBean --><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-orm</artifactId>  <version>4.3.2.RELEASE</version></dependency>  </dependencies>  <build>    <sourceDirectory>${basedir}/src</sourceDirectory>    <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>    <resources>      <resource>        <directory>${basedir}/src</directory>        <excludes>          <exclude>**/*.java</exclude>        </excludes>      </resource>    </resources>    <plugins>      <plugin>        <artifactId>maven-war-plugin</artifactId>        <configuration>          <webappDirectory>${basedir}/WebRoot</webappDirectory>          <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>        </configuration>      </plugin>      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <configuration>          <source>1.5</source>          <target>1.5</target>        </configuration>       </plugin>          </plugins>  </build></project>


spring.xml

<?xml version="1.0" encoding="UTF-8"?><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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- springmvc的配置只能扫描控制层  spring配置文件不能扫描控制层 --><context:component-scan base-package="cn.et"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 读取jdbc.properties文件 --><context:property-placeholder location="classpath:/cn/et/ssha/emp/utils/jdbc.properties" /><!-- 数据的连接  数据源 --><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="url" value="${url}"></property><property name="driverClassName" value="${driverClass}"></property><property name="username" value="${account}"></property><property name="password" value="${password}"></property><!-- initialSize   10默认生成10个连接,那么要用到连接的时候就直接拿一条出来用就可以了,就不用等到用的时候再去产生连接 --><property name="initialSize" value="10"></property><!-- 发起一条测试的sql语句去连接一下数据库,看是否可以正常连接数据库 --></bean> <!-- 生成SqlSessionFactory对象 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">       <property name="dataSource" ref="dataSource"></property>       <property name="mappingResources">       <array>       <!-- 注册映射文件 -->       <value>cn/et/ssha/emp/entity/Emp2.hbm.xml</value>       </array>       </property>       <property name="hibernateProperties">       <props>       <prop key="show_sql">true</prop>       </props>       </property>    </bean>    <!-- 扫描接口映射的注解和xml文件  --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 事务管理器 spring帮助我们控制事务 --><bean id="transManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- aspectjweaver 事务架包 --><!-- 当切点拦截到某个操作的方法  发送通知给tx定义的通知管理  调用事务管理器 提交和回滚 --><tx:advice id="myAdvice" transaction-manager="transManager"><tx:attributes><!-- 默认的配置 --><tx:method name="add*" propagation="REQUIRED" /><tx:method name="query*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><!-- *代表了除了上面配置的方法都不使用事务 --><tx:method name="*" read-only="true" /></tx:attributes></tx:advice><!-- 切面 --><aop:config><aop:pointcut id="myPointCut" expression="execution(* cn.*..*.service.EmpService.*(..))" /><aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut" /></aop:config></beans>


springmvc.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- springmvc的配置只能扫描控制层  spring配置文件不能扫描控制层 --><context:component-scan base-package="cn.et"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan><mvc:default-servlet-handler></mvc:default-servlet-handler><mvc:annotation-driven><!-- 配置消息转换器 --><mvc:message-converters><!-- 设置json转换消息转换器,并且设置supportedMediaTypes  否则抛出406 --><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><!-- 设置响应支持的类型 --><value>text/html;charset=UTF-8</value><!-- 设置请求body支持的类型 --><value>application/x-www-form-urlencoded</value><value>application/json;charset=UTF-8</value></list></property></bean></mvc:message-converters></mvc:annotation-driven></beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 加载spring.xml配置文件 --><!-- spring 要使用springmvc的标签和国际化必须加载spring--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/cn/et/ssha/emp/spring.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- hibernate的查询是懒加载,所以可以导致list对象传到jsp的时候还没有加载数据这里把session打开,那么在jsp读取数据的时候可以再去数据库查询 --><filter><filter-name>open</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>open</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 设置一个字符集的过滤器,这个过滤器要在其它过滤器之前过滤,不然其它过滤器先拦截同样会出现中文乱码问题 --><filter><filter-name>utf</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>utf</filter-name><url-pattern>/*</url-pattern></filter-mapping> <filter>  <filter-name>myencode</filter-name>  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  <init-param>  <param-name>encoding</param-name>  <param-value>UTF-8</param-value>  </init-param>  </filter> <!-- servlet一般是不支持delete和put  所以要配置一个过滤器 --> <filter> <filter-name>hidden</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hidden</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>  <!-- 配置action -->  <servlet>  <servlet-name>spring</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>    <init-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/springmvc.xml</param-value>    </init-param>    <load-on-startup>0</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>spring</servlet-name>  <url-pattern>*.action</url-pattern>  <url-pattern>/</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
















原创粉丝点击