首页 > 代码库 > maven开发SSH

maven开发SSH

虽然开发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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jmu.graduation</groupId>
    <artifactId>video</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>video Maven Webapp</name>
    <url>http://maven.apache.org</url>
    
    <!-- 定义常量、SSH的版本、日志系统版本 -->
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <spring.version>4.3.3.RELEASE</spring.version>  
        <hibernate.version>4.3.5.Final</hibernate.version>  
        <struts.version>2.3.30</struts.version>  
        <logback.version>1.1.6</logback.version>
        <slf4j.version>1.6.0</slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>


        <!-- struts2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
        </dependency>

        <!-- hibernate4.3.5 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
         <dependency>  
            <groupId>org.hibernate</groupId>  
            <artifactId>hibernate-c3p0</artifactId>  
            <version>${hibernate.version}</version>  
        </dependency>  

        <!-- spring4.3.3 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.30</version>
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-orm</artifactId>  
            <version>${spring.version}</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency> 

        <!-- 日志依赖包,logback需要3个包 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>${logback.version}</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>video</finalName>
    </build>
</project>

(2)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>

    <!-- 配置struts2过滤器 -->
    <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>

    <!-- Spring框架的核心监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

</web-app>

(3)jdbc.properties

jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=binjf
jdbc.password=123456

(4)logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>E:\workspace\myApp.log</file>
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

(5)applicationContext.xml

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

    <!-- 引入外部的属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置Hibernate的相关属性,需要spring-orm.jar包 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"></property>

        <!-- 配置Hibernate的属性,配置hibernate 的基本信息 -->
        <property name="hibernateProperties">
            <props>
                <!-- hibernate 所使用的数据库方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!-- 执行操作时是否在控制台打印SQL -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 是否对SQL 进行格式化 -->
                <prop key="hibernate.format_sql">true</prop>
                <!-- 指定自动生成数据表的策略。update:数据库中有该表就使用,没表就创建 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <!-- 加载Hibernate中的映射文件 -->
        <property name="mappingResources">
            <list>
                <!-- .hbm.xml的全路径,包名用"/"隔开而不是用"." -->
                <value>com/jmu/video/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置action的类(把action交给spring管理,在struts.xml配置action时,class不用写全类名,使等于bean的id即userinfoAction) -->
    <bean id="loginAction" class="com.jmu.video.action.LoginAction"
        scope="prototype">
        <!-- 手动注入service -->
        <property name="userService" ref="userService"></property>
    </bean>
    <bean id="registerAction" class="com.jmu.video.action.RegisterAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>

    <!-- 配置业务层的类 -->
    <bean id="userService" class="com.jmu.video.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <!-- 配置dao的类 -->
    <bean id="userDao" class="com.jmu.video.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

(6)struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="loginAction" class="loginAction" method="execute">
            <result name="success">success.jsp</result>
            <result name="fail">fail.jsp</result>
        </action>
        
        <action name="registerAction" class="registerAction" method="execute">
            <result name="success">success.jsp</result>
            <result name="fail">fail.jsp</result>
        </action>

        <!-- 验证码 -->
        <action name="createImageAction" class="com.jmu.video.action.CreateImageAction"
            method="execute">
            <result name="success" type="stream">
                <param name="contentType">image/jpeg</param>
                <param name="inputName">inputStream</param>
            </result>
        </action>
    </package>
</struts>

2. java文件

(1)LoginAction.java

package com.jmu.video.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jmu.video.entity.User;
import com.jmu.video.service.UserService;
import com.jmu.video.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 用户登陆处理类
 *
 * @author Sky
 * @date 2017年3月20日 上午11:04:59
 */
public class LoginAction extends ActionSupport implements ModelDriven<User> {
    
    private static final long serialVersionUID = 1L;
    
    private static final Logger log = LoggerFactory.getLogger(ActionSupport.class);
    private User user = new User();
    private UserService userService;
    
    public String execute(){
        log.info("loginAction...");
        log.info(user.getUserName());
        List<User> users = userService.findUserByName(user.getUserName());
        log.info(users.get(0).getUserName() + "==================");
        return "success";
    }

    @Override
    public User getModel() {
        return user;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

(2)UserService.java接口

package com.jmu.video.service;

import java.util.List;

import javax.transaction.Transactional;

import com.jmu.video.entity.User;

/**
 * 业务层 UserService 接口
 *
 * @author Sky
 * @date 2017年3月20日 上午11:04:33
 */
@Transactional
public interface UserService {

    List<User> findUserByName(String userName);
}

(3)UserServiceImpl.java实现类

package com.jmu.video.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jmu.video.dao.UserDao;
import com.jmu.video.entity.User;
import com.jmu.video.service.UserService;

/**
 * 业务层 UserService类
 *
 * @author Sky
 * @date 2017年3月20日 上午11:03:57
 */
public class UserServiceImpl implements UserService {
    
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> findUserByName(String userName) {
        log.info("userService....");
        return userDao.findUserByName(userName);
    }
}

(4)UserDao.java类

package com.jmu.video.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.jmu.video.entity.User;

/**
 * dao类
 *
 * @author Sky
 * @date 2017年3月20日 上午11:03:37
 */
public class UserDao extends HibernateDaoSupport{
    
    private static final Logger log = LoggerFactory.getLogger(UserDao.class);
    
    private User user;

    public List<User> findUserByName(String userName) {
        log.info("userDao...");
        String hql = "from User u where u.userName=?";
        return (List<User>) this.getHibernateTemplate().find(hql, userName);
    }
}

(5)User.java类

package com.jmu.video.entity;
/**
 * 用户实体类
 *
 * @author Sky
 * @date 2017年3月20日 上午10:39:48
 */
public class User {
    private int userId;
    private String userName;
    private String password;
    private String email;
    private String address;
    private String phone; 
    private String vocation; //职业
    
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getVocation() {
        return vocation;
    }
    public void setVocation(String vocation) {
        this.vocation = vocation;
    }
}

(6)User.hbm.xml实体类数据库映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-20 12:13:43 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.jmu.video.entity.User" table="USERINFO">
        <id name="userId" type="int">
            <column name="USERID" />
            <generator class="native" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="USERNAME" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
        <property name="phone" type="java.lang.String">
            <column name="PHONE" />
        </property>
        <property name="vocation" type="java.lang.String">
            <column name="VOCATION" />
        </property>
    </class>
</hibernate-mapping>

3. 目录结构

技术分享

 

maven开发SSH