项目结构:
项目中需要的依赖
<!-- Servlet 的 api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- 引入Mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
<!-- log4j-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<!--fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
<!-- 引入spring-aspects:解析事务的表达式 -->
<!-- 在后面的spring配置数据库的基本连接信息中会用到这个jar包 ${} -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
到pom.xml中导入hibernate的依赖。
<!-- Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
编写hibernate的配置文件:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--
数据库连接信息。
在spring 中去配置连接信息。将hibernate交由spring加载。
-->
<!-- 显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 格式化SQL语句 -->
<property name="format_sql">true</property>
<!-- 创建数据表
<property name="hbm2ddl.auto">create</property>
-->
<!-- 关联映射文件 -->
<!-- <mapping resource="com/woomoon/entitys/User.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
导入c3p0连接池的依赖(使用c3p0,不用hibernate自带的连接池):
<!-- 引入c3p0数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
数据库的基本的连接信息:
db.properties(包括连接用户名,密码,连接url,和驱动)
initialPoolSize(初始化连接池连接数量)maxPoolSize (最大连接条数)
uname=root
upass=qwe
url=jdbc:mysql://localhost/test
driverClass=com.mysql.jdbc.Driver
initPoolSize=5
maxPoolSize=20
导入spring-content 包和spring 对于orm技术的一些支持:
<!-- 引入Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- spring 对于orm技术的一些支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
编写spring配置文件:
applicationContext-public.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 引入(db.properties)数据库的基本的连接信息:-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源:配置数据库连接池c3p0 dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${uname}"></property>
<property name="password" value="${upass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据源 依赖上面定义的 dataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载hibernate配置文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 加载UserEntity 映射的 User.hbm.xml 的xml 文件-->
<property name="mappingLocations" value="classpath:com/woomoon/entitys/User.hbm.xml"></property>
</bean>
<!-- 配置事务管理器 在使用Session时不必频繁开启事物和提交事物 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务的属性 -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--以add开头的方法必须使用事物 -->
<tx:method name="add*" propagation="REQUIRED"/>
<!--以update开头的方法必须使用事物 -->
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!--其他的方法 手动开启事物-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的切点 -->
<aop:config>
<!-- com.woomoon.dao.impl包下面所有的类,类下面所有的方法 -->
<aop:pointcut expression="execution(* com.woomoon.dao.impl.*.*(..))" id="myPoint"/>
<!--让事物管理器和dao.impl包下面所有类的方法建立关系 advisor【通知者】 -->
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPoint"/>
</aop:config>
</beans>
给com.woomoon.service包编写spring 配置文件
applicationContext-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--userServiceImpl依赖 userDaoImpl ; name是com.woomoon.service.impl.UserServiceImpl 类中 userdaoimpl 的变量名; ref 是依赖的 applicationContext-dao.xml 中id为userDaoImpl 这个 bean -->
<bean id="userServiceImpl" class="com.woomoon.service.impl.UserServiceImpl">
<property name="dao" ref="userDaoImpl"></property>
</bean>
</beans>
给com.woomoon.dao包编写spring 配置文件
applicationContext-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDaoImpl" class="com.woomoon.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
给com.woomoon.action包编写spring 配置文件
applicationContext-action.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- spring ioc 默认单例模式,要给userAction设置为原型模式 -->
<bean id="userAction" class="com.woomoon.action.UserAction" scope="prototype">
<property name="service" ref="userServiceImpl"></property>
</bean>
</beans>
整合struts2 框架:
<!-- 引入Struts2依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.33</version>
</dependency>
<!-- struts2整合Spring的 插件包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>
strust2 的配置文件 :
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="myPackage" extends="struts-default"> <!-- 访问路径为user_add.action -->
<action name="user_*" class="userAction" method="{1}"><!-- 那么 会自动解析到userAction中的add方法 -->
<!-- class不能写限定名,要经过spring【applicationContext-action.xml文件】 否则service注入不进来UserAction会报空指针异常。 -->
<result name="success">/WEB-INF/html/index.html</result> <!-- mathod={1},会调用add【useradd.action】方法。 -->
</action>
</package>
</struts>
到此纯配置ssh框架整合完成!
不足之处,但请体谅,不喜勿喷,谢谢!
- 注意:
- 1.当一个类依赖于另一个类时,要在当前类中给被依赖的类提供getter and setter 方法。
- 例如:
- UserAction 依赖 IUserService ,所以要给service 提供getter and setter 方法。
- ` IUserService service;
public void setService(IUserService service) {
this.service = service;
看到报错是一般都是create bean… not font class …. not method ….
仔细检查代码,大小写错误。或者单词错误都有可能造成以上类似错误。
小心敲代码。