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的更多相关文章
- maven整合ssh框架笔记
具体工程会上传文件sshpro <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:x ...
- Maven开发环境的搭建,含jetty调试[简单明了]
之前的一片老文,搬过来. 1. Maven的开发环境需要哪些部件a) Maven.去http://maven.apache.org/ 下载.将其bin目录加入进系统PATH.测试方法:用命令mvn – ...
- Maven开发基础总结(Maven自启动,Maven打war包,Maven热部署)
学习内容: 1.不依赖外部Tomcat,自己启动方式部署 2.Maven打war包,远程部署到centOS 3.Maven热部署(不关闭Tomcat部署应用) 做maven开发前提: 1.编码UT ...
- Eclipse配置Maven开发环境
前言: 现在Eclipse版本越来越高.高版本的Eclipse甚至已经集成了Maven像是SpringSource的哪个版本.用习惯了Eclipse.在开发中还是不想更换掉自己的IDE.如此一来就又了 ...
- windows下Idea结合maven开发spark和本地调试
本人的开发环境: 1.虚拟机centos 6.5 2.jdk 1.8 3.spark2.2.0 4.scala 2.11.8 5.maven 3.5.2 在开发和搭环境时必须注意版本兼容的问题 ...
- SpringMVC+Maven开发项目源码详细介绍
代码地址如下:http://www.demodashi.com/demo/11638.html Spring MVC概述 Spring MVC框架是一个开源的Java平台,为开发强大的基于Java的W ...
- 如何在不允许联网的环境下使用Maven开发
前言:Maven的运行机理是:Maven核心组件先去本地的.m2目录下的库中去寻找依赖或者插件,如果本地库里没有,如果配置了私服则上私服去下载依赖或者插件,如果私服上没有,则上中央服务等Maven服务 ...
- Eclipse搭建maven开发环境
上一篇学习了maven开发环境的搭建,而且手动编写了一个mavenproject,可是这样子效率非常低下.今天带大家学习在eclipse下搭建maven开发环境. 经常使用的maven命令 mvn c ...
- MAVEN day04 SSH之分模块开发
一.创建父工程 1.选择>>"Maven Project"创建Maven工程.并且选择Packaging为 POM. 创建父工程主要是让子工程区继承父工程,减少冗余,多 ...
随机推荐
- 修改CENTOS7的网卡ens33修改为eth0
1.先编辑网卡的配置文件将里面的NAME DEVICE项修改为eth0 vim /etc/sysconfig/network-scripts/ifcfg-ens33 2.[root@linux-nod ...
- js 实现文字滚动功能,可更改配置参数 带完整版解析代码。
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS写文字滚动效果 需求分析: 需要 ...
- Django入门3:视图views
1.获取用户请求数据 1.1 request.GET 获取request.method='GET'的数据 request.GET.get('name',None) 1.2 request.POST 获 ...
- 腾讯视频怎么转成mp4模式 软件 工具 方法 最新【已解决】
1.搜索: 小白兔视频格式在线转换 2.转换好后视频已经是MP4格式了. 转载于:https://blog.51cto.com/14204019/2396896
- 【React踩坑记六】create-react-app创建的react项目通过iP地址访问(实现局域网内访问)
同项目组的小伙伴想用自己的电脑访问我电脑上开发阶段的create-react-app创建的react项目. 试过了了各种内网穿透工具ngrok以及localtunnel等. 奈何打开效率实在太过于龟速 ...
- Shiro踩坑记(二):使用RequiresXXX的注解后,访问对应请求返回404
问题描述: 我在项目中的某个Controller上添加了@RequirePermissions注解,希望在执行该请求前,可以先进行权限验证.但是当我请求该Controller时,返回的确是404错误. ...
- 在IIS服务器上本地部署 ArcGIS API for js 4.15
作为一名刚入门的小白,还没开始一个helloworld就在软件安装,环境部署时遇到了一大堆问题,简直太让人头秃了,脑壳疼.话不多说,这篇主要想分享一下自己部署ArcGIS API for js 4.1 ...
- VUE简单整理
在用 Vue.js 构建大型应用时推荐使用 NPM 安装: # 最新稳定版 $ cnpm install vue 命令行工具 Vue.js 提供一个官方命令行工具,可用于快速搭建大型单页应用. # 全 ...
- Navicat,SQL注入,pymysql模块
# 关键字exists(了解) 只返回布尔值 True False 返回True的时候外层查询语句执行 返回False的时候外层查询语句不再执行 select * from emp where exi ...
- POJ2376Cleaning Shifts(区间覆盖贪心)
应该还是蛮简单的一题,但是因为模拟太差,一直没调出来....... \(显而易见的应该按照左区间从小到大排序,相等按照右区间大到小排序\). \(那么第一个区间的l一定要是1,而且必拿(否则没有区间能 ...