zoukankan      html  css  js  c++  java
  • SSH整合的xml和注解

    DAO:

    1 public interface IDeptDAO {
    2     public int addDept(Dept dept);
    3 }
     1 @Repository("deptDAO")
     2 public class DeptDAOImpl implements IDeptDAO {
     3     @Resource(name = "sessionFactory")  //JDK 1.4
     4             /*@Autowired()
     5             @Qualifier("sessionFactory")*/
     6     SessionFactory sessionFactory;
     7     public int addDept(Dept dept) {
     8         Session session = sessionFactory.getCurrentSession();
     9         Serializable count = session.save(dept);
    10         return (Integer)count;
    11     }
    12 
    13     public SessionFactory getSessionFactory() {
    14         return sessionFactory;
    15     }
    16 
    17     public void setSessionFactory(SessionFactory sessionFactory) {
    18         this.sessionFactory = sessionFactory;
    19     }
    20 }

    Action:

     1 @Controller("deptAction")
     2 @ParentPackage("struts-default")
     3 @Namespace("/")
     4 @Scope("prototype") //多例的
     5 public class DeptAction implements Action {
     6     private Dept dept;
     7     @Resource(name = "deptService")  // DI  依赖注入
     8     IDeptService service;
     9     @org.apache.struts2.convention.annotation.Action(value = "/add",results={@Result(name = "success",location="/jsp/index.jsp")})
    10     public String execute() throws Exception {
    11         service.addDept(dept);
    12         return SUCCESS;
    13     }
    14 
    15     public Dept getDept() {
    16         return dept;
    17     }
    18 
    19     public void setDept(Dept dept) {
    20         this.dept = dept;
    21     }
    22 
    23 }

    entity:

     1 @Entity
     2 @Table(name = "Dept")
     3 public class Dept {
     4     @Id
     5     @GeneratedValue  //native  xml中native  Oracle
     6     private Integer deptno;
     7     @Column
     8     private String deptname;
     9 
    10     public Integer getDeptno() {
    11         return deptno;
    12     }
    13 
    14     public void setDeptno(Integer deptno) {
    15         this.deptno = deptno;
    16     }
    17 
    18     public String getDeptname() {
    19         return deptname;
    20     }
    21 
    22     public void setDeptname(String deptname) {
    23         this.deptname = deptname;
    24     }
    25 }

    service:

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

    applicationContext:

     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:xsi="http://www.w3.org/2001/XMLSchema-instance"
     7        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8      http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     9      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    10      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    11 ">
    12 
    13     <context:component-scan base-package="action,dao,service"></context:component-scan>
    14 
    15 
    16 <!--
    17 1.数据源  c3p0
    18 -->
    19     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    20         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    21         <property name="driverClass" value="${jdbc.driverClass}"></property>
    22         <property name="user" value="${jdbc.username}"></property>
    23         <property name="password" value="${jdbc.password}"></property>
    24     </bean>
    25 
    26     <!--
    27  2.识别到jdbc.properties
    28 -->
    29     <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    30     <!--
    31  3.形成SessionFactory
    32 -->
    33     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    34         <property name="dataSource" ref="dataSource"></property>
    35         <property name="hibernateProperties">
    36             <props>
    37                 <!--方言-->
    38               <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    39                 <!--是否打印sql-->
    40                 <prop key="hibernate.show_sql">true</prop>
    41                 <prop key="hibernate.format_sql">true</prop>
    42                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
    43             </props>
    44         </property>
    45         <!--关联小配置-->
    46          <property name="packagesToScan" value="entity"></property>
    47     </bean>
    48 
    49 <!--
    50 7.事务管理器
    51 -->
    52     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    53         <property name="sessionFactory" ref="sessionFactory"></property>
    54     </bean>
    55 <!--
    56 8.事务真实配置
    57 -->
    58     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    59 
    60 
    61 </beans>

    web.xml:

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app>
     6   <display-name>Archetype Created Web Application</display-name>
     7   <!--Spring配置
     8  1.上下文  识别applicationContext.xml
     9 
    10  2.监听器 在Tomcat容器启动的时候,帮我创建Spring容器,并且放入application中-->
    11   <context-param>
    12     <param-name>contextConfigLocation</param-name>
    13     <param-value>classpath:applicationContext.xml</param-value>
    14   </context-param>
    15 
    16   <!--Struts2配置  核心过滤器 -->
    17   <filter>
    18     <filter-name>struts</filter-name>
    19     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    20   </filter>
    21   <filter-mapping>
    22     <filter-name>struts</filter-name>
    23     <url-pattern>/*</url-pattern>
    24   </filter-mapping>
    25 
    26   <listener>
    27     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    28   </listener>
    29 
    30 </web-app>

    pom.xml:

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3     <parent>
      4         <artifactId>Y2166</artifactId>
      5         <groupId>cn.happy</groupId>
      6         <version>1.0-SNAPSHOT</version>
      7     </parent>
      8     <modelVersion>4.0.0</modelVersion>
      9     <artifactId>15SSHAnno</artifactId>
     10     <packaging>war</packaging>
     11     <name>15SSHAnno Maven Webapp</name>
     12     <url>http://maven.apache.org</url>
     13     <dependencies>
     14         <dependency>
     15             <groupId>junit</groupId>
     16             <artifactId>junit</artifactId>
     17             <version>4.3</version>
     18             <scope>test</scope>
     19         </dependency>
     20 
     21         <!--spring配置-->
     22         <dependency>
     23             <groupId>org.springframework</groupId>
     24             <artifactId>spring-context</artifactId>
     25             <version>4.2.0.RELEASE</version>
     26         </dependency>
     27         <!--aop使用的jar-->
     28         <dependency>
     29             <groupId> org.aspectj</groupId >
     30             <artifactId> aspectjweaver</artifactId >
     31             <version> 1.8.7</version >
     32         </dependency>
     33 
     34         <!--SpringWeb-->
     35         <dependency>
     36             <groupId>org.springframework</groupId>
     37             <artifactId>spring-web</artifactId>
     38             <version>4.1.8.RELEASE</version>
     39         </dependency>
     40 
     41         <!--JavaEE-->
     42         <dependency>
     43             <groupId>javaee</groupId>
     44             <artifactId>javaee-api</artifactId>
     45             <version>5</version>
     46         </dependency>
     47 
     48         <dependency>
     49             <groupId>javax.servlet</groupId>
     50             <artifactId>jstl</artifactId>
     51             <version>1.2</version>
     52             <scope>runtime</scope>
     53         </dependency>
     54 
     55         <dependency>
     56             <groupId>org.springframework</groupId>
     57             <artifactId>spring-tx</artifactId>
     58             <version>4.2.5.RELEASE</version>
     59         </dependency>
     60 
     61         <!--c3p0-->
     62         <dependency>
     63             <groupId>com.mchange</groupId>
     64             <artifactId>c3p0</artifactId>
     65             <version>0.9.5.2</version>
     66         </dependency>
     67 
     68         <!--hibernate jar包-->
     69         <!--jta的jar包-->
     70         <dependency>
     71             <groupId>javax.transaction</groupId>
     72             <artifactId>jta</artifactId>
     73             <version>1.1</version>
     74         </dependency>
     75 
     76         <dependency>
     77             <groupId>org.hibernate</groupId>
     78             <artifactId>hibernate-core</artifactId>
     79             <version>5.0.6.Final</version>
     80         </dependency>
     81 
     82         <!--Spring-ORM-->
     83         <dependency>
     84             <groupId>org.springframework</groupId>
     85             <artifactId>spring-orm</artifactId>
     86             <version> 4.2.2.RELEASE</version>
     87         </dependency>
     88 
     89         <!--Oracle驱动的jar-->
     90         <dependency>
     91             <groupId>com.oracle</groupId>
     92             <artifactId>ojdbc6</artifactId>
     93             <version>11.2.0.1.0</version>
     94         </dependency>
     95 
     96         <dependency>
     97             <groupId>org.apache.struts</groupId>
     98             <artifactId>struts2-core</artifactId>
     99             <version>2.3.4.1</version>
    100         </dependency>
    101 
    102         <dependency>
    103             <groupId>org.apache.struts.xwork</groupId>
    104             <artifactId>xwork-core</artifactId>
    105             <version>2.3.4.1</version>
    106         </dependency>
    107 
    108         <!--Struts整合Spring的jar包-->
    109         <dependency>
    110             <groupId>org.apache.struts</groupId>
    111             <artifactId>struts2-spring-plugin</artifactId>
    112             <version>2.3.4.1</version>
    113         </dependency>
    114 
    115         <!--Struts2注解支持jar包-->
    116         <dependency>
    117             <groupId>org.apache.struts</groupId>
    118             <artifactId>struts2-convention-plugin</artifactId>
    119             <version>2.3.4.1</version>
    120         </dependency>
    121     </dependencies>
    122     <build>
    123         <resources>
    124             <resource>
    125                 <directory>src/main/java</directory>
    126                 <includes>
    127                     <include>**/*.xml</include>
    128                 </includes>
    129             </resource>
    130         </resources>
    131     </build>
    132 </project>
  • 相关阅读:
    第二阶段冲刺第1天
    每周总结(5.30)
    每周总结(5.23)
    个人作业——顶会热词进程2.3
    个人作业——顶会热词进程2.2
    c#日期相关代码
    Linux服务器安装mysql
    Linux运行yum时出现/var/run/yum.pid已被锁定,PID为xxxx的另一个程序正在运行的问题解决
    【转】火狐浏览器js转换日期问题
    docker流程
  • 原文地址:https://www.cnblogs.com/ztca/p/8508869.html
Copyright © 2011-2022 走看看