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>");
- %>