1. 程式人生 > >註解 SSHXML---sprong+springmvc+struts+hibirthary 註解整合

註解 SSHXML---sprong+springmvc+struts+hibirthary 註解整合

mat pan onf ons system default dialect actor content

項目架構

技術分享圖片

主要是配置文件的刪除

1.更改實體類並刪除小配置

package cn.happy.entity;

import javax.persistence.*;

/**
 * Created by CY on 2018/1/5.
 */
@Entity
@Table(name = "Dept")
public class Dept {
    @Id
    @GeneratedValue //native
    private Integer deptno;
    @Column
    private  String deptname;

    
public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } }

2.dao層的實現類添加註解

package cn.happy.dao;

import cn.happy.entity.Dept;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * Created by CY on 2018/1/5.
 */
@Repository("deptDao")
public
class DeptDaoImpl implements IDeptDao { //事件 植入SessionFactory @Resource(name = "sessionFactory") private SessionFactory sessionFactory; public void addDept(Dept dept) { Session session = sessionFactory.getCurrentSession(); session.save(dept); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }

3.service的實現類添加註解

package cn.happy.service;

import cn.happy.dao.IDeptDao;
import cn.happy.entity.Dept;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by CY on 2018/1/5.
 */
@Service("deptService")
public class DeptServiceImpl implements IDeptService {
   @Resource(name = "deptDao")
    private IDeptDao deptDao;

    @Transactional
    public void addDept(Dept dept) {
         deptDao.addDept(dept);
    }

    public IDeptDao getDeptDao() {
        return deptDao;
    }

    public void setDeptDao(IDeptDao deptDao) {
        this.deptDao = deptDao;
    }
}

4.action添加註解

package cn.happy.action;

import cn.happy.entity.Dept;
import cn.happy.service.IDeptService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * Created by CY on 2018/1/5.
 */
@Controller("deptAction")
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype")
public class DeptAction extends ActionSupport {
    private Dept dept;
    @Resource(name = "deptService")
    private IDeptService deptService;
    @Action(value = "add",results = {@Result(name = "success",location = "/jsp/index.jsp")})
    public  String add(){
        deptService.addDept(dept);
        return SUCCESS;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public IDeptService getDeptService() {
        return deptService;
    }

    public void setDeptService(IDeptService deptService) {
        this.deptService = deptService;
    }
}

5.applicationContext.xml刪除dao層和service的註入並添加包掃描器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


    <context:component-scan base-package="cn.happy"></context:component-scan>
    <!--配置數據源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--2.識別到jdbc.properties文件-->
   <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>






    <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.happy.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>






</beans>

------------------------------------------------------------------------------------------

dao

package cn.happy.dao;

import cn.happy.entity.Dept;

/**
 * Created by CY on 2018/1/5.
 */
public interface IDeptDao {
    public  void  addDept(Dept dept);
}

service

package cn.happy.service;

import cn.happy.entity.Dept;

/**
 * Created by CY on 2018/1/5.
 */
public interface IDeptService {
    public  void  addDept(Dept dept);
}

jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=happytwo
jdbc.password=happytwo

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--上下文-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <filter>
    <filter-name>Struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--監聽器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

頁面

技術分享圖片

<%@taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加部門</title>
</head>
<body>
<h2>註解版</h2>
<form method="post" action="/add">
    部門名稱:<input name="dept.deptname">
    <input type="submit" value="添加">
</form>
</body>
</html>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加部門</title>
</head>
<body>
<h2>添加成功</h2>
</body>
</html>

註解 SSHXML---sprong+springmvc+struts+hibirthary 註解整合