ssh整合之七注解结合xml形式

  1。在之前的ssh搭建过程中,我们是使用纯xml的方式进行编写的,这样的话,确实也挺麻烦,我们可不可以使用更简单的形式进行配置呢?

  答案是肯定的,我们可以使用我们的注解形式进行快速搭建ssh框架

  2.我们的配置文件hibernate是用jpa注解,struts是用struts的注解,spring是使用ioc和事务的注解

  首先先修改我们的hibernate中内容,即实体类

package com.itheima.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="cst_customer")
public class Customer {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="cust_id")
    private Long custId;  //客户编号
    @Column(name="cust_name")
    private String custName; //客户名称
    @Column(name="cust_source")
    private String custSource; //客户信息来源
    @Column(name="cust_industry")
    private String custIndustry; //客户所属行业
    @Column(name="cust_level")
    private String custLevel;  //客户级别
    @Column(name="cust_address")
    private String custAddress; //客户联系地址
    @Column(name="cust_phone")
    private String custPhone; //客户联系电话
    public Long getCustId() {
        return custId;
    }
    public void setCustId(Long custId) {
        this.custId = custId;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getCustSource() {
        return custSource;
    }
    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }
    public String getCustIndustry() {
        return custIndustry;
    }
    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }
    public String getCustLevel() {
        return custLevel;
    }
    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }
    public String getCustAddress() {
        return custAddress;
    }
    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    @Override
    public String toString() {
        return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
                + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
                + ", custPhone=" + custPhone + "]";
    }

}

  完了以后我们也需要修改我们的applicationContext.xml文件,开启注解扫描,开启事务,以及修改hibernate的一些信息

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring的配置文件:导入约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">

    <!-- 自定义的java对象交给spring进行管理,我们使用注解的形式替换-->
   <context:component-scan base-package="com.itheima"></context:component-scan>
   <tx:annotation-driven/>
   <!--  第三方的jar包中的java对象交给spring进行管理 -->
   <!--  创建一个hibernateTemplate对象 -->
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
         <!-- 注入sessionFactory -->
         <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!-- 创建sessionFactory对象 -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
           <!-- 配置数据库连接池 -->
           <property name="dataSource" ref="dataSource"></property>
           <!-- hibernate的配置文件 -->
           <property name="hibernateProperties">
               <props>
                   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                   <prop key="hibernate.hbm2ddl.auto">update</prop>
                   <prop key="hibernate.show_sql">true</prop>
                   <prop key="hibernate.format_sql">true</prop>
               </props>
           </property>
           <!-- 指定要扫描的包
               packagesToScan 在加载配置文件的时候,自动扫描包中的java类
           -->
        <property name="packagesToScan" value="com.itheima.entity"></property>
   </bean>
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
           <property name="url" value="jdbc:mysql:///ssh_280"></property>
           <property name="username" value="root"></property>
           <property name="password" value="root"></property>
   </bean>
   <!--配置hibernate的事务管理 -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
           <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
</beans>

  下面是CustomerAction以及CustomerServiceImpl以及CustomerDaoImpl

package com.itheima.action;

import java.util.List;

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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.entity.Customer;
import com.itheima.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@ParentPackage("struts-default")
@Namespace("/customer")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
    private Customer customer = new Customer();
    @Autowired
    private CustomerService customerService;

    private List<Customer> customers;

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public Customer getModel() {
        return customer;
    }

    //进入添加页面
    @Action(value="addCustomerUI",results={@Result(name="success",location="/jsp/customer/add.jsp")})
    public String addCustomerUI(){
        return this.SUCCESS;
    }
    //进入列表页面
    @Action(value="getAllCustomer",results={@Result(name="success",location="/jsp/customer/list.jsp")})
    public String getAllCustomer(){
        customers = customerService.getAllCustomer();
        return this.SUCCESS;
    }

}
package com.itheima.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.dao.CustomerDao;
import com.itheima.entity.Customer;
import com.itheima.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerDao customerDao;

    public void addCustomer(Customer customer) {
        customerDao.save(customer);
    }
    @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
    public List<Customer> getAllCustomer() {
        return customerDao.find();
    }
}

 

package com.itheima.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.itheima.dao.CustomerDao;
import com.itheima.entity.Customer;
@Repository
public class CustomerDaoImpl implements CustomerDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;

    public void save(Customer customer) {
        hibernateTemplate.save(customer);
    }

    public List<Customer> find() {
        return (List<Customer>) hibernateTemplate.find("from Customer");
    }
}

  到这里,我们基于注解形式的ssh整合就可以了

 

原文地址:https://www.cnblogs.com/wh-share/p/ssh_spring_hibernate_struts_xml_anno.html

时间: 2024-08-04 13:50:38

ssh整合之七注解结合xml形式的相关文章

SSH整合开发的web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.su

SSH整合案例注解式

首先准备一个实体:注解 @GeneratedValue:指定主键的生成策略.            IDENTITY:支持数据库字段自增长            SEQUENCE:支持数据库序列自增长            AUTO:使用Hibernate中的高低位算法. dao的实现类 @Repository自动装配到Spring的容器 @Resource:不用get set ,底层反射,直接就是按照名称注入. 指定bean的id的属性:name service的实现层 @Service:标识

在myeclipse中maven项目关于ssh整合时通过pom.xml导入依赖是pom.xml头部会报错

错误如下 ArtifactTransferException: Failure to transfer org.springframework:spring-jdbc:jar:3.0.5.RELEASE from http:// repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of  central has

spring的使用-ssh整合

ssh整合-xml方式: 1.需要记住的三个jar包: spring-web-4.2.4.RELEASE.jar           ---保证项目启动时就实例化spring配置的对象(通过一个servletContext监听器ContextLoaderListener实现),保证整个项目只有一个工厂. struts2-spring-plugin-2.3.24.jar ---解决了struts2和spring的整合问题,将struts2中的action交给spring创建 spring-orm-

SSH整合总结(xml与注解)

本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 一.使用XML配置: SSH版本 Struts-2.3.31 Spring-4.3.5 Hibernate-4.2.21 引入jar包 必须在WEB-INF下添加jar包(其他无效) spring以及它的依赖包 com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar hibernate的jar包及struts2的jar和插件包,包含两个重复的javassist.jar,保留高

SSH整合之全注解

  SSH整合之全注解 使用注解配置,需要我们额外引入以下jar包 我们下面从实体层开始替换注解 1 package cn.hmy.beans; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 @Entity 9 @Table 10 public c

SSH三大框架注解整合(一)

1.导入jar包,ssh的jar包一共是38个,此时还需要多加一个包,就是struts的注解插件jar. 2.在web.xml文件中配置struts filter 和spring 的listener.代码如下: <!-- spring 监听器 -->  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:applicat

SSH三大框架注解整合(二)

5.使用spring注解注入service,DAO action: @ParentPackage(value = "struts-default") @Namespace("/") @Controller @Scope("prototype") public class BookAction extends ActionSupport implements ModelDriven<Book>{ //模型驱动 public Book b

使用注解实现ssh整合

1.搭建项目: 项目名称:ss12.添加jar包    1).struts 2.3.7         --基础        asm-3.3.jar        asm-commons-3.3.jar        asm-tree-3.3.jar        commons-fileupload-1.2.2.jar        commons-io-2.0.1.jar        commons-lang3-3.1.jar        freemarker-2.3.19.jar