Seven notes of ssh integration combined with xml

Keywords: Java Hibernate Spring xml

1. In the previous pure xml mode, we have many configuration files. We can use annotation and xml to develop. In this way, our configuration files will be much less. At the same time, we can directly see the configuration in the class, so that we can quickly build a ssh integration project

First of all, we should consider: what do we need to replace with annotations?

First: the relation mapping file in hibernate. We can use jpa and Hibernate annotation to replace it

Second: in spring, our custom objects can be replaced with ioc and tx annotations in our spring

Third: our struts.xml can be replaced with the annotations of struts

This is a general idea for us. Now let's realize it!

Our entity class Customer

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;  //Customer number
    @Column(name="cust_name")
    private String custName; //Customer name
    @Column(name="cust_source")
    private String custSource; //Customer information source
    @Column(name="cust_industry")
    private String custIndustry; //Customer industry
    @Column(name="cust_level")
    private String custLevel;  //Customer level
    @Column(name="cust_address")
    private String custAddress; //Customer contact address
    @Column(name="cust_phone")
    private String custPhone; //Customer contact number
    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 + "]";
    }
}

Our applicationContext.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring Profiles for: importing constraints -->
<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
        ">
     
    <!-- Self defined java Object to spring Conduct management,We use annotations instead-->
   <context:component-scan base-package="com.itheima"></context:component-scan>
   <tx:annotation-driven/>
   <!--  The third party jar In bag java Object to spring Conduct management -->
   <!--  Create a hibernateTemplate object -->
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
         <!-- injection sessionFactory -->
         <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!-- Establish sessionFactory object -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
           <!-- Configure database connection pool -->
           <property name="dataSource" ref="dataSource"></property>
           <!-- hibernate Profile for -->
           <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>
           <!-- Specify packages to scan 
               packagesToScan When loading the configuration file, automatically scan the java class
           -->
        <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>
   <!--To configure hibernate Transaction management of -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
           <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
</beans>

Our CustomerAction

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;
    }
    //Go to add page
    @Action(value="addCustomerUI",results={@Result(name="success",location="/jsp/customer/add.jsp")})
    public String addCustomerUI(){
        return this.SUCCESS;
    }
    //Enter the list page
    @Action(value="getAllCustomer",results={@Result(name="success",location="/jsp/customer/list.jsp")})
    public String getAllCustomer(){
        customers = customerService.getAllCustomer();
        return this.SUCCESS;
    }
}

Our CustomerServiceImpl

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();
    }
}

Our CustomerDaoImpl

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");
    }
}

In this way, our annotation can be combined with xml. Next time, we will change this project to maven project

Posted by FraXTC on Wed, 01 Apr 2020 16:31:22 -0700