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 ...
随机推荐
- DB9_公头_母头_串口引脚定义及RS-232串口线制作方法
RS-232连接线制作方法 材料及工具 一根双绞线(8芯).一个标准RJ45头.一个DB9孔型插头.一把RJ45专用工具.一个电烙铁及若干焊锡. 引脚定义 按以下管脚定义制作RJ45端头:I表示网络视 ...
- apache 不执行PHP,显示代码
首先检查是否安装PHP,已经安装过的话,先执行 locate libphp5.so 查看APACHE是否有SO文件,如果没有,那就要重装PHP了,先执行php -i | grep configure ...
- log4j.xml(信息打印)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...
- 找最大重复次数的数和重复次数(C++ Pair)
Problem A: 第一集 你好,世界冠军 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 265 Solved: 50[Submit][Statu ...
- PAXOS may not terminate
It’s easy to see that Paxos does have a failure mode. When two proposers are active at the same time ...
- 学习PHP函数:preg_match_all
<?php $str = '10.10.10.10, 10.10.10.11'; preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', ...
- Xcode6无法用xib得问题解决方法
1.创建一个新工程,选择singleView application 2.将storyboard和launchscreen删除,选择moveToTrash 3.删除info.plist文件中Main ...
- VMware设置NAT网络
很多初学者迷茫与如何实现虚拟机VMware与主机互联,这里小编介绍下简单实用的NAT网络. 工具/原料 VMware 方法/步骤 打开VMware,选择 编辑, 虚拟网络编辑器 默认情况下, ...
- 算法:1!+(1!+3!)+(1!+3!+5!) + ( 1! + 3! + 5! + 7! + 9!)+....+(1!+3!+5!+ ... + m!)
-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{ / ...
- 纯CSS做的一个Silder
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...