spring + hibernate 添加用户

Employee.java:

  1. package com.lh.bean;  
  2.   
  3. public class Employee {  
  4.     private int id;  
  5.     private String name;  
  6.     private String sex;  
  7.     private int age;  
  8.     private String dept;  
  9.     private String duty;  
  10.     private String telephone;  
  11.     public Employee(){  
  12.           
  13.     }  
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(int id) {  
  18.         this.id = id;  
  19.     }  
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.     public void setName(String name) {  
  24.         this.name = name;  
  25.     }  
  26.     public String getSex() {  
  27.         return sex;  
  28.     }  
  29.     public void setSex(String sex) {  
  30.         this.sex = sex;  
  31.     }  
  32.     public int getAge() {  
  33.         return age;  
  34.     }  
  35.     public void setAge(int age) {  
  36.         this.age = age;  
  37.     }  
  38.     public String getDept() {  
  39.         return dept;  
  40.     }  
  41.     public void setDept(String dept) {  
  42.         this.dept = dept;  
  43.     }  
  44.     public String getDuty() {  
  45.         return duty;  
  46.     }  
  47.     public void setDuty(String duty) {  
  48.         this.duty = duty;  
  49.     }  
  50.     public String getTelephone() {  
  51.         return telephone;  
  52.     }  
  53.     public void setTelephone(String telephone) {  
  54.         this.telephone = telephone;  
  55.     }  
  56.       
  57. }  


 

Employee.hbm.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3.  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4.  <!-- Employee信息字段配置信息 -->  
  5.  <hibernate-mapping>  
  6.     <class name="com.lh.bean.Employee" table="tb_myemp">  
  7.         <!-- id值 -->  
  8.         <id name="id" column="id" type="int">  
  9.             <generator class="native"/>  
  10.         </id>  
  11.         <!-- 姓名 -->  
  12.         <property name="name" type="string" length="45">  
  13.             <column name="name"/>  
  14.         </property>  
  15.         <!-- 年龄 -->  
  16.         <property name="age" type="int">  
  17.             <column name="age"/>  
  18.         </property>  
  19.         <!-- 性别 -->  
  20.         <property name="sex" type="string" length="45">  
  21.             <column name="sex"/>  
  22.         </property>  
  23.         <!-- 部门 -->  
  24.         <property name="dept" type="string" >  
  25.             <column name="dept" />  
  26.         </property>  
  27.         <!-- 职务 -->  
  28.         <property name="duty" type="string">  
  29.             <column name="duty" />  
  30.         </property>  
  31.         <!-- 联系电话 -->  
  32.         <property name="telephone">  
  33.             <column name="telephone"></column>  
  34.         </property>  
  35.     </class>  
  36.  </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:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  5.     <!-- 配置数据源 -->  
  6.     <bean id="dataSource"  
  7.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  8.         <property name="driverClassName">  
  9.             <value>com.mysql.jdbc.Driver</value>  
  10.         </property>  
  11.         <property name="url">  
  12.             <value>jdbc:mysql://localhost:3306/test  
  13.             </value>  
  14.         </property>  
  15.         <property name="username">  
  16.             <value>root</value>  
  17.         </property>  
  18.         <property name="password">  
  19.             <value>001052</value>  
  20.         </property>  
  21.     </bean>  
  22.     <!-- 定义Hibernate的sessionFactory -->  
  23.     <bean id="sessionFactory"  
  24.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  25.         <property name="dataSource">  
  26.             <ref bean="dataSource" />  
  27.         </property>  
  28.         <property name="hibernateProperties">  
  29.             <props>  
  30.                 <!-- 数据库连接方言 -->  
  31.                 <prop key="dialect">org.hibernate.dialect.SQLServerDialect</prop>  
  32.                 <!-- 在控制台输出SQL语句 -->  
  33.                 <prop key="hibernate.show_sql">true</prop>  
  34.                 <!-- 格式化控制台输出的SQL语句 -->  
  35.                 <prop key="hibernate.format_sql">true</prop>  
  36.             </props>  
  37.         </property>  
  38.         <!--Hibernate映射文件 -->  
  39.         <property name="mappingResources">  
  40.             <list>  
  41.                 <value>com/lh/bean/Employee.hbm.xml</value>  
  42.             </list>  
  43.         </property>  
  44.     </bean>  
  45.     <!-- 注入SessionFactory -->  
  46.     <bean id="empDao" class="com.lh.dao.EmpDao">  
  47.         <property name="sessionFactory">  
  48.             <ref local="sessionFactory" />  
  49.         </property>  
  50.     </bean>  
  51. </beans>  


 

index.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  4. <%  
  5.     String path = request.getContextPath();  
  6.     request.setAttribute("ContextPath", path);  
  7. %>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  9. <html>  
  10. <head>  
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  12. <title>批量添加信息</title>  
  13. </head>  
  14. <body>  
  15. <div id="main">  
  16. <form action="${ContextPath}/save.do" method="post">  
  17. <br />  
  18. <table>  
  19.     <tr>  
  20.         <td width="120px" class="td_pad">用户名称:</td>  
  21.         <td><input type="text" name="name" value="" /></td>  
  22.     </tr>  
  23.     <tr>  
  24.         <td width="120px" class="td_pad">职务:</td>  
  25.         <td><input type="text" name="business" value="" /></td>  
  26.     </tr>  
  27.     <tr>  
  28.         <td width="120px" class="td_pad">添加信息数量:</td>  
  29.         <td><input type="text" name="count" value="" /></td>  
  30.     </tr>  
  31.     <tr>  
  32.         <td align="center" colspan="2" class="td_pad">  
  33.         <input type="submit" value="添加" />    <input type="reset"  
  34.             value="取消" /></td>  
  35.     </tr>  
  36. </table>  
  37. </form>  
  38. </div>  
  39. <!-- 信息添加成功后的提示信息 -->  
  40. <script type="text/javascript">  
  41.         <c:if test="${succ!=null}">  
  42.             alert("${succ}");  
  43.         </c:if>     
  44. </script>  
  45. </body>  
  46. </html>  


 save.jsp:

    1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
    2. <%@ page  import="org.springframework.core.io.*"%>  
    3. <%@ page  import="org.springframework.beans.factory.BeanFactory"%>  
    4. <%@ page  import="org.springframework.beans.factory.xml.XmlBeanFactory"%>  
    5. <%@ page  import="com.lh.dao.*"%>  
    6. <%@ page  import="com.lh.bean.Employee"%>  
    7. <%  
    8.     request.setCharacterEncoding("GBK");  
    9.     String name = request.getParameter("name");  
    10.     String sex = request.getParameter("sex");  
    11.     String age = request.getParameter("age");  
    12.     String dept = request.getParameter("dept");  
    13.     String duty = request.getParameter("duty");  
    14.     String tel = request.getParameter("telephone");  
    15.     Employee emp = new Employee();  
    16.     emp.setName(name);  
    17.     emp.setSex(sex);  
    18.     emp.setAge(Integer.parseInt(age));  
    19.     emp.setDept(dept);  
    20.     emp.setDuty(duty);  
    21.     emp.setTelephone(tel);  
    22.     Resource resource = new ClassPathResource("applicationContext.xml");   
    23.     BeanFactory factory = new XmlBeanFactory(resource);   
    24.     EmpDao dao = (EmpDao)factory.getBean("empDao");  
    25.     dao.addEmp(emp);    
    26.     out.println("<script type='text/javascript'> alert('添加成功!');window.location.href='index.jsp'</script>");  
    27. %
原文地址:https://www.cnblogs.com/henuyuxiang/p/4110399.html