struts2+Mybatis+spring 框架整合
一.创建web项目导入必需的jar文件
二.创建并编写配置文件,配置文件比较多。
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-2.5.xsd"> <!--配置数据源属性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/configs/sqlServer.properties</value> </property> </bean> <!--配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${jdbc.driver}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.user}</value> </property> <property name="password"> <value>${jdbc.pwd}</value> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:com/test/sqlMapper/mybatis_config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="loginDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.test.dao.ILoginDao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="loginAction" class="com.test.action.LoginAction"> <property name="loginDao" ref="loginDao"></property> </bean> </beans>
其实applicationContext.xml也可用DBCP配置数据连接参数,开启注解扫描,本人比较推荐,简单实用!一下是配置代码:
1 <!-- 1.定义DBCP组件加载数据库连接参数 --> 2 <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> 3 <property name="username" value="root"></property> 4 <property name="password" value="wyq881205"></property> 5 <property name="url" value="jdbc:mysql://localhost:3306/bill?useUnicode=true&characterEncoding=utf8"></property> 6 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 7 </bean> 8 9 <!-- 2.注入连接信息 --> 10 <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> 11 <property name="dataSource" ref="dbcp"></property> 12 <property name="mapperLocations" value="classpath:com/ablii/sql/*.xml"></property> 13 </bean> 14 15 <!-- 3.MapperScannerConfigrer配置 --> 16 <bean id="mapperscanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 17 <property name="sqlSessionFactory" ref="ssf"></property> 18 <property name="basePackage" value="com.ablii.dao"></property> 19 </bean>
三.接下来就是web.xml。
1 <filter> 2 <filter-name>123</filter-name> 3 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>123</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping> 9 10 <context-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:applicationContext.xml</param-value> 13 </context-param>
三.接下来就是struts.xml和业务关联了。
1 <struts> 2 <package name="demo1" extends="struts-default"> 3 <!-- name请求名;class Action类型; 4 method Action类方法名,默认execute --> 5 <action name="hello" method="execute" 6 class="org.tarena.action.HelloAction"> 7 <!-- name标识符与action方法返回值对应 8 type指定result组件类型 --> 9 <result name="success" type="dispatcher"> 10 /WEB-INF/hello.jsp 11 </result> 12 </action> 13 </package> 14 15 <package name="demo2" namespace="/user" 16 extends="struts-default"> 17 <!-- 对应/tologin.action或/tologin 18 class省略默认使用框架的Action 19 method默认execute --> 20 <action name="tologin"> 21 <!-- name默认success;type默认dispatcher --> 22 <result> 23 /WEB-INF/login.jsp 24 </result> 25 </action> 26 27 <action name="login" 28 class="org.tarena.action.LoginAction"> 29 <result name="success"> 30 /WEB-INF/ok.jsp 31 </result> 32 <result name="error"> 33 /WEB-INF/login.jsp 34 </result> 35 </action> 36 37 </package> 38 39 40 41 </struts>
最后在在mybatis中有个映射文件Mapper.xml 写上你的SQL语句就可以注意一下参数类型,和返回值类型还有参数的个数。
这也要求定义的实体类必须符合JAVABEAN规范。