一、导入jar包
<
dependency
>
<
groupId
>
javax.servlet
</
groupId
>
<
artifactId
>
jstl
</
artifactId
>
<
version
>
1.2
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
javax.servlet
</
groupId
>
<
artifactId
>
javax.servlet-api
</
artifactId
>
<
version
>
3.0.1
</
version
>
<
scope
>
provided
</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-web
</
artifactId
>
<
version
>
4.2.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.aspectj
</
groupId
>
<
artifactId
>
aspectjweaver
</
artifactId
>
<
version
>
1.8.10
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.struts
</
groupId
>
<
artifactId
>
struts2-core
</
artifactId
>
<
version
>
2.5.8
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.struts.xwork
</
groupId
>
<
artifactId
>
xwork-core
</
artifactId
>
<
version
>
2.3.31
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.struts
</
groupId
>
<
artifactId
>
struts2-spring-plugin
</
artifactId
>
<
version
>
2.5.8
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
mysql
</
groupId
>
<
artifactId
>
mysql-connector-java
</
artifactId
>
<
version
>
5.1.38
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.mybatis
</
groupId
>
<
artifactId
>
mybatis
</
artifactId
>
<
version
>
3.4.1
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.mybatis
</
groupId
>
<
artifactId
>
mybatis-spring
</
artifactId
>
<
version
>
1.3.1
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-jdbc
</
artifactId
>
<
version
>
4.2.1.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-tx
</
artifactId
>
<
version
>
4.3.7.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.javassist
</
groupId
>
<
artifactId
>
javassist
</
artifactId
>
<
version
>
3.20.0-GA
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.slf4j
</
groupId
>
<
artifactId
>
slf4j-api
</
artifactId
>
<
version
>
1.7.21
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.slf4j
</
groupId
>
<
artifactId
>
slf4j-log4j12
</
artifactId
>
<
version
>
1.7.21
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.logging.log4j
</
groupId
>
<
artifactId
>
log4j-core
</
artifactId
>
<
version
>
2.7
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
cglib
</
groupId
>
<
artifactId
>
cglib
</
artifactId
>
<
version
>
3.2.5
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-context-support
</
artifactId
>
<
version
>
4.3.8.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring-orm
</
artifactId
>
<
version
>
4.3.8.RELEASE
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
commons-fileupload
</
groupId
>
<
artifactId
>
commons-fileupload
</
artifactId
>
<
version
>
1.3.1
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.freemarker
</
groupId
>
<
artifactId
>
freemarker
</
artifactId
>
<
version
>
2.3.23
</
version
>
</
dependency
>
二、web.xml配置文件
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version
=
"2.5"
>
<
display-name
>
spring_struts2_mybatis
</
display-name
>
<
welcome-file-list
>
<
welcome-file
>
index.html
</
welcome-file
>
</
welcome-file-list
>
<!-- 配置spring -->
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath:applicationContext.xml
</
param-value
>
</
context-param
>
<!-- 当服务器容器启动的时候,会自动读取contextConfigLocation,也就是beans.xml -->
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<!-- 配置struts2 -->
<
filter
>
<
filter-name
>
struts2
</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
struts2
</
filter-name
>
<
url-pattern
>
*.action
</
url-pattern
>
</
filter-mapping
>
</
web-app
>
三、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
>
<
typeAliases
>
<
package
name
=
"com.liujie.model"
/>
</
typeAliases
>
<
mappers
>
<
mapper
resource
=
"com/liujie/model/user.mapper.xml"
/>
</
mappers
>
</
configuration
>
四、编码
UserDao接口:
public
interface
UserDao {
public
List<User> getAll();
}
UseDaoImpl实现:
public
class
UserDaoImpl
extends
SqlSessionDaoSupport
implements
UserDao {
public
List<User> getAll() {
return
this
.getSqlSession().selectList(
"com.liujie.model.UserMapper.getAll"
);
}
}
UserDaoService接口:
public
interface
UserService {
public
List<User> getAll();
}
UserServiceDaoImpl实现:
public
class
UserServiceImpl
implements
UserService {
private
UserDao
userDao
;
public
void
setUserDao(UserDao
userDao
) {
this
.
userDao
=
userDao
;
}
public
List<User> getAll() {
return
userDao
.getAll();
}
}
UserAction:
public
class
UserAction {
private
List<User>
list
;
private
UserService
userService
;
public
void
setUserService(UserService
userService
) {
this
.
userService
=
userService
;
}
public
List<User> getList() {
return
list
;
}
public
void
setList(List<User>
list
) {
this
.
list
=
list
;
}
public
String list() {
list
=
userService
.getAll();
return
"success"
;
}
}
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:tx
=
"http://www.springframework.org/schema/tx"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
xsi:schemaLocation
=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!-- 配置数据源 -->
<
bean
id
=
"dataSource"
class
=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<
property
name
=
"driverClassName"
value
=
"com.mysql.jdbc.Driver"
></
property
>
<
property
name
=
"url"
value
=
"jdbc:mysql://localhost:3306/test"
></
property
>
<
property
name
=
"username"
value
=
"root"
></
property
>
<
property
name
=
"password"
value
=
"123456"
></
property
>
</
bean
>
<!-- 声明式事务配置 -->
<!-- 配置事务管理器 -->
<
bean
id
=
"txManager"
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
<!-- 配置事务通知 -->
<
tx:advice
id
=
"txAdvice"
transaction-manager
=
"txManager"
>
<
tx:attributes
>
<!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
<
tx:method
name
=
"add"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"insert"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"update"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"delete"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"remove*"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"get"
read-only
=
"true"
/>
<
tx:method
name
=
"*"
propagation
=
"REQUIRED"
/>
</
tx:attributes
>
</
tx:advice
>
<
aop:config
>
<!-- 切入点应该是service.impl下的所有类的所有方法,这里为了方便测试所以直接用了dao.impl -->
<
aop:pointcut
expression
=
"execution(* com.liujie.dao.impl.*.*(..))"
id
=
"pointcut"
/>
<
aop:advisor
advice-ref
=
"txAdvice"
pointcut-ref
=
"pointcut"
/>
</
aop:config
>
<!-- 声明式事务配置结束 -->
<!-- 配置sqlSessionFactory -->
<
bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
></
property
>
<
property
name
=
"configLocation"
value
=
"classpath:mybatis-config.xml"
></
property
>
</
bean
>
<
import
resource
=
"config/spring/user.xml"
/>
</
beans
>
user.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"
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.xsd"
>
<
bean
id
=
"userDao"
class
=
"com.liujie.dao.impl.UserDaoImpl"
>
<
property
name
=
"sqlSessionFactory"
ref
=
"sqlSessionFactory"
></
property
>
</
bean
>
<
bean
id
=
"userService"
class
=
"com.liujie.service.impl.UserServiceImpl"
>
<
property
name
=
"userDao"
ref
=
"userDao"
></
property
>
</
bean
>
<
bean
id
=
"userAction"
class
=
"com.liujie.action.UserAction"
scope="prototype"
>
<
property
name
=
"userService"
ref
=
"userService"
></
property
>
</
bean
>
</
beans
>
struts配置文件:
struts.xml:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"
>
<
struts
>
<
include
file
=
"config/struts/user.xml"
></
include
>
</
struts
>
user.xml:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"
>
<
struts
>
<
package
name
=
"user"
namespace="/"
extends
=
"struts-default"
>
<
action
name="list"
class
=
"userAction"
method
=
"list"
>
<
result
>
/list.jsp
</
result
>
</
action
>
</
package
>
</
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
>
<
typeAliases
>
<
package
name
=
"com.liujie.model"
/>
</
typeAliases
>
<
mappers
>
<
mapper
resource
=
"config/mybatis/user.mapper.xml"
/>
</
mappers
>
</
configuration
>
user.mapper.xml:
<?
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.liujie.model.UserMapper"
>
<
select
id
=
"getAll"
resultType
=
"User"
>
select * from user
</
select
>
</
mapper
>
list.jsp页面:
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<%@taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
<!
DOCTYPE
html
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"
>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>
Insert title here
</
title
>
</
head
>
<
body
>
<
table
width
=
"80%"
align
=
"center"
>
<
tr
>
<
td
>
编号
</
td
>
<
td
>
姓名
</
td
>
<
td
>
密码
</
td
>
</
tr
>
<c:forEachitems="${list}"var="user">
<
tr
>
<
td
>
${user.id}
</
td
>
<
td
>
${user.name}
</
td
>
<
td
>
${user.pwd}
</
td
>
</
tr
>
</c:forEach>
</
table
>
</
body
>
</
html
>