maven搭建ssh

maven搭建ssh_第1张图片

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.itcast.maven2groupId>
    <artifactId>maven-ssh3artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>

    
    <properties>
        <spring.version>4.2.4.RELEASEspring.version>
        <hibernate.version>5.0.7.Finalhibernate.version>
        <struts.version>2.3.24struts.version>
        <mysql.version>5.1.6mysql.version>
        <c3p0.version>0.9.1.2c3p0.version>
        <servlet-api.version>2.5servlet-api.version>
        <jsp-api.version>2.0jsp-api.version>
        <slf4j.version>1.7.2slf4j.version>
        <jstl.version>1.2jstl.version>
    properties>
    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-ormartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-beansartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-coreartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-context-supportartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jmsartifactId>
                <version>${spring.version}version>
            dependency>

            
            <dependency>
                <groupId>org.hibernategroupId>
                <artifactId>hibernate-coreartifactId>
                <version>${hibernate.version}version>
            dependency>

            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            
            <dependency>
                <groupId>c3p0groupId>
                <artifactId>c3p0artifactId>
                <version>${c3p0.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-coreartifactId>
                <version>${struts.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-spring-pluginartifactId>
                <version>${struts.version}version>
            dependency>
            
            <dependency>
                <groupId>org.apache.strutsgroupId>
                <artifactId>struts2-json-pluginartifactId>
                <version>${struts.version}version>
            dependency>

            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>servlet-apiartifactId>
                <version>${servlet-api.version}version>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jsp-apiartifactId>
                <version>${jsp-api.version}version>
            dependency>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>jstlartifactId>
                <version>${jstl.version}version>
            dependency>
            
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>${slf4j.version}version>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
        dependency>

        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        

        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
        dependency>
        
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-coreartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-spring-pluginartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.strutsgroupId>
            <artifactId>struts2-json-pluginartifactId>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jsp-apiartifactId>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.9version>
            <scope>testscope>
        dependency>

    dependencies>
    <build>
        
        <finalName>maven-sshfinalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.2version>
                <configuration>
                    
                    <port>80port>
                    
                    <path>/path>
                configuration>
            plugin>
        plugins>
    build>
project>

project>

2:web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    
    <listener>
        
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        
        <param-value>classpath:spring/applicationContext-*.xmlparam-value>
    context-param>
    
    <filter>
        <filter-name>OpenSessionInViewfilter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
    filter>

    <filter-mapping>
        <filter-name>OpenSessionInViewfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

     <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>

web-app>

3:domain

public class CstCustomer implements Serializable {
    private Long custId;
    private String custName;
    private String custLinkman;
    private String custPhone;
    private String custMobile;
    private Date custCreatetime;
    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 getCustLinkman() {
        return custLinkman;
    }
    public void setCustLinkman(String custLinkman) {
        this.custLinkman = custLinkman;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    public String getCustMobile() {
        return custMobile;
    }
    public void setCustMobile(String custMobile) {
        this.custMobile = custMobile;
    }
    public Date getCustCreatetime() {
        return custCreatetime;
    }
    public void setCustCreatetime(Date custCreatetime) {
        this.custCreatetime = custCreatetime;
    }

}

4:dao

@Repository
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

    @Override
    public CstCustomer findCustomerById(Long custId) {
        return this.getHibernateTemplate().get(CstCustomer.class, custId);
    }

}

5:applicationContext-db.xml

    
    <context:property-placeholder location="classpath:db.properties" />

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    bean>

    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="configLocations" value="classpath:hibernate/hibernate.cfg.xml" />
    bean>

beans>

6:db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/maven
jdbc.username=root
jdbc.password=root

7:log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=TRACE

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug, stdout

8:hibernate.cfg.xml




<hibernate-configuration>
    
    <session-factory>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectproperty>

        
        <property name="hibernate.show_sql">trueproperty>
        
        <property name="hibernate.format_sql">trueproperty>

        <property name="hibernate.hbm2ddl.auto">noneproperty>

        
        
        <property name="hibernate.connection.isolation">4property>
        
        
        
        <property name="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContextproperty>

        
        <mapping resource="com/itcast/ssh/domain/CstCustomer.hbm.xml" />
    session-factory>
hibernate-configuration>

9:CstCustomer.hbm.xml




<hibernate-mapping>
    <class name="com.itcast.ssh.domain.CstCustomer" table="cst_customer"
        catalog="maven" optimistic-lock="version">
        <id name="custId" type="java.lang.Long">
            <column name="cust_id" />
            <generator class="identity" />
        id>

        <property name="custName" type="string">
            <column name="cust_name" length="32" not-null="true">column>
        property>
        <property name="custLinkman" type="string">
            <column name="cust_linkman" length="64">column>
        property>
        <property name="custPhone" type="string">
            <column name="cust_phone" length="64">column>
        property>
        <property name="custMobile" type="string">
            <column name="cust_mobile" length="16">column>
        property>
        <property name="custCreatetime" type="timestamp">
            <column name="cust_createtime" length="19">column>
        property>

    class>
hibernate-mapping>

10:service

package com.itcast.ssh.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itcast.ssh.dao.CustomerDao;
import com.itcast.ssh.domain.CstCustomer;
import com.itcast.ssh.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    public CstCustomer findCustomerById(Long custId) {
        return customerDao.findCustomerById(custId);
    }

}

11:applicationContext-service.xml

<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">

    
    <context:component-scan base-package="com.itcast.ssh.service" />
    
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        
        <property name="sessionFactory" ref="sessionFactory" />
    bean>

    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
                 <tx:method name="update*" propagation="REQUIRED" />
        tx:attributes>
    tx:advice>

    
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.itcast.ssh.service.impl.*.*(..))" />
    aop:config>

beans>

12:Action

public class CustomerAction extends ActionSupport {

    private Long custId;

    private CstCustomer customer;

    public CustomerService getCustomerService() {
        return customerService;
    }

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

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public CstCustomer getCustomer() {
        return customer;
    }

    public void setCustomer(CstCustomer customer) {
        this.customer = customer;
    }

    @Autowired
    private CustomerService customerService;

    public String findCustomerById() {
        customer = customerService.findCustomerById(custId);

        return SUCCESS;
    }

}

13:applicationContext-action.xml

"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">

    "customerAction" class="com.itcast.ssh.action.CustomerAction">
    

14:struts-common.xml




<struts>

    
    
    <constant name="struts.i18n.encoding" value="UTF-8">constant>
    
    <constant name="struts.devMode" value="false">constant>
    
    <constant name="struts.ui.theme" value="simple">constant>
    
    <constant name="struts.action.extension" value="action">constant>
    
    <constant name="struts.enable.DynamicMethodInvocation" value="true">constant>

    
    <constant name="struts.multipart.maxSize" value="4194304">constant>

    
    <package name="common" extends="struts-default">package>

struts>

15:struts-customer.xml




<struts>
    <package name="customer" extends="common" namespace="/customer">
        <action name="findCustomerById" class="customerAction" method="findCustomerById">
            <result>/jsp/findCustomerById.jsp result>
        action>



    package>

struts>

16:struts.xml



<struts>
    <include file="struts/struts-common.xml">include>
    <include file="struts/struts-customer.xml">include>
struts>

17:jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试title>
head>
<body>

${customer.custName}
body>
html>

你可能感兴趣的:(maven)