一、项目pom.xml文件引入的Jar包配置,可以通过maven库查到相关配置
maven库访问地址:二、配置文件 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.rayoo</groupId> <artifactId>my-springmvc</artifactId> <version>1.0-RELEASE</version> <packaging>war</packaging> <!-- 公用属性声明 --> <properties> <project.encoding>UTF-8</project.encoding> <spring.version>3.1.0.RELEASE</spring.version> <cxf.version>2.6.0</cxf.version> </properties> <dependencies> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> <!-- alibaba druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <!-- hibernate4 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.2.RELEASE</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.4.RELEASE</version> </dependency> <!-- json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.9</version> </dependency> <!-- jackson-jaxrs --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6</version> </dependency> <!-- jdom --> <dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.5</version> </dependency> <!--WebService--> <dependency> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency></dependencies><build> <finalName>${project.name}</finalName> <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.5</version> <configuration> <contextPath>/${project.name}</contextPath> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> <scanIntervalSeconds>0</scanIntervalSeconds> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8090</port> </connector> </connectors> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>${project.encoding}</encoding> </configuration> </plugin> </plugins></build></project>-----------------------------------------------------pom.xml END -------------------------------------------------
三、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring/framework/applicationContext.xml, classpath*:spring/glamour/applicationContext-glamour.xml, classpath*:spring/glamourappapi/applicationContext-glamourappapi.xml </param-value> </context-param> <filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring 配置 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/framework/mvc/applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>----------------------------------------------------web.xml END ---------------------------------------------
四、数据源配置 applicationContext-datasource.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置数据库连接 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.102:3306/gls_china?characterEncoding=UTF-8"/> <property name="username" value="gls"/> <property name="password" value="sRd5g8xk"/> </bean> <!-- 配置hibernate相关信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 以下列表写入实体类 --> <property name="annotatedClasses"> <!-- 中间写入hibernate注解的实体 --> <list> <value>com.rayoo.glamour.home.dto.PbCustomers</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置事务异常封装 --> <!-- <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> --> <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只对业务逻辑层实施事务 --> <aop:pointcut id="txPointcut" expression="execution(* com.rayoo..*Service.*(..))" /> <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice --> <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/> </aop:config> </beans>---------------------------------------------数据源配置END -------------------------------------------------------------
五、spring mvc 配置文件 applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.rayoo.glamour.home.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- JSP视图解析 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> <property name="contentType" value="text/html;charset=UTF8" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> <!-- 静态资源映射 --> <mvc:resources mapping="js/**" location="js/" /> <mvc:resources mapping="images/**" location="images/" /> <mvc:resources mapping="css/**" location="css/" /> <mvc:resources mapping="robots.txt" location="/robots.txt" /></beans>----------------------------------------------spring mvc 配置END-------------------------------------------
实体类PbCustomers:
package com.rayoo.glamour.home.dto;
import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import com.rayoo.framework.base.BaseObject;/** * 会员 POJO */ @Entity(name="pb_customers")public class PbCustomers extends BaseObject{ private static final long serialVersionUID = 1L; public PbCustomers(){ super(); } /** ID */ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id") private java.lang.String id; /** 用户名称 */ @Column(name="customer_name") private java.lang.String customer_name; /** 用户编码 */ @Column(name="customer_code") private java.lang.String customer_code; /** 联系电话 */ @Column(name="common_telphone") private java.lang.String common_telphone; public void setId(java.lang.String id) { this.id = id; } public java.lang.String getId() { return this.id; } public void setCustomer_name(java.lang.String customer_name) { this.customer_name = customer_name; } public java.lang.String getCustomer_name() { return this.customer_name; } public void setCustomer_code(java.lang.String customer_code) { this.customer_code = customer_code; } public java.lang.String getCustomer_code() { return this.customer_code; } public java.lang.String getCommon_telphone() { return common_telphone; } public void setCommon_telphone(java.lang.String common_telphone) { this.common_telphone = common_telphone; }}BaseHibernateDao:
package com.rayoo.glamour.base;
import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate4.support.HibernateDaoSupport; public class BaseHibernateDao extends HibernateDaoSupport { @Resource public void setMySessionFactory(SessionFactory sessionFactory){ this.setSessionFactory(sessionFactory); } }HomeDaoImpl:
package com.rayoo.glamour.home.dao.impl;
import com.rayoo.glamour.base.BaseHibernateDao;import com.rayoo.glamour.home.dao.HomeDao;import com.rayoo.glamour.home.dto.PbCustomers;public class HomeDaoImpl extends BaseHibernateDao implements HomeDao { @Override public PbCustomers selectHome(String str) { return (PbCustomers) this.getHibernateTemplate().get(PbCustomers.class, str); }略 (cotrollor/service).....
问题:
[ERROR] Failed startup of context org.mortbay.jetty.webapp.WebAppContext@988edd{/my-springmvc,D:\WorkSpace_616\my-springmvc\src\main\webapp}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\WorkSpace_616\my-springmvc\src\main\webapp\WEB-INF\classes\spring\framework\datasource\applicationContext-datasource.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;解决:
org.springframework.orm.hibernate3.HibernateTransactionManager
改为:org.springframework.orm.hibernate4.LocalSessionFactoryBean