• 关于SSH框架的注解版整合


     整合过程和XML版的大抵相同

    直接整合:

    第一步:引入节点(与上一篇的节点相同)

    第二步:分层

    @Repository("deptDao")
    public class DeptDaoImpl implements IDeptDao {
        @Resource(name = "sessionFactory")
        SessionFactory sessionFactory;
        public int add(Dept dept) {
            Session session = sessionFactory.getCurrentSession();
            Serializable count = session.save(dept);
            return (Integer)count;
        }
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    }
    

      

    @Entity
    @Table(name = "Dept")
    public class Dept implements Serializable{
    
        @Id
        @GeneratedValue
        private Integer dno;
        @Column
        private String dname;
    
        public Integer getDno() {
            return dno;
        }
    
        public void setDno(Integer dno) {
            this.dno = dno;
        }
    
        public String getDname() {
            return dname;
        }
    
        public void setDname(String dname) {
            this.dname = dname;
        }
    }
    

      

    @Service("dpetService")
    public class DeptServiceImpl implements IDeptService {
        @Resource(name = "deptDao")
        DeptDaoImpl dao;
        @Transactional
        public int add(Dept dept) {
            return dao.add(dept);
        }
    
        public DeptDaoImpl getDao() {
            return dao;
        }
    
        public void setDao(DeptDaoImpl dao) {
            this.dao = dao;
        }
    }
    

      

    第三步:配置文件

     

    <context:component-scan base-package="cn.com"></context:component-scan>
    
    <!--数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.dirverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!--识别jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    
    <!--sessionfactory工厂-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
        <!--扫描小配置文件-->
        <property name="packagesToScan" value="cn.com.entity"></property>
    </bean>
    
    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!--事务注解配置-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    

      

    第四步:web.xml (与上一篇相同的web.xml)

    第五步:action层

     

    @Controller
    @ParentPackage("struts-default")
    @Namespace("/")
    @Scope("prototype")
    public class DeptAction implements Action {
    
        private Dept dept;
        @Resource(name = "dpetService")
        IDeptService service;
    
        @org.apache.struts2.convention.annotation.Action(value = "addDept",results={@Result(name = "success",location = "/index.jsp")})
        public String execute() throws Exception {
            service.add(dept);
            return SUCCESS;
        }
    
        public Dept getDept() {
            return dept;
        }
    
        public void setDept(Dept dept) {
            this.dept = dept;
        }
    
        public IDeptService getService() {
            return service;
        }
    
        public void setService(IDeptService service) {
            this.service = service;
        }
    }
    

      

    第六步:UI层 (添加方法)

     

    数据库

     完毕!!

  • 相关阅读:
    HDU.5765.Bonds(DP 高维前缀和)
    SPOJ.TLE
    LOJ.2585.[APIO2018]新家(二分 线段树 堆)
    BZOJ.2679.Balanced Cow Subsets(meet in the middle)
    BZOJ.3293.[CQOI2011]分金币(思路)
    BZOJ.4558.[JLOI2016]方(计数 容斥)
    BZOJ.3631.[JLOI2014]松鼠的新家(树上差分)
    BZOJ.1568.[JSOI2008]Blue Mary开公司(李超线段树)
    BZOJ.1071.[SCOI2007]组队(思路)
    BZOJ.4910.[SDOI2017]苹果树(树形依赖背包 DP 单调队列)
  • 原文地址:https://www.cnblogs.com/ruiannan/p/8509206.html
Copyright © 2020-2023  润新知