简单实现添加用户功能,仅供初学者参考,可自行扩展程序功能(增删改查)。

这里贴下代码,需要的可以下载看(因为比较懒)。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
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/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 引入外部配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- 配置Hibernate相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property> <!-- 加载Hibernate中的映射文件 -->
<property name="mappingResources">
<list>
<value>cn/bj/ssh/entity/User.hbm.xml</value>
</list>
</property> </bean>
<!-- 配置Action类 -->
<bean id="userAction" class="cn.bj.ssh.action.UserAction" scope="prototype">
<!-- 手动注入service -->
<property name="userService" ref="userService"/>
</bean> <!-- 配置业务的类 -->
<bean id="userService" class="cn.bj.ssh.service.UserService">
<property name="userDao" ref="userDao"/>
</bean> <!-- 配置DAO的类 -->
<bean id="userDao" class="cn.bj.ssh.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 开启注解事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

连接数据库配置:jdbc.properties

# JDBC Configuration
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssh
jdbc.username=root
jdbc.password=root

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> <!-- 交由spring管理 直接写id即可 -->
<package name="ssh" extends="struts-default" namespace="/">
<action name="user_*" class="userAction" method="{1}">
<result name="loginSuccess">/index.jsp</result>
<!-- <result name="success_save">/index.jsp</result>-->
</action>
</package> </struts>

UserAction.java

package cn.bj.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import cn.bj.ssh.entity.User;
import cn.bj.ssh.service.UserService; public class UserAction extends ActionSupport implements ModelDriven<User>{ private static final long serialVersionUID = 1L;
//模型驱动使用的类
private User user = new User(); //自动注入
private UserService userService; public void setUserService(UserService userService) {
this.userService = userService;
} @Override
public User getModel() {
return user;
} //保存用户
public String save(){
userService.save(user);
return "loginSuccess";
} }

UserService.java(由于比较简单,看起来更直观,service和dao就没有写接口)

package cn.bj.ssh.service;

import org.springframework.transaction.annotation.Transactional;

import cn.bj.ssh.dao.UserDao;
import cn.bj.ssh.entity.User; @Transactional
public class UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void save(User user){
userDao.save(user);
} }

UserDao.java

package cn.bj.ssh.dao;

import org.hibernate.SessionFactory;

import cn.bj.ssh.entity.User;

public class UserDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} public String save(User user) {
this.sessionFactory.getCurrentSession().save(user);
return "success_save";
} }

实体类User.jsp

package cn.bj.ssh.entity;

public class User {

    private Integer pid;
private String name;
private String password;
private Double height; public User(){} public User(Integer pid, String name, String password,Double height) {
super();
this.pid = pid;
this.name = name;
this.password = password;
this.height = height;
} public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
} }

映射文件:User.hbm.xml

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<h1>保存用户页面</h1>
<s:form action="user_save" method="post" namespace="/" theme="simple">
<table border="1" width="400">
<tr>
<td>用户名</td>
<td><s:textfield name="name" /></td>
</tr>
<tr>
<td>用户密码</td>
<td><s:textfield name="password" /></td>
</tr>
<tr>
<td>用户身高</td>
<td><s:textfield name="height" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加" /></td>
</tr>
</table>
</s:form>
</body>
</html>

添加用户页面:addUser.jsp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cn.bj.ssh.entity.User" table="user">
<id name="pid" column="pid">
<generator class="native"/>
</id>
<property name="name" column="name" length="20"></property>
<property name="password" column="password" length="20"/>
<property name="height" column="height"/>
</class>
</hibernate-mapping>

Demo链接,有兴趣的可以下载看看。

http://share.weiyun.com/b0a8c4fb51feaed92c69af29c5232d81

struts2+spring+hibernte整合示例的更多相关文章

  1. Struts2 Spring hibernate 整合示例 .

    示例工具:MyEclipse 8.5.Tomcat 6.0.MySql 步骤: 1.创建一个WEB工程,命名为BookShop(名字自己取,此处为示例工程名): 2.导入struts2的核心jar包, ...

  2. Struts2+Spring+Hibernate整合开发(Maven多模块搭建)

    Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...

  3. WebService之Spring+CXF整合示例

    一.Spring+CXF整合示例 WebService是一种跨编程语言.跨操作系统平台的远程调用技术,它是指一个应用程序向外界暴露一个能通过Web调用的API接口,我们把调用这个WebService的 ...

  4. struts2 spring mybatis 整合(test)

    这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...

  5. Spring、Struts2+Spring+Hibernate整合步骤

    所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...

  6. Struts2框架07 Struts2 + Spring + Mybatis 整合

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  7. 三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板

    最近在学SSH三大框架的整合,在此对他们整合时相关配置文件做一简单的模板总结,方便以后复用! 首先是web.xml配置文件,这里面就配置一些简单的监听器.过滤器,包括spring核心配置文件appli ...

  8. Struts2+Spring+Hibernate3整合

    这几天正在复习三大框架的知识,特意把写出来,如有错误,希望大家多指教! 代码地址:https://git.coding.net/puchenglin/SSHDemo.git 1. 引入jar包 Str ...

  9. Struts2 + Spring + Hibernate

    Struts2 + Spring + Hibernate整合. 使用的是无配置方法进行SSH的整合,struts-convertion plugin + spring annotation + hib ...

随机推荐

  1. Mybatis框架基于注解的方式,实对数据现增删改查

    编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...

  2. CSS 的overflowhidden 属性详细解释

    overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解.      一提到清除浮动,我们就会想到另外一个 ...

  3. Linux内核分析:dup、dup2的实现

    一.首先需要看一下这两个函数的作用: #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 根据m ...

  4. python 学习笔记十五 web框架

    python Web程序 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. Python的WEB框架分为两类: 自己写socket,自 ...

  5. ImageLoader图片加载

    http://blog.csdn.net/liu1164316159/article/details/38728259       转载请注明http://write.blog.csdn.net/po ...

  6. 设计一个泛型类orderedCollection

    设计一个泛型类orderedCollection,它存储的Comparable对象的集合(在数组中),以及该集合的当前大小.提供public方法isEmpty,makeEmpty,insert,rem ...

  7. 学习视频更新管理,对于前面数据库视频资料,以及.net资料失效感到抱歉

    首先,对不起,各位网友,我知道也都是好学的的人才会来查找视频,抱歉视频失效了.以后有需要的可以常联系我,有错误定当及时改正.如有延误多多包含. 上一次发的.net学习视频失效了,我决定帮大家多找一些学 ...

  8. 利用Javascript判断操作系统的类型实现不同操作系统下的兼容性

    原文地址 http://www.jb51.net/article/33640.htm 在通过Javascript实现客户端和服务端的交互时,有时候需要对操作系统进行判断,以便实现不同操作系统下的兼容性 ...

  9. 工厂食堂3D指纹考勤系统解决方案

    指纹考勤就餐管理系统利用3D活体指纹技术完成对正式员工就餐管理.就餐者只需办理完入职手续,并登记考勤指纹,就可通过考勤指纹在工厂食堂领餐. 大多数工厂食堂就餐是福利性的,只准员工就餐,不准员工带亲戚朋 ...

  10. display inline-block 垂直居中

    table td:after {display:inline-block;width:0;height:100%;vertical-align:middle;content:'';}