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. 目录结构

时间: 2024-10-13 02:47:05

maven开发SSH的相关文章

Eclipse 下用Maven构建ssh框架web工程

之前的项目中用到了maven,而且是web项目(但不是标准的ssh架构),趁着假期,重用南哥的pom文件,自己也试试用maven构建web项目,搭建完整的ssh开发框架. 工具准备:jdk,eclipse,maven Eclipse 插件:Hibernate Tools (以上工具软件请请自行Google下载安装) 当然,构建过程需要畅通的网络,以确保maven能顺利下载依赖包. 第一步,在eclipse新建Maven项目, New->Maven Project: 两个next之后选择maven

maven之ssh项目搭建

1:新建maven-archetupe-webapp项目 2:web.xml配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2

【课程分享】jQuery2.0应用开发:SSH框架整合jQuery2.0实战OA办公自动化(VSS、operamasks-UI框架)

我的qq是2059055336,对这个课程有兴趣的可以加我qq联系. 课程下载地址:链接:http://pan.baidu.com/share/link?shareid=395438909&uk=3611155194 密码:mlvl 课程下载地址:http://www.kuaipan.cn/file/id_192202874577496484.htm?source=1 一.本课程是怎么样的一门课程(全面介绍)    1.1.课程的背景 jQuery 2.0 正式版发布,不在支持 IE 6/7/8

Eclipse搭建maven开发环境

上一篇学习了maven开发环境的搭建,并且手动编写了一个maven工程,但是这样子效率很低下,今天带大家学习在eclipse下搭建maven开发环境. 常用的maven命令 mvn clean :运行清理操作,会将target文件夹中的数据删除 mvn clean compile:先运行清理操作,之后运行编译操作,会将代码编译到target文件夹中. mvn clean test: 运行清理和测试 mvn clean package : 运行清理和打包 mvn clean install : 运

Java学习篇之---Maven开发跳过单元测试

Maven开发跳过单元测试 最近做Maven项目,因为工程很庞大,每次单元测试会耗去很多时间,本着节省时间的原则,想要跳过单元测试部分,于是网上找了两种方案,分别是在DOS环境下操作Maven和在eclipse中操作Maven. Maven 提供了跳过单元测试的能力,只需要使用 Surefire 插件的 skip 参数. DOS下,只要简单的给任何目标添加 maven.test.skip 属性就能跳过测试: $ mvn install -Dmaven.test.skip=true ... [IN

jQuery2.0应用开发:SSH框架整合jQuery2.0实战OA办公自动化(VSS、operamasks-UI框架)

我的qq是2059055336,对这个课程有兴趣的可以加我qq联系. 一.本课程是怎么样的一门课程(全面介绍)    1.1.课程的背景 jQuery 2.0 正式版发布,不在支持 IE 6/7/8 浏览器.因此该版本更小.更快.       为了提高现代社会人们的办公效率,满足人们自动化办公的需要,我们开发了这套稳定可靠.操作方便.安全有效的MyOffice系统     1.2.课程内容简介       本课程全面详细的介绍jQuery2.0以及OA办公自动化系统的开发流程.其中重点详细的介绍

如何在不允许联网的环境下使用Maven开发

前言:Maven的运行机理是:Maven核心组件先去本地的.m2目录下的库中去寻找依赖或者插件,如果本地库里没有,如果配置了私服则上私服去下载依赖或者插件,如果私服上没有,则上中央服务等Maven服务器起下载.因此,所有的依赖和插件都源于中央服务器.可是在有些苛刻环境下,是不允许局域网和外网有任何连接,而又想使用Maven来构建项目,那怎么办呢?以下将讲述具体步骤: 1)寻找一台可以联网的电脑,在上边搭建nexus私服: 2)配置setting.xml文件,使得本地开发环境依赖nexus私服:

J2EE整合Maven开发,Tomcat正常启动,访问链接报404错误

Tomcat7.X正常启动,通过浏览器访问URL时,报404错误.原来是部署时,初了问题,因为使用IDE开发,Eclipse帮我们自动部署了,可有时候会出现问题.解决方案如下: 选中项目,右键:Properties -> Deployment Assembly J2EE整合Maven开发,Tomcat正常启动,访问链接报404错误

maven开发mybatis 让*.xml 拷贝到classes目录下

在开发mybatis项目时,很多时候我们要把配置文件放在src/main/java目录下,可是maven在编译项目时,默认不把src/main/java目录下的*.xml拷贝到classes目录下,那么就需要我们配置maven <build>    <resources>    <resource>    <directory>src/main/java</directory>    <includes>    <include