• SSH(Spring SpringMVC Hibernate)框架整合


    项目说明:

    使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能

    项目结构

     

    1.导入依赖jar包

     1   <!--单测-->
     2         <dependency>
     3             <groupId>junit</groupId>
     4             <artifactId>junit</artifactId>
     5             <version>4.3</version>
     6             <scope>test</scope>
     7         </dependency>
     8         <!--spring配置-->
     9         <dependency>
    10             <groupId>org.springframework</groupId>
    11             <artifactId>spring-context</artifactId>
    12             <version>4.2.0.RELEASE</version>
    13         </dependency>
    14         <!--aop使用的jar-->
    15         <dependency>
    16             <groupId> org.aspectj</groupId >
    17             <artifactId> aspectjweaver</artifactId >
    18             <version> 1.8.7</version >
    19         </dependency>
    20 
    21         <!--SpringWeb-->
    22         <dependency>
    23             <groupId>org.springframework</groupId>
    24             <artifactId>spring-web</artifactId>
    25             <version>4.1.8.RELEASE</version>
    26         </dependency>
    27 
    28         <!--JavaEE-->
    29         <dependency>
    30             <groupId>javaee</groupId>
    31             <artifactId>javaee-api</artifactId>
    32             <version>5</version>
    33         </dependency>
    34 
    35         <dependency>
    36             <groupId>javax.servlet</groupId>
    37             <artifactId>jstl</artifactId>
    38             <version>1.2</version>
    39             <scope>runtime</scope>
    40         </dependency>
    41 
    42         <dependency>
    43             <groupId>org.springframework</groupId>
    44             <artifactId>spring-tx</artifactId>
    45             <version>4.2.5.RELEASE</version>
    46         </dependency>
    47 
    48         <!--c3p0-->
    49         <dependency>
    50             <groupId>com.mchange</groupId>
    51             <artifactId>c3p0</artifactId>
    52             <version>0.9.5.2</version>
    53         </dependency>
    54 
    55         <!--hibernate jar包-->
    56         <!--jta的jar包-->
    57         <dependency>
    58             <groupId>javax.transaction</groupId>
    59             <artifactId>jta</artifactId>
    60             <version>1.1</version>
    61         </dependency>
    62 
    63         <dependency>
    64             <groupId>org.hibernate</groupId>
    65             <artifactId>hibernate-core</artifactId>
    66             <version>5.0.6.Final</version>
    67         </dependency>
    68 
    69         <!--Spring-ORM-->
    70         <dependency>
    71             <groupId>org.springframework</groupId>
    72             <artifactId>spring-orm</artifactId>
    73             <version> 4.2.2.RELEASE</version>
    74         </dependency>
    75 
    76 
    77         <!--SpringWebMVC-->
    78         <dependency>
    79             <groupId>org.springframework</groupId>
    80             <artifactId>spring-webmvc</artifactId>
    81             <version>4.1.8.release</version>
    82         </dependency>
    83 
    84         <!--Oracle驱动的jar-->
    85         <dependency>
    86             <groupId>com.oracle</groupId>
    87             <artifactId>ojdbc6</artifactId>
    88             <version>11.2.0.1.0</version>
    89         </dependency>

    2.创建applicationContext.xml文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:context="http://www.springframework.org/schema/context"
     4        xmlns:aop="http://www.springframework.org/schema/aop"
     5        xmlns:tx="http://www.springframework.org/schema/tx"
     6        xmlns:mvc="http://www.springframework.org/schema/mvc"
     7        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     8        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     9      http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
    10      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    11        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    12      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    13 ">
    14     <!--包扫描器-->
    15     <context:component-scan base-package="cn.happy"></context:component-scan>
    16     <!--mvc注解驱动   作用:创建7个HttpMessaeingConvert-->
    17     <mvc:annotation-driven></mvc:annotation-driven>
    18     <!--静态资源放行-->
    19     <mvc:default-servlet-handler/>
    20     <!--视图解析器-->
    21     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    22         <property name="prefix" value="/jsp/"></property>
    23         <property name="suffix" value=".jsp"></property>
    24     </bean>
    25 
    26     <!--1.Datasource-->
    27     <!--1.配置数据源c3p0-->
    28     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    29         <property name="driverClass" value="${jdbc.driverClassName}"/>
    30         <property name="user" value="${jdbc.username}"/>
    31         <property name="password" value="${jdbc.password}"/>
    32         <property name="jdbcUrl" value="${jdbc.url}"/>
    33     </bean>
    34 
    35     <!--jdbc.properties-->
    36     <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    37 
    38     <!--2.SessionFactory         类:Local-->
    39     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    40         <property name="dataSource" ref="dataSource"></property>
    41         <property name="hibernateProperties">
    42             <props>
    43                 <!--hibernate.xxxxxx必须以hibernate-->
    44                 <prop key="hibernate.show_sql">true</prop>
    45                 <prop key="hibernate.format_sql">true</prop>
    46                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    47                 <!--with current thread bind session和线程绑定的session-->
    48                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
    49             </props>
    50         </property>
    51         <!--扫描小配置文件 所有的hbm文件-->
    52         <property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>
    53         <!--去扫描指定包下带有注解的类-->
    54         <!--<property name="packagesToScan">
    55             <list>
    56                 <value>cn.happy.bean</value>
    57             </list>
    58         </property>-->
    59     </bean>
    60 
    61     <!--3.dao-->
    62     <bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl">
    63         <property name="sessionFactory" ref="sessionFactory"></property>
    64     </bean>
    65 
    66     <!--4.service-->
    67     <bean id="deptService" class="cn.happy.service.DeptServiceImpl">
    68         <property name="dao" ref="deptDAO"></property>
    69     </bean>
    70     <!--  5.事务管理器    -->
    71     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    72         <property name="sessionFactory" ref="sessionFactory"></property>
    73     </bean>
    74 
    75     <!--6.事务-->
    76     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    77     <!--  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    78           <tx:attributes>
    79               <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
    80           </tx:attributes>
    81       </tx:advice>
    82 
    83       <aop:config>
    84           &lt;!&ndash;配置了切点Pointcut&ndash;&gt;
    85          <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
    86         &lt;!&ndash; 顾问&ndash;&gt;
    87          <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
    88      </aop:config>-->
    89 </beans>

    3.搭建分层

    4.创建实体类Dept

    1 /**
    2  * Created by Administrator on 2017/12/24.
    3  * 部门表
    4  */
    5 public class Dept {
    6     private Integer deptno;  //部门编号
    7     private String dname;  //部门名称
    8     private String loc;   //部门位置
    }

    5.创建映射文件Dept.hbm.xml

     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" dynamic-update="true">
     9         <!--底层数据表对应的主键-->
    10         <id name="deptno" column="DEPTNO">
    11             <!--主键生成策略: assigned  程序员手动给值-->
    12             <generator class="native"/>
    13         </id>
    14         <property name="dname" column="DNAME"></property>
    15         <property name="loc" column="LOC"></property>
    16     </class>
    17 
    18 </hibernate-mapping>

    6.dao层

    IDeptDAO

    1 public interface IDeptDAO {
    2     public int addDept(Dept dept);
    3 }

    DeptDAOImpl:

     1 public class DeptDAOImpl implements IDeptDAO{
     2     private SessionFactory sessionFactory;
     3     public int addDept(Dept dept) {
     4         Serializable count = sessionFactory.getCurrentSession().save(dept);
     5         return (Integer)count;
     6     }
     7 
     8     public SessionFactory getSessionFactory() {
     9         return sessionFactory;
    10     }
    11 
    12     public void setSessionFactory(SessionFactory sessionFactory) {
    13         this.sessionFactory = sessionFactory;
    14     }
    15 }

    service层

    IDeptService

    1 public interface IDeptService {
    2     public int addDept(Dept dept);
    3 }

    DeptServiceImpl

     1 public class DeptServiceImpl implements IDeptService {
     2     private IDeptDAO dao;
     3     @Transactional
     4     public int addDept(Dept dept) {
     5         return dao.addDept(dept);
     6     }
     7 
     8     public IDeptDAO getDao() {
     9         return dao;
    10     }
    11 
    12     public void setDao(IDeptDAO dao) {
    13         this.dao = dao;
    14     }
    15 }

    Controller层

    DeptController:

     1 @Controller
     2 public class DeptController {
     3 
     4     private IDeptService deptService;
     5     @RequestMapping("/adddept")
     6         public String addDept(Dept dept){
     7         deptService.addDept(dept);
     8         return "success";
     9     }
    10 }

    add.jsp

    执行后sql语句:

  • 相关阅读:
    [UGUI]事件机制
    [C#]CompareTo
    [Unity算法]交换排序(二):快速排序
    [Unity算法]交换排序(一):冒泡排序
    [Lua]string(二):string.sub处理中文
    [Lua]string(一):string库
    [Unity热更新]LuaFramework14.打包、加载和卸载策略
    [Unity热更新]LuaFramework13.事件派发
    数仓day01
    大数据学习day39----数据仓库02------1. log4j 2. 父子maven工程(子spring项目的创建)3.项目开发(埋点日志预处理-json数据解析、清洗过滤、数据集成实现、uid回补)
  • 原文地址:https://www.cnblogs.com/liutao1122/p/8214144.html
Copyright © 2020-2023  润新知