各个框架在本项目内的作用
:
spring2:主要利用ioc,以及对事物的管理,减少硬性编码和脱离手动事务控制。
struts2:主要用于MVC以及数据校验。struts2摆脱了struts1性能上的瓶颈,达到了新的高度,配置更灵活,全面支持ajax,freemark等等,采用ognl动态语言使得输出也更加灵活。
iBatis:主要用于作orm。开发效率不如hibernate,但是由于是半自动映射,因此更加灵活,并且效率更好,维护更方便。
整合过程(使用工具MyEclipse8.5):
1.Struts2的整合(这里采用手动整合的方式) 导入包:freemarker-2.3.15.jar,ognl-2.7.3.jar,struts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar,struts2-spring-plugin-2.1.8.1.jar(置于为什么在此不解释)然后再web.xml中加入 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>(到此Struts2就已经导入成功了,并且跟spring整合的配置也都配好了额)
2.Spring的导入使用myeclipse自动导入
3.ibatis的导入:导入包:ibatis-2.3.4.726.jar
4.下面的就是将3个框架整合的配置文件了,要看清楚了额,别晕哦
Spring的核心配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config></context:annotation-config> <!--想使用注入此行不可缺少-->
<bean id="dataSource"<!--配置c3p0连接池-->
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<property name="acquireIncrement"
value="${jdbc.acquireIncrement}" />
<property name="maxStatements" value="${jdbc.maxStatements}" />
<property name="initialPoolSize"
value="${jdbc.initialPoolSize}" />
<property name="idleConnectionTestPeriod"
value="${jdbc.idleConnectionTestPeriod}" />
<property name="acquireRetryAttempts"
value="${jdbc.acquireRetryAttempts}" />
</bean>
<bean id="sqlMapClient"<!--引入ibatis的配置文件,下面会有SqlMapConfig.xml的配置-->
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>/WEB-INF/iBatis/SqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="propertyConfig"<!--将连接池中的配置信息引入,下面会有jdbc.properties的配置-->
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/iBatis/jdbc.properties</value>
</property>
</bean>
<!--下面是配置自己的service 和dao的声明-->
<bean id = "user_Service" class="com.unite.service.User_service"></bean>
<bean id= "user_dao" class="com.unite.dao.User_dao">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>
ibatis的核心配置文件:SqlMapConfig.xml:<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<typeAlias alias="user" type="com.unite.bean.User" /><!--给User这个bean类起个别名方便下面的使用--><!--在此还可以添加更多的功能,具体配置找百度大神吧-->
<sqlMap resource="com/unite/bean/User.xml"></sqlMap><!--引入bean类的信息,不解释-->
</sqlMapConfig>
连接池的配置文件jdbc.properties:我这里使用的MySQL数据库,记得吧mysql的包加到项目中哦
jdbc.driverClass=com.mysql.jdbc.Driver //驱动
jdbc.url=jdbc:mysql://localhost:3306/unite //链接字符串,unite是数据库名
jdbc.user=root //数据库登录用户,下面是密码,
jdbc.password=
jdbc.minPoolSize=5
jdbc.maxPoolSize=20
jdbc.maxIdleTime=1800
jdbc.acquireIncrement=5
jdbc.maxStatements=50
jdbc.initialPoolSize=10
jdbc.idleConnectionTestPeriod=1800
jdbc.acquireRetryAttempts=30
bean类的配置文件User.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="user" >
<typeAlias alias="users" type="com.unite.bean.User" /><!--还是别名-->
<select id="checkLogin" parameterClass="users" resultClass="users">
select * from User where user_name = #user_name# and user_password = #user_password#
</select><!--这里只有一个查询的功能,sql语句中的表明是数据库的表的名字,列名也同样parameterClass:参数类型,resultClass:返回值类型-->
</sqlMap>
Struts2的配置文件struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<!--这是的配置不解释-->
<action name="User_LogAction" class="com.unite.action.User_LogAction">
<result name = "ok">/index.jsp</result>
<result name = "no">/index.jsp</result>
</action>
</package>
</struts>
5.到这里就应经整合完成了,项目结构:,jsp中的代码这里就拿过来了,跟普通的用法一样<--DAO中的代码-->
package com.unite.dao;
import java.sql.SQLException;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.unite.bean.User;
public class User_dao extends SqlMapClientDaoSupport{
@SuppressWarnings("unchecked")
public User checkLogin(User user){
System.out.println(user.getUser_name()+":"+user.getUser_password());
try {
List<User> list = this.getSqlMapClient().queryForList("checkLogin", user);
if(list.size() > 0){
return list.get(0);
}
} catch (SQLException e) { e.printStackTrace();}
return null;
}}