- 浏览: 168614 次
- 性别:
- 来自: 长沙
文章分类
最新评论
-
单v纯微笑:
您好,能把dbsyn_config_quartz这个表发给我一 ...
定时器(quartz+spring)读取数据库配置 -
fenglingabc:
我用了comet4j 结果因为推送比较频繁(基本上1秒一次), ...
comet4j轮询 -
NeverGiveUpToChange:
nice,还不错哦,谢谢啦!!!
springmvc上传 -
ralfsqual:
自定义的线程中的逻辑每次都是被执行两次,不知道为什么啊。比如打 ...
comet4j轮询 -
goll2012:
我倒,原来要配置tomcat。。。果然是高手,对于只看不动手的 ...
comet4j轮询
struts2.2.3 + spring3.1.0 + mybatis3.1.0集成简单demo
- 博客分类:
- java web 开发
1、导入相应的jar包
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件加载的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<!-- 配置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>/*</url-pattern>
</filter-mapping>
</web-app>
3、配置spring配置文件
application.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: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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.pdsu.edu"></context:component-scan>
<!--proxy-target-class="true"强制使用cglib代理 如果为false则spring会自动选择-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 最大活跃数 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.JDBC配置文件详细
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/operationLog
jdbc.username=root
jdbc.password=
jdbc.maxActive = 2
jdbc.maxIdle =5
jdbc.minIdle=1
jdbc.initialSize =3
jdbc.maxWait =3000
5、配置mybatis主配置文件:
<?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="user" type="com.pdsu.edu.domain.User"/>
</typeAliases>
<mappers>
<mapper resource="com/pdsu/edu/domain/sqlMappers/user.xml" />
</mappers>
</configuration>
6、配置user.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.pdsu.edu.domain.User">
<resultMap type="com.pdsu.edu.domain.User" id="userResult">
<result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
<select id="userLogin" parameterType="user" resultMap="userResult">
select * from user
where
username=#{username} and password=#{password}
</select>
<select id="selectAllUser" resultMap="userResult">
select * from user
</select>
<select id="findUserById" parameterType="int" resultMap="userResult">
select *
from user where id=#{id}
</select>
<insert id="insertUser" parameterType="user">
<![CDATA[
insert into
user(username,password) values(#{username},#{password})
]]>
</insert>
<update id="updateUser" parameterType="user">
update user set
username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where
id=#{id}
</delete>
</mapper>
7、User实体的写法
public class User implements Serializable {
private static final long serialVersionUID = -4415990281535582814L;
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", password=" + password + ", username=" + username + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
8、UserDao的写法
public interface UserDao {
public abstract void insertUser(User user);
public abstract void updateUser(User user);
public abstract void deleteUser(Integer userId);
public abstract User findUserByid(Integer userId);
public abstract List<User> findAll();
public abstract User userLogin(User user);
}
9、UserDao的实现
@Repository
public class UserDaoImpl implements UserDao {
private final String INSERT_USER = "insertUser";
private final String UPDATE_USER = "updateUser";
private final String DELETE_USER = "deleteUser";
private final String FIND_USER_BYID = "findUserById";
private final String SELECT_ALL_USER = "selectAllUser";
private final String USER_LOGIN = "userLogin";
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public void insertUser(User user) {
sqlSessionTemplate.insert(INSERT_USER, user);
}
public void updateUser(User user) {
sqlSessionTemplate.update(UPDATE_USER, user);
}
public void deleteUser(Integer userId) {
sqlSessionTemplate.delete(DELETE_USER, userId);
}
public User findUserByid(Integer userId) {
return sqlSessionTemplate.selectOne(FIND_USER_BYID, userId);
}
public List<User> findAll() {
return sqlSessionTemplate.selectList(SELECT_ALL_USER);
}
public User userLogin(User user) {
return sqlSessionTemplate.selectOne(USER_LOGIN, user);
}
}
10、UserService接口
public interface UserService {
// 添加用户
public abstract void addUser(User user);
public abstract void updateUser(User user);
public abstract void deleteUser(Integer userId);
public abstract User findUserById(Integer userId);
public abstract List<User> findAllUser();
public abstract User login(User user);
}
11、UserService接口的实现
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
// 添加用户
public void addUser(User user) {
userDao.insertUser(user);
}
// 更新用户
public void updateUser(User user) {
userDao.updateUser(user);
}
public void deleteUser(Integer userId) {
userDao.deleteUser(userId);
}
public User findUserById(Integer userId) {
return userDao.findUserByid(userId);
}
public List<User> findAllUser() {
return userDao.findAll();
}
public User login(User user) {
return userDao.userLogin(user);
}
}
12、配置Struts2
<?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>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定默认编码集 ,作用于HttpServletRequest的setCharacterEncoding()和freemarker,vilocity的输出 -->
<constant name="struts.configuration.xmlreload" value="true"/>
<!-- 当struts配置文件修改时是否自动加载 -->
<constant name="struts.devMode" value="true"/>
<!-- 开发模式下打印详细的错误信息 -->
<constant name="struts.ui.theme" value="xhtml"/>
<package name="user" namespace="/user" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="success" type="redirectAction">user_queryAllUser.action</result>
<result name="input">/index.jsp</result>
<result name="userList">/userList.jsp</result>
<result name="addUser">/userAdd.jsp</result>
<result name="updateUser">/userUpdate.jsp</result>
</action>
</package>
</struts>
13、UserAction具体实现
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {
@Autowired
private UserService userService;
private User user;
private List<User> userList;
public String execute() throws Exception {
return null;
}
public String login() {
if (user != null) {
User user2 = userService.login(user);
if (user2 != null) {
return SUCCESS;
}
}
this.addFieldError("user.username", "用户名或密码错误!");
return INPUT;
}
public String addUI() {
return "addUser";
}
public String updateUI() {
user = userService.findUserById(user.getId());
return "updateUser";
}
public String add() {
userService.addUser(user);
return SUCCESS;
}
public String delete() {
userService.deleteUser(user.getId());
return SUCCESS;
}
public String update() {
userService.updateUser(user);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String queryAllUser() {
userList = userService.findAllUser();
return "userList";
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
14、登录页面的实现
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<s:head/>
</head>
<body>
<center>
<h1>用户登录</h1>
<s:a action="user_addUI" namespace="/user">添加新用户</s:a>
<s:form action="user_login" namespace="/user" method="post">
<s:textfield label="用户名" name="user.username"></s:textfield>
<s:password label="密码" name="user.password"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</center>
</body>
</html>
15、添加页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加新用户</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<center>
<h1>添加新用户</h1>
<s:form action="user_add" namespace="/user" method="post">
<s:textfield label="用户名" name="user.username"></s:textfield>
<s:password label="密码" name="user.password"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
</center>
</body>
</html>
16、修改页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>修改用户</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<center>
<h1>修改用户</h1>
<s:form action="user_update" namespace="/user" method="post">
<s:hidden name="user.id"></s:hidden>
<s:textfield label="用户名" name="user.username"></s:textfield>
<s:password label="密码" name="user.password"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
</center>
</body>
</html>
17、列表页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户列表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<center>
<h2>用户列表</h2>
<h3><s:a action="user_addUI" namespace="/user">添加新用户</s:a> </h3>
<table width="90%" border="1">
<tr>
<th>用户id</th>
<th>用户名称</th>
<th>用户密码</th>
<th>操作</th>
</tr>
<s:iterator value="userList">
<tr>
<td><s:property value="id"/> </td>
<td><s:property value="username"/> </td>
<td><s:property value="password"/> </td>
<td><s:a action="user_updateUI" namespace="/user"><s:param name="user.id">${id}</s:param>修改</s:a>
<s:a action="user_delete" namespace="/user"><s:param name="user.id">${id}</s:param>删除</s:a></td>
</tr>
</s:iterator>
</table>
</center>
</body>
</html>
参考地址http://blog.csdn.net/afgasdg/article/details/7392367
发表评论
-
生成webservices代理类方法
2013-12-15 21:06 720根据wsdl成生客户端信息: 在D盘下新建一个src目录 ... -
自动补全-jquery.autocomplete.js
2013-07-08 13:58 13770研究了三天自动补全jquery.autocomplete ... -
js设置cookie
2013-06-17 13:52 1318这几天用到cookie,所以在网上搜了一下js设置cook ... -
java 获取mac地址
2013-03-20 14:20 1403一下是用java来获取mac地址,一般可用作接口权限控制 ... -
oracle数据库-使用proxool进行中断重连
2013-02-17 11:03 3202(1)proxool.xml配置项参数 <?xml ... -
jsp自定义错误页
2013-02-17 11:00 991为了给浏览者一个友好的错误提示页面,我们可以象IIS一样自定义 ... -
Spring对Hibernate事务管理的配置
2013-01-09 15:11 1004声明式事务配置主要分以下几步: 声明式事务配置 •配置 ... -
md5
2013-01-08 10:37 1105MD5是一种常用的加密算法,在项目中经常用到,我参考了网上一些 ... -
axis 自动生成webservices客户端代理类
2012-11-18 11:16 28691.新建wsdltojava.bat文件,放到C盘,文件内容如 ... -
定时器(quartz+spring)读取数据库配置
2012-08-21 15:37 19498今天把项目中的一部分移出来,主要是实现定时器任务,定时 ... -
ztree 小实例
2012-08-03 09:55 2522今天搞了一下ztree这个开源的东东,感觉很不错,很强大 ... -
java 读取.properties文件 最简单方式
2012-07-25 08:45 1126param.properties 内容如下: url ... -
Quartz入门示例
2012-05-14 17:28 1015文章出处 http://www.cnblogs.com ... -
Struts2访问request,session,application的四种方式
2012-04-19 09:10 1594方式1--》访问Map类型的: package cn ... -
struts.xml动态返回结果
2012-04-11 15:51 1059今天有个难题,被我解决啦。详细的问题我就不多说了,用到的 ... -
springmvc上传
2012-04-05 15:55 5394最近一直在网上看资料,今天我在网上发现一个demo,是 ... -
jsoup解析网页
2012-03-31 09:24 4442jsoup功能很强大,我用它来解析网页很轻松。但我只用了 ... -
fileupload多文件上传
2012-03-31 09:16 1157以前做过单文件的 ... -
解析百度输入框(在线查询)
2012-03-28 14:51 1894百度大家都会用,但是相信大家用百度的时候在输入框中随便输 ... -
ajax struts实现在线查询功能
2012-03-27 11:58 1210我用的是ajax 和struts实现的一个在线查询功能, ...
相关推荐
struts2.2.3_+_spring3.1.0_+_mybatis3.1.0集成简单.docx,我想免费让大家下载,可以不知道怎么设置,有知道的可以私信我
Struts2.2.3、Spring3.1.0和Mybatis3.1.0是三个非常著名的开源框架,它们的集成使用在企业级Java应用开发中非常常见。本项目是一个简单的集成示例,旨在展示如何将这三个框架结合起来实现用户管理的CRUD操作,并且...
Struts2.2.3、Spring3.1.0和Mybatis3.1.0是三个流行的开源Java框架,它们分别用于MVC(Model-View-Controller)架构、依赖注入和持久层操作。将这三个框架整合在一起,可以构建出一个高效、可维护的Web应用程序。...
1.1 springframework4.3.7.RELEASE 1.2 mybatis3.1.0s MyBatis-Plus 3.1.0 1.3 shirol.3.2 1.4 servlet3.1.0 1.5 druid1.0.28 1.6 slf4j1.7.19 1.7 fastjson1.2.30 1.8 poi3.15 1.9 velocityl .7 1.10 quartz2.2.3 ...
### Memcached与Spring、Mybatis集成 #### 一、简单介绍 本文档主要讲述了如何将Memcached缓存系统与Spring框架以及MyBatis ORM工具进行集成。通过这种方式,可以有效地提高应用程序性能,尤其是在处理大量数据读取...
标题 "integer with spring struts hibernate mybatis jpa" 提示了这个压缩包包含的是一个集成多种技术的Java Web开发框架。这些技术包括Spring、Struts、Hibernate、MyBatis和JPA,它们都是Java领域中非常重要的...
SSM框架集成是Java开发中常见的技术组合,主要包括Struts2、Spring3和MyBatis3这三大组件。这个"ssm集成简单demo"提供了一个基础的示例,帮助开发者理解如何将这三个框架协同工作,构建一个完整的Web应用程序。 ...
springframework4.3.7.RELEASE mybatis3.4.1 shiro1.3.2 servlet3.1.0 druid1.0.28 slf4j1.7.19 fastjson1.2.30 poi3.15 velocity1.7 alisms1.0 quartz2.2.3 mysql5.1.39 前端使用技术 Vue2.5.1 ...
springframework4.3.7.RELEASE EhCache2.10.2 mybatis3.4.1 shiro1.3.2 servlet3.1.0 druid1.0.28 slf4j1.7.19 fastjson1.2.30 velocity1.7 quartz2.2.3 mysql5.1.39 swagger2.4 前端使用技术 Vue...
1.1 springframework4.3.7.RELEASE 1.2 mybatis3.4.1 1.3 shiro1.3.2 1.4 servlet3.1.0 1.5 druid1.0.28 1.6 slf4j1.7.19 1.7 fastjson1.2.30 1.8 poi3.15 1.9 velocity1.7 1.10 quartz2.2.3 1.11 mysql5.1.39 1.12 ...