关于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层 (添加方法)

 

 

数据库

 

 完毕!!

 

posted @ 2018-03-05 15:27  哈喽小伙  阅读(330)  评论(0编辑  收藏  举报