ssh分模块开发配置模版

ssh-parent

父模块,作为项目顶层,用于jar包导入

pom.xml



    4.0.0

    chzu
    ssh-parent
    pom
    1.0-SNAPSHOT

    
        ssh-dao
        ssh-service
        ssh-web
    

    ssh-parent

    
        UTF-8
        1.8
        1.8
        5.3.1.Final
        2.5.16
        5.1.46
        5.0.6.RELEASE
        2.10.0
    

    
        
        
            javax.servlet
            javax.servlet-api
            4.0.0
            provided
        

        
            javax.servlet
            jsp-api
            2.0
            provided
        

        
            javax.servlet
            jstl
            1.2
            compile
        


        
        
            mysql
            mysql-connector-java
            ${mysql-connector-java.version}
        

        
        
            junit
            junit
            4.12
        

        
        
            org.apache.logging.log4j
            log4j-core
            ${log4j.version}
        
        
            org.apache.logging.log4j
            log4j-api
            ${log4j.version}
        
             
            org.apache.logging.log4j
            log4j-slf4j-impl
            ${log4j.version}
        
        
            org.slf4j
            
            log4j-over-slf4j
            1.7.25
        
        
        
            com.lmax
            disruptor
            3.4.2
        


        
        
            org.apache.struts
            struts2-core
            ${struts2.version}
        
        
        
            org.apache.struts
            struts2-spring-plugin
            ${struts2.version}
        
        
            org.apache.struts
            struts2-convention-plugin
            ${struts2.version}
        
        
            org.apache.struts
            struts2-json-plugin
            ${struts2.version}
        

        
        
            org.hibernate
            hibernate-core
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-c3p0
            ${hibernate.version}
        
        
            c3p0
            c3p0
            0.9.1.2
        

        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
    

ssh-dao

数据访问层模块,用户与数据库交互,作为ssh-parent子模块

pom.xml



    
        ssh-parent
        chzu
        1.0-SNAPSHOT
    
    4.0.0
    ssh-dao
    
    

hibernate配置

db.properties

jdbc.url=jdbc:mysql:///maven_ssh
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root

hibernate.cfg.xml




    
        org.hibernate.dialect.MySQL5Dialect
        true
        true
        update

        
        
    

Customer.hbm.xml





    
        
            
        
        
        
    

spring配置

applicationContext-basic.xml

连接数据库基本配置




    
    

    
    
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
    


    
    
        
    

    
    
    
        
            
            
            
            
            
        
    

    
    
        
        

        
        
    

    
    
    
    



applicationContext-dao.xml

创建操作数据库对象




    
    

    
    
        
    


测试用例

Customer.java

public class Customer {

    private Integer customerId;

    private String customerName;

    private String customerPhone;
    ....setter getter

CustomerDao.java

public interface CustomerDao {

    /**
     * 根据ID查询客户
     * @param id 客户ID
     * @return 客户
     */
    Customer findById(Integer id);

}

CustomerDaoImpl.java

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

    @Override
    public Customer findById(Integer id) {
        return this.getHibernateTemplate().get(Customer.class,id);
    }
}

CustomerDaoImplTest.java

public class CustomerDaoImplTest {

    @Test
    public void findById() {

        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext-basic.xml", "applicationContext-dao.xml");

        CustomerDaoImpl customerDao = context.getBean("customerDao", CustomerDaoImpl.class);

        System.out.println(customerDao.findById(1));
    }
}

ssh-service

pom.xml



    
        ssh-parent
        chzu
        1.0-SNAPSHOT
    
    4.0.0

    ssh-service

    
        
            chzu
            ssh-dao
            1.0-SNAPSHOT
        
    

spring配置文件

applicationContext-service.xml




    
    
        
    

测试用例

CustomerService.java


public interface CustomerService {
    /**
     * 根据ID查询客户
     * @param id 客户ID
     * @return 客户
     */
    Customer findById(Integer id);
}

CustomerServiceImpl.java

public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao;

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public Customer findById(Integer id) {
        return customerDao.findById(id);
    }
}

CutomerServiceDaoImplTest.java

public class CustomerServiceImplTest {

    @Test
    public void findById() {

        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("classpath*:applicationContext-*.xml");

        CustomerService customerService =
                context.getBean("customerService", CustomerService.class);

        System.out.println(customerService.findById(1));

    }
}

ssh-web

pom.xml




    
        ssh-parent
        chzu
        1.0-SNAPSHOT
    
    4.0.0

    ssh-web
    war

    ssh-web

    
        UTF-8
        1.8
        1.8
    

    
        
            chzu
            ssh-service
            1.0-SNAPSHOT
            compile
        
    

    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
            
        
    

web.xml




    
    
        contextConfigLocation
        classpath*:applicationContext-*.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        struts2
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    
    
        struts2
        /*
    


struts.xml





    
        
        regex:.*

        
            /index.jsp
            
            
        
    

log4j2.xml



    
        
            
        
    
    
        
            
        
    

spring配置

applicationContext-action.xml




  
    
        
    


测试用例

CustomerAction.java

package chzu.action;

import chzu.bean.Customer;
import chzu.service.CustomerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {

    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    // 访问http://localhost:8080/ssh-web/customer_find?id=1
    // 访问http://localhost:8080/customer_find?id=1
    private String  id;

    public String getId() {
        return id;
    }
    // 注入属性
    public void setId(String id) {
        this.id = id;
    }

    public String find() throws Exception {

        Customer customer = customerService.findById(Integer.parseInt(id));

        ActionContext.getContext().getValueStack().set("customer",customer);

        return SUCCESS;
    }
}

index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>


    index


    姓名:



你可能感兴趣的:(ssh分模块开发配置模版)