Controller:
1 @Controller 2 public class DeptController { 3 @Resource(name = "deptService") 4 DeptService deptservice; 5 6 @RequestMapping("/DeptAdd") 7 @ResponseBody 8 public Object userAdd(Model model, Dept dept, HttpSession session) { 9 System.out.println(dept.getDeptname()); 10 System.out.println(dept.getDeptno()); 11 Integer flag = deptservice.AddDept(dept); 12 if (flag > 0) { 13 14 return true; 15 16 } else { 17 return false; 18 } 19 20 } 21 }
DAO:
1 public interface DeptDAO { 2 public Integer AddDept(Dept dept); 3 4 }
1 public class DeptDAOImpl implements DeptDAO { 2 3 4 private SessionFactory sessionFactory; 5 6 public void setSessionFactory(SessionFactory sessionFactory) { 7 this.sessionFactory = sessionFactory; 8 } 9 10 public SessionFactory getSessionFactory() { 11 return sessionFactory; 12 } 13 14 public Integer AddDept(Dept dept) { 15 Serializable count = sessionFactory.getCurrentSession().save(dept); 16 return (Integer)count; 17 } 18 }
entity:
1 public class Dept { 2 3 private Integer deptno; 4 5 private String deptname; 6 7 8 9 public Integer getDeptno() { 10 return deptno; 11 } 12 13 public void setDeptno(Integer deptno) { 14 this.deptno = deptno; 15 } 16 17 public String getDeptname() { 18 return deptname; 19 } 20 21 public void setDeptname(String deptname) { 22 this.deptname = deptname; 23 } 24 }
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="cn.happy.entity"> 7 <!--实体 name=实体端的内容 column=DB端的内容--> 8 <class name="Dept" table="DEPT" schema="system"> 9 <!--和底层数据表对应的主键 业务意义--> 10 <id name="deptno" column="deptno" length="4"> 11 <!--主键生成策略 :assigned:程序员手动给值--> 12 <!--<generator class="assigned"/>--> 13 <generator class="native"></generator> 14 </id> 15 <property name="deptname" ></property> 16 17 </class> 18 19 </hibernate-mapping>
service:
1 public interface DeptService { 2 public Integer AddDept(Dept dept); 3 }
1 @Service("deptService") 2 public class DeptServiceImpl implements DeptService { 3 /* @Resource( 4 name = "DeptDAO" 5 )*/ 6 7 private DeptDAO dao; 8 @Transactional 9 public void setDao(DeptDAOImpl dao) { 10 this.dao = dao; 11 } 12 13 14 15 public Integer AddDept(Dept dept) { 16 17 18 19 return dao.AddDept(dept); 20 } 21 }
applicationContext.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 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 8 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 15 <!--包扫描器--> 16 <context:component-scan base-package="cn.happy"></context:component-scan> 17 <!--mvc注解驱动 作用:创建7个HttpMessaeingConvert--> 18 <mvc:annotation-driven></mvc:annotation-driven> 19 <!--视图解析器--> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 21 <property name="prefix" value="/jsp/"></property> 22 <property name="suffix" value=".jsp"></property> 23 </bean> 24 25 <!--1.Datasource--> 26 <!--1.配置数据源c3p0--> 27 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 28 <property name="driverClass" value="${jdbc.driverClass}"/> 29 <property name="user" value="${jdbc.username}"/> 30 <property name="password" value="${jdbc.password}"/> 31 <property name="jdbcUrl" value="${jdbc.url}"/> 32 </bean> 33 34 <!--jdbc.properties--> 35 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> 36 37 <!--2.SessionFactory 类:Local--> 38 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 39 <property name="dataSource" ref="dataSource"></property> 40 <property name="hibernateProperties"> 41 <props> 42 <!--hibernate.xxxxxx必须以hibernate--> 43 <prop key="hibernate.show_sql">true</prop> 44 <prop key="hibernate.format_sql">true</prop> 45 <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</prop> 46 <!--with current thread bind session和线程绑定的session--> 47 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> 48 </props> 49 </property> 50 <!--扫描小配置文件 所有的hbm文件--> 51 <property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property> 52 <!--去扫描指定包下带有注解的类--> 53 <!--<property name="packagesToScan"> 54 <list> 55 <value>cn.happy.bean</value> 56 </list> 57 </property>--> 58 </bean> 59 60 <!--3.dao--> 61 <bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl"> 62 <property name="sessionFactory" ref="sessionFactory"></property> 63 </bean> 64 65 <!--4.service--> 66 <bean id="deptService" class="cn.happy.service.DeptServiceImpl"> 67 <property name="dao" ref="deptDAO"></property> 68 </bean> 69 70 <!--5.Controller--> 71 72 <!-- 5.事务管理器 --> 73 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 74 <property name="sessionFactory" ref="sessionFactory"></property> 75 </bean> 76 77 <!--6.事务--> 78 <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> 79 <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> 80 <tx:attributes> 81 <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/> 82 </tx:attributes> 83 </tx:advice> 84 85 <aop:config> 86 <!–配置了切点Pointcut–> 87 <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/> 88 <!– 顾问–> 89 <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor> 90 </aop:config>--> 91 92 </beans>
jdbc.properties:
1 jdbc.driverClass=oracle.jdbc.OracleDriver 2 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl 3 jdbc.username=system 4 jdbc.password=117451
jsp页面:
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 <%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> 3 <html> 4 <head> 5 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script> 6 <script type="text/javascript"> 7 function addDept(){ 8 var dept=$('#dept').serializeArray(); 9 //返回数据结构,是json数组,每个对像分别name和value为key,代表这个表单元素的name和value 10 alert(dept) 11 $.post('${pageContext.request.contextPath}/DeptAdd',dept,function(data){ 12 if(data==true){ 13 alert('添加成功'); 14 }else { 15 alert('添加失败'); 16 } 17 }) 18 } 19 20 </script> 21 </head> 22 <body> 23 <h2>Hello World!</h2> 24 <form id="dept" action="" method="post"> 25 <input type="text" name="deptno"> 26 <input type="text" name="deptname"> 27 28 <input type="button" value="保存" onclick="addDept()" /> 29 </form> 30 </body> 31 </html>