最近公司搭建一个应用系统,由于本人好多年没有用到struts、spring和mybatis(以前叫abatis),一切都要从头开始,只能边查文档边开发。邻近年末,对前期的工作大概总结一下,作为资料保存。
1、开源框架
struts2- 2.3.15.3+spring3.2.4+mybatis-3.2.3
2、下载所需要的包
有的包不需要,可适当删除
3、建表
以mysql为例,建库Test,并执行下面sql
CREATE TABLE T_TEST (
id varchar(12),
name varchar(50)
)
4、新建工程工程名为:Test
包结构和配置文件
包结构
action 处理前台交互
bean 实体类
dao 负责和数据库交互
mapper 是mybatis的配置文件
services 是业务逻辑接口
services.impl是业务逻辑实现类
5、配置文件
Web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 配置spring资源 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- 配置spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Spring配置文件applicationContext-common.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: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-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 启用spring注解支持 -->
<context:annotation-config />
<!-- 配置DataSource数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/temp" />
<property name="username" value="root" />
<property name="password" value="123" />
</bean>
<!--创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/zhou/mapper/*.xml" />
</bean>
<!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务的传播特性 -->
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="*">readOnly</prop>
</props>
</property>
</bean>
</beans>
Stuts2配置文件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.0.dtd">
<struts>
<!-- 将Action的创建交给spring来管理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="abstract_struts" abstract="true" extends="struts-default"
namespace="/">
<!-- 公共东西可以放到这个抽象包下 -->
</package>
<!-- 包含的配置文件 -->
<include file="struts-test.xml"></include>
</struts>
Mybatis配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 因为已经在applicationContext-common中指定了映射文件的位置,这里就省略了 -->
<!-- <mappers>
<mapper resource="com/zhou/mapper/test.xml"/>
</mappers> -->
</configuration>
日志采用log4j,配置文件log4j.properties
log4j.rootLogger=ERRO, CONSOLE,ZHOU
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c(%L) - %m%n
log4j.appender.ZHOU=org.apache.log4j.RollingFileAppender
log4j.appender.ZHOU.file=../log/ZHOU.log
log4j.appender.ZHOU.layout=org.apache.log4j.PatternLayout
log4j.appender.ZHOU.layout.conversionPattern=%d [%t] %-5p %c(%L) - %m%n
log4j.appender.ZHOU.MaxFilZHOUize=1000KB
log4j.appender.ZHOU.MaxBackupIndex=10
log4j.logger.com.zhou=INFO,ZHOU
log4j.logger.org.springframework=ERROR,ZHOU
log4j.logger.org.springframework.jdbc.support=DEBUG
log4j.logger.org.springframework.scheduling=DEBUG
log4j.logger.java.sql.Connection=DEBUG
6、验证
我们以数据从前台添加保存到数据库中为例
在WebRoot下新建test.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title></title>
<script type="text/javascript">
function insertTest() {
var myform = document.forms[0];
myform.action = "Test!save.action";
myform.method = "post";
myform.submit();
}
</script>
</head>
<body>
<h1>样例</h1>
<hr>
<form name="myform">
id: <input type="text" name="test.id"> <br>
name: <input type="text" name="test.name"> <br>
<input type="button" name="btninsert" οnclick="insertTest()" value="增加" />
</form>
</body>
</html>
在包bean新建Test.java实体类
package com.zhou.action;
import com.zhou.bean.Test;
import com.zhou.services.TestService;
/**
* @author zhouzhenlong
* @date Sept 18, 201212:55:36 PM
* @description
* @version V1.0
*/
public class TestAction {
private static final long serialVersionUID = 1L;
/**
* 注入service
*/
private TestService testService;
private Test test;
/**
* 保存Test对象
*
* @return
*/
public String save() {
testService.save(test);
return "success";
}
public void setTestService(TestService testService) {
this.testService = testService;
}
public void setTest(Test test) {
this.test = test;
}
public Test getTest() {
return test;
}
}
在包services创建接口TestService.java
package com.zhou.services;
import com.zhou.bean.Test;
/**
* @author zhouzhenlong
* @date Sept 18, 201212:55:36 PM
* @description
* @version V1.0
*/
public interface TestService {
/**
* 保存Test对象
*
* @param test
*/
public void save(Test test);
}
在包services.impl创建接口的实现类TestServiceImpl.java
package com.zhou.services.impl;
import com.zhou.bean.Test;
import com.zhou.dao.TestDao;
import com.zhou.services.TestService;
/**
* @author zhouzhenlong
* @date Sept 18, 201212:55:36 PM
* @description
* @version V1.0
*/
public class TestServiceImpl implements TestService {
private TestDao testDao;
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public void save(Test test) {
testDao.insertTest(test);
}
}
在包dao下创建TestDao.java
package com.zhou.dao;
import com.zhou.bean.Test;
/**
* @author zhouzhenlong
* @date Sept 18, 201212:55:36 PM
* @description
* @version V1.0
*/
public interface TestDao {
/**
* 插入
*
* @param test
*/
public void insertTest(Test test);
}
在包mapper先创建mybatis的配置文件test-mapper.xml
注意namespace要和实际映射的接口dao的路径相同
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhou.dao.TestDao">
<insert id="insertTest" parameterType="com.zhou.bean.Test" >
insert into t_test(id,name) values (#{id},#{name})
</insert>
</mapper>
创建配置文件applicationContext-beans.xml,负责spring依赖注入
<?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: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-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 样例代码 -->
<bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean"
scope="prototype">
<property name="mapperInterface" value="com.zhou.dao.TestDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="testService" class="com.zhou.services.impl.TestServiceImpl"
scope="prototype">
<property name="testDao" ref="testDao" />
</bean>
<!--action 注入service-->
<bean id="testAction" class="com.zhou.action.TestAction"
scope="prototype">
<property name="testService" ref="testService" />
</bean>
</beans>
创建配置文件struts-test.xml,负责struts2的访问和跳转
<?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>
<package name="MyActions" extends="struts-default">
<action name="Test!*" class="testAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
部署到tomcat下访问http://localhost:8080/Test/test.jsp
保存到数据库,成功!!!