Struts2+Spring3+Mybatis3开发环境搭建
本文主要介绍Struts2+Spring3+Mybatis3开发环境搭建


<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>test_ssm</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 配置spring资源 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext-*.xml</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>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
<?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/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean> <!--创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mabatis-config.xml" />
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/jialin/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>
applicationContext-beans.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"> <bean id="userInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean"
scope="prototype">
<property name="mapperInterface" value="com.jialin.dao.UserInfoDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <bean id="userInfoService" class="com.jialin.service.UserInfoService"
scope="prototype">
<property name="userInfoDao" ref="userInfoDao" />
</bean> <!-- 为IRegisterService接口配置事务拦截器,baseTransactionProxy是事务拦截器,在Controller中获取这个对象 -->
<bean id="IUserInfoService" parent="baseTransactionProxy">
<!-- 实现类 -->
<property name="target" ref="userInfoService" />
</bean> <bean id="userManageAction" class="com.jialin.action.UserManageAction"
scope="prototype">
<property name="userInfoService" ref="IUserInfoService" />
</bean> </beans>
<?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" /> <!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 -->
<constant name="struts.action.extension" value=","></constant> <package name="abstract_struts" abstract="true" extends="struts-default"
namespace="/">
<!-- 公共东西可以放到这个抽象包下 -->
</package> <!-- 包含的配置文件 -->
<include file="/config/struts-user.xml"></include>
</struts>
struts-user.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> <package name="MyActions" extends="struts-default">
<action name="*_*" class="userManageAction" method="{1}">
<result name="success" type="redirect">/{2}.jsp</result>
</action>
</package> </struts>
<?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>
<typeAlias alias="userinfo" type="com.jialin.entity.UserInfo"/>
</typeAliases> <!-- 因为已经在applicationContext-common中指定了映射文件的位置,这里就省略了 -->
<!-- <mappers>
<mapper resource="com/jialin/dao/registerMapper.xml"/>
</mappers> --> </configuration>
UserManageAction.java
package com.jialin.action; import com.jialin.entity.UserInfo;
import com.jialin.service.IUserInfoService; public class UserManageAction { private IUserInfoService userInfoService; public IUserInfoService getUserInfoService() {
return userInfoService;
} public void setUserInfoService(IUserInfoService userInfoService) {
this.userInfoService = userInfoService;
} private UserInfo userInfo; public UserInfo getUserInfo() {
return userInfo;
} public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
} public String insertUser() { userInfoService.insertUser(userInfo);
return "success";
} public String editUser() { userInfoService.edit(userInfo);
return "success";
} public String removeUser() { userInfoService.remove(userInfo);
return "success";
} public String getUserById() {
userInfoService.get(userInfo);
return "success";
} public String getListByName() { userInfoService.getList(userInfo); return "success";
} public String getAllUser() {
userInfoService.getAllUser();
return "success";
} }
IUserInfoService.java
package com.jialin.service;
import java.util.List;
import com.jialin.entity.UserInfo;
public interface IUserInfoService {
public void insertUser(UserInfo user);
public void edit(UserInfo user);
public void remove(UserInfo user);
public UserInfo get(UserInfo user);
public List getList(UserInfo user);
public List getAllUser();
}
UserInfoService.java
package com.jialin.service; import java.util.List; import java.util.Iterator; import com.jialin.dao.UserInfoDao;
import com.jialin.entity.UserInfo; public class UserInfoService implements IUserInfoService { private UserInfoDao userInfoDao; public UserInfoDao getUserInfoDao() {
return userInfoDao;
} public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
} @Override
public void insertUser(UserInfo user)
{
userInfoDao.insertUser(user);
} @Override
public void edit(UserInfo user) {
userInfoDao.edit(user); } @Override
public void remove(UserInfo user) {
userInfoDao.remove(user); } @Override
public UserInfo get(UserInfo user) {
UserInfo user1=userInfoDao.get(user);
System.out.println(user1.getUsername());
return user1;
} @Override
public List getList(UserInfo user) {
List list=userInfoDao.getList(user); for(Iterator iter=list.iterator();iter.hasNext();)
{
UserInfo user1=(UserInfo)iter.next();
System.out.println(user1.getUsername()+",");
} return list;
} @Override
public List getAllUser() {
List list= userInfoDao.getAllUser();
for(Iterator iter=list.iterator();iter.hasNext();)
{
UserInfo user=(UserInfo)iter.next();
System.out.println(user.getUsername()+",");
} return list;
} }
UserInfoDao.java
package com.jialin.dao;
import java.util.List;
import com.jialin.entity.UserInfo;
public interface UserInfoDao {
public void insertUser(UserInfo user);
public void edit(UserInfo user);
public void remove(UserInfo user);
public UserInfo get(UserInfo user);
public List getList(UserInfo user);
public List getAllUser();
}
UserInfo.java
package com.jialin.entity;
public class UserInfo {
private int id;
private String username;
private String password;
private String ismanager;
public void setIsmanager(String ismanager) {
this.ismanager = ismanager;
}
public String getIsmanager() {
return ismanager;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
Mybaitis映射文件
userinfo-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.jialin.dao.UserInfoDao">
<!-- 自动生成id策略 -->
<insert id="insertUser" parameterType="userinfo" useGeneratedKeys="true" keyProperty="id">
insert into userinfo(username,password,ismanager) values (#{username},#{password},#{ismanager})
</insert> <!-- userInfoResultMap是userinfo-resultmap.xml中定义的resultmap -->
<select id="getList" parameterType="userinfo" resultType="list" resultMap="userInfoResultMap">
select * from userinfo where username like '%' #{username} '%'
</select> <select id="getAllUser" resultType="list" resultMap="userInfoResultMap">
select * from userinfo
</select> <select id="get" parameterType="userinfo" resultType="com.jialin.entity.UserInfo" resultMap="userInfoResultMap">
<![CDATA[
select * from userinfo where id = #{id}
]]>
</select> <update id="edit" parameterType="userinfo">
update userinfo set
username = #{username},
password = #{password}
where id = #{id}
</update> <delete id="remove" parameterType="userinfo">
delete from userinfo where id = #{id}
</delete> </mapper>
userinfo-resultMap.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.jialin.dao.UserInfoDao">
<resultMap type="com.jialin.entity.UserInfo" id="userInfoResultMap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="ismanager" column="ismanager"/>
</resultMap>
</mapper>
测试jsp
<%@ page contentType="text/html; charset=UTF-8"%> <html>
<head>
<title></title> <script type="text/javascript"> function insertUser()
{
var myform=document.forms[0];
myform.action="insertUser_success";
myform.method="post";
myform.submit();
} function getUserByName()
{
var myform=document.forms[0];
myform.action="getListByName_success";
myform.method="post";
myform.submit();
} function getAllUser()
{
var myform=document.forms[0];
myform.action="getAllUser_success";
myform.method="post";
myform.submit();
} function editUser()
{
var myform=document.forms[0];
myform.action="editUser_success";
myform.method="post";
myform.submit();
} function getUserById()
{
var myform=document.forms[0];
myform.action="getUserById_success";
myform.method="post";
myform.submit();
} function removeUser()
{
var myform=document.forms[0];
myform.action="removeUser_success";
myform.method="post";
myform.submit();
}
</script> </head>
<body>
<h1>用户管理</h1>
<hr>
<form name="myform" >
id:<input type="text" name="userInfo.id"> <br>
用户名:<input type="text" name="userInfo.username"> <br>
密码:<input type="text" name="userInfo.password"> <br>
是否为管理员:<input type="text" name="userInfo.ismanager"><br>
<input type="button" name="btninsert" onclick="insertUser()" value="增加" />
<input type="button" name="btnedit" onclick="editUser()" value="修改" />
<input type="button" name="btnremove" onclick="removeUser()" value="删除" /><br>
<input type="button" name="btnget" onclick="getUserById()" value="按id查询" />
<input type="button" name="btngetlist" onclick="getUserByName()" value="按名称查询" />
<input type="button" name="btngetall" onclick="getAllUser()" value="查询全部" />
</form>
</body>
</html>
本文转自:http://www.cnblogs.com/Augusdi/archive/2013/06/12/3132813.html
Struts2+Spring3+Mybatis3开发环境搭建的更多相关文章
- 【转】SpringMVC+Spring3+Hibernate4开发环境搭建
原文地址: SpringMVC+Spring3+Hibernate4开发环境搭建
- SpringMVC+Spring3+hibernate4 开发环境搭建以及一个开发实例教程
刚刚接触了SpringMVC这个框架,因此有必要把它拿过来同hibernate.Spring框架进行集成和开发一个实例,在真正企业从头开发的项目中往往一个稳定的开发环境至关重要,开发一个项目选择什么样 ...
- schemamvcSpringMVC+Spring3+Hibernate4开发环境搭建
上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下schemamvc <?xml version="1.0" encoding=" ...
- 使用Maven搭建Struts2框架的开发环境
一.创建基于Maven的Web项目
- struts2 + spring3 + mybatis3 环境搭建
struts2 + spring3 + mybatis3 1. 框架下载 struts2: http://struts.apache.org/ 下载 struts-2.3.14-all.zip spr ...
- SpringMVC+Spring3+Hibernate4开发环境的搭建
在项目早期比较简单,大多用JSP .Servlet + JDBC 直接获取,以后使用 Struts1(Struts2)+Spring+Hibernate, 严格格按照分层概念驱动项目开发.利用这段时间 ...
- Struts2开发环境搭建,及一个简单登录功能实例
首先是搭建Struts2环境. 第一步 下载Struts2去Struts官网 http://struts.apache.org/ 下载Struts2组件.截至目前,struts2最新版本为2.3.1. ...
- mybatis实战教程(mybatis in action)之一:开发环境搭建
mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载. 首先建立一 ...
- spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明
一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 struts2.3 ...
随机推荐
- 安装mac os x时about a second remaining解决方法
转自: http://www.hongkiat.com/blog/clean-install-mavericks/ During the installation process, you may e ...
- DOS命令符基本操作
怎么改变DOS默认路径 开始->所有程序->附件->命令提示符,在“命令提示符”上右键,选择“属性”,(或者在快捷方式上点击属性)会弹出一个“命令提示符 属性”对话框,可以通过修改该 ...
- 动态设置Div坐标
<style type="text/css"> #main{text-align:center; background-color:#9FF; height:600px ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- Sybase IQ如何将大文件数据迅速加载到数据库
试想一下,如果一个文件5G.10G甚至更大.如何将它迅速地加载到数据库指定的表呢?我们看看Sybase IQ是如何迅速地将表的数据加载到数据库的. 数据文件格式: 1440,2011-01-09 00 ...
- 六间房 繁星 酷我 来疯 秀吧 新浪秀 直播播放器 Live 1.2
适合用于进行录制的时候 特别说明: 安装 falsh play 19 时 不能正常播放 每个按钮都有提示,不详细说明 下载地址 http://pan.baidu.com/s/1i32ETIt 下载地址 ...
- poj 2013 Symmetric Order 解题报告
题目链接:http://poj.org/problem?id=2013 设长度非递减的字串序列为s[1]...s[n].设计递归子程序print(n),其中n为字串序号,每分析1个字串,n=n-1. ...
- CentOS下配置Hadoop集群:java.net.NoRouteToHostException: No route to host问题的解决
我用的是hadoop 1.2.1 遇到的问题是: hadoop中datanode无法启动,报Caused by: java.net.NoRouteToHostException: No route t ...
- nginx打开目录浏览
server { listen 80; server_name localhost; index index.html index.htm index.php; autoindex on; #开启ng ...
- linux中断申请之request_threaded_irq
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21977330&id=3755609 在linux里,中断处理分 ...