(原创)3大框架应该如何的整合在一起?

1.先导入一些列架包文件,并在项目中引用。

2.配置hibernate.cfg.xml文件。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<!-- 数据库配置信息,等下跟spring整合的时候需要拷贝走 -->
        <property name="connection.url">jdbc:mysql:///.....</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123</property>
	<!-- 其他配置信息 -->
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQL5Dialect
	</property>
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.connection.autocommit">true</property>
	
	<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />



</session-factory>

</hibernate-configuration>

3.下面配置spring的配置文件。(开始做spring和hibernate的整合)

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<!-- 自动扫描与装配bean -->
	<context:component-scan base-package="cn.itcast.oa"></context:component-scan>


	<!-- 加载外部的properties配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>


	<!-- 配置数据库连接池(c3p0) -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 基本信息 -->
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="user" value="${username}"></property>
		<property name="password" value="${password}"></property>
		<!-- 其他配置 -->
		<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="3"></property>
		<!--连接池中保留的最小连接数。Default: 3 -->
		<property name="minPoolSize" value="3"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="5"></property>
		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="3"></property>
		<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
		<property name="maxStatements" value="8"></property>
		<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
		<property name="maxStatementsPerConnection" value="5"></property>
		<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="1800"></property>
	</bean>

	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>


	<!-- 配置声明式的事务管理(采用基于注解的方式) -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>


</beans>

4.建立外面的jdbc.propertiese文件用来存放DB的连接信息,然后在spring配置文件中引用。

jdbcUrl = jdbc:mysql:///123
driverClass = com.mysql.jdbc.Driver
username = root
password =123

5.建立测试文件先测试下sessionFactory能否用。建立SpringTest.java.

public class SpringTest {
	ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
   
	@Test
	public void testSessionFactory(){
	SessionFactory sessionFactory=	(SessionFactory) ac.getBean("sessionFactory");
	System.out.println(sessionFactory);
	}


6.测试完sessionFactory后开始测试Transaction,此时需要用到service层,所以建立TestService.java文件,然后还需要在domain层建立一个实体User,然后配置相应的映射文件User.hb.xml。最后在hibernate.cfg.xml.文件中加入User.hbm.xml文件的映射.

①先建立User实体类

package cn.itcast.oa.domain;

public class User {
	
	private long id;
	private String name;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
②User.hb.xml对应的映射到数据库的文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.oa.domain">

	<class name="User" table="itcast_user">
		<id name="id">
            <generator class="native"/>
		</id>
		<property name="name"/>
	</class>
	
</hibernate-mapping>
③建立TestService.java文件

@Service("testService")
public class TestService {

	@Resource
	private SessionFactory sessionFactory;

	@Transactional
	public void saveTwoUsers() {
		Session session = sessionFactory.getCurrentSession();
		session.save(new User());
		session.save(new User());
	}
}
④一定记住最后在hibernate.cfg.xml.文件中加入User.hbm.xml文件的映射

<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />

7.在TestService类中加入注解,然后在SpringTest中写一个Transaction的测试方法,运行看看数据库中是否有插入数据。

	@Test
	public void testTransaction() throws Exception {
		TestService testService  = (TestService) ac.getBean("testService");//切记这里不需要new,因为交给spring管理,new的话没人管你!
		testService.saveTwoUsers();
	}
结果:



8.下面开始的是action和spring的整合,先建立TestAction.java类,在struts.xml文件中配置action,新建一个test.jsp页面,因为struts与spring整合,所在struts.xml文件中的class直接写成spring中的bean的名字,同时需要在action类中加入注解,最后需要在web.xml文件中加入spring的监听器,启动服务输入网址即可!

①建立TestAction.java类

@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {

	@Resource
	private TestService testService;

	@Override
	public String execute() throws Exception {
		System.out.println("--------> TestAction.execute()");
		testService.saveTwoUsers();
		return "success";
	}
}
②struts.xml文件中配置action

<?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.0.dtd">

<struts>
	<!-- 配置为开发模式 -->
    <constant name="struts.devMode" value="true" />
	<!-- 配置扩展名为action -->
    <constant name="struts.action.extension" value="action" />
    <package name="default" namespace="/" extends="struts-default">
		
		<!-- 测试用的action,当与Spring整合后,class属性写的就是Spring中bean的名称 -->
		<action name="test" class="testAction">
			<result name="success">/test.jsp</result>
		</action>

    </package>

</struts>
③在web.xml文件中加入spring的监听器

<?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的监听器,用于初始化ApplicationContext对象 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans*.xml</param-value>//这里记住beans这个名字一定要跟spring的配置文件名字一样
	</context-param>


	<!-- 配置Struts2的主过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
④启动服务输入网址(http://localhost:8080/ItcastOA/test.action)

 结果:
9.至此SSH3大框架的整合完毕。(其中有许多许多的细节,值得大家去注意的,只有自己亲手去做过,才会发现其中有很多的细节容易出错!!!)。

下面就是日志文件加入。。。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值