spring + hibernate 添加用户

Employee.java:
- package com.lh.bean;
- public class Employee {
- private int id;
- private String name;
- private String sex;
- private int age;
- private String dept;
- private String duty;
- private String telephone;
- public Employee(){
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getDept() {
- return dept;
- }
- public void setDept(String dept) {
- this.dept = dept;
- }
- public String getDuty() {
- return duty;
- }
- public void setDuty(String duty) {
- this.duty = duty;
- }
- public String getTelephone() {
- return telephone;
- }
- public void setTelephone(String telephone) {
- this.telephone = telephone;
- }
- }
Employee.hbm.xml:
- <?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">
- <!-- Employee信息字段配置信息 -->
- <hibernate-mapping>
- <class name="com.lh.bean.Employee" table="tb_myemp">
- <!-- id值 -->
- <id name="id" column="id" type="int">
- <generator class="native"/>
- </id>
- <!-- 姓名 -->
- <property name="name" type="string" length="45">
- <column name="name"/>
- </property>
- <!-- 年龄 -->
- <property name="age" type="int">
- <column name="age"/>
- </property>
- <!-- 性别 -->
- <property name="sex" type="string" length="45">
- <column name="sex"/>
- </property>
- <!-- 部门 -->
- <property name="dept" type="string" >
- <column name="dept" />
- </property>
- <!-- 职务 -->
- <property name="duty" type="string">
- <column name="duty" />
- </property>
- <!-- 联系电话 -->
- <property name="telephone">
- <column name="telephone"></column>
- </property>
- </class>
- </hibernate-mapping>
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.lh.bean.Employee;
- public class EmpDao extends HibernateDaoSupport {
- public void addEmp(Employee emp){
- this.getHibernateTemplate().save(emp);
- }
- }
applicationcontenxt.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- 配置数据源 -->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/test
- </value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>001052</value>
- </property>
- </bean>
- <!-- 定义Hibernate的sessionFactory -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <!-- 数据库连接方言 -->
- <prop key="dialect">org.hibernate.dialect.SQLServerDialect</prop>
- <!-- 在控制台输出SQL语句 -->
- <prop key="hibernate.show_sql">true</prop>
- <!-- 格式化控制台输出的SQL语句 -->
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- <!--Hibernate映射文件 -->
- <property name="mappingResources">
- <list>
- <value>com/lh/bean/Employee.hbm.xml</value>
- </list>
- </property>
- </bean>
- <!-- 注入SessionFactory -->
- <bean id="empDao" class="com.lh.dao.EmpDao">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
- </beans>
index.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <%
- String path = request.getContextPath();
- request.setAttribute("ContextPath", path);
- %>
- <!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>批量添加信息</title>
- </head>
- <body>
- <div id="main">
- <form action="${ContextPath}/save.do" method="post">
- <br />
- <table>
- <tr>
- <td width="120px" class="td_pad">用户名称:</td>
- <td><input type="text" name="name" value="" /></td>
- </tr>
- <tr>
- <td width="120px" class="td_pad">职务:</td>
- <td><input type="text" name="business" value="" /></td>
- </tr>
- <tr>
- <td width="120px" class="td_pad">添加信息数量:</td>
- <td><input type="text" name="count" value="" /></td>
- </tr>
- <tr>
- <td align="center" colspan="2" class="td_pad">
- <input type="submit" value="添加" /> <input type="reset"
- value="取消" /></td>
- </tr>
- </table>
- </form>
- </div>
- <!-- 信息添加成功后的提示信息 -->
- <script type="text/javascript">
- <c:if test="${succ!=null}">
- alert("${succ}");
- </c:if>
- </script>
- </body>
- </html>
save.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- <%@ page import="org.springframework.core.io.*"%>
- <%@ page import="org.springframework.beans.factory.BeanFactory"%>
- <%@ page import="org.springframework.beans.factory.xml.XmlBeanFactory"%>
- <%@ page import="com.lh.dao.*"%>
- <%@ page import="com.lh.bean.Employee"%>
- <%
- request.setCharacterEncoding("GBK");
- String name = request.getParameter("name");
- String sex = request.getParameter("sex");
- String age = request.getParameter("age");
- String dept = request.getParameter("dept");
- String duty = request.getParameter("duty");
- String tel = request.getParameter("telephone");
- Employee emp = new Employee();
- emp.setName(name);
- emp.setSex(sex);
- emp.setAge(Integer.parseInt(age));
- emp.setDept(dept);
- emp.setDuty(duty);
- emp.setTelephone(tel);
- Resource resource = new ClassPathResource("applicationContext.xml");
- BeanFactory factory = new XmlBeanFactory(resource);
- EmpDao dao = (EmpDao)factory.getBean("empDao");
- dao.addEmp(emp);
- out.println("<script type='text/javascript'> alert('添加成功!');window.location.href='index.jsp'</script>");
- %>
spring + hibernate 添加用户的更多相关文章
- hibernate添加spring 事务管理注意问题记录
今天弄了一天的hibernate添加事务的问题 首先,建立的是一个java工程,把hibernate添加进工程里,很容易就可以写一个增删改查的方法.索性就多加点东西,把接口,抽象类也加到里面,自己看着 ...
- 深入浅出Struts2+Spring+Hibernate框架
一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
- 第七次课:ssh的集成(SpringMV+Spring+Hibernate)
第一部分:程序结构 第二部分:配置 1.配置web.xml文件,启动spring和springMVC: 1)配置启动spring: <context-param> <param-na ...
- spring+hibernate常见异常集合
spring+hibernate出错小结: (1)java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext ...
- Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(二)
然后是项目下的文件:完整的项目请看 上一篇 Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一) 项目下的springmvc-servlet.xml配置文件: ...
- structs spring hibernate 三者之间有什么关系?
现在开发流行MVC模式,structs在C(控制器)中使用:hibernate在M(模型)中被使用:至于 spring ,最大的作用在于,structs.hibernate的对象,由于在各个层之间相互 ...
- Spring/Hibernate 应用性能优化的7种方法
对于大多数典型的 Spring/Hibernate 企业应用而言,其性能表现几乎完全依赖于持久层的性能.此篇文章中将介绍如何确认应用是否受数据库约束,同时介绍七种常用的提高应用性能的速成法.本文系 O ...
- SpringMVC+Spring+hibernate整合及分页
1. 新建web project 2. 引入jar, 3. 创建包com.tgb.web.controller, 下面创建包(dao,entity,service, config,spring,hib ...
随机推荐
- android 实践项目
电子词典 http://files.cnblogs.com/blogLYF/lyf_danci.apk
- 【第三篇】学习 android 事件总线androidEventbus之list数据事件的传递,发送list数据事件到另外一个Activity
这个和普通的事件总线的发送接收一样. package com.example.mysimpleeventbus; import java.util.ArrayList; import java.uti ...
- 标准IO库
IO标准库类型和头文件
- Sublime Text: [Decode error - output not utf-8]
今天编译Python时, 输出窗口信息出现: [Decode error - output not utf-8][Decode error - output not utf-8] 发现是print ...
- Chapter 1 First Sight——33
At that moment, the bell rang loudly, making me jump, and Edward Cullen was out of his seat. 在这个时候,铃 ...
- VS中的快捷键快速格式化代码,使好看,整齐
在VC2005中,快捷键是Ctrl + K, Ctrl + F, 这是一个组合键,即先按Ctrl + K, 这时候编辑器会等待下一个按键动作,此时再按Ctrl + F, 即可以格式化代码了,当然,也可 ...
- thinkphp整合系列之phpqrcode生成二维码
php生成二维码其实挺简单的:当然指的是使用qrcode类库: 因此关于是否要写这篇博客:我是犹豫了再三的: 不过最后还是决定写下吧:如果有童鞋急着用:就可以直接引了: 再个也可以作为即将写的文章微信 ...
- Linux中ls命令详解
ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法 英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所 ...
- hdu_1181_变形课(dfs)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 题意:中文题,不解释 题解:直接DFS #include<cstdio> #incl ...
- MySQL慢日志查询全解析:从参数、配置到分析工具【转】
转自: MySQL慢日志查询全解析:从参数.配置到分析工具 - MySQL - DBAplus社群——围绕数据库.大数据.PaaS云,运维圈最专注围绕“数据”的学习交流和专业社群http://dbap ...