Java-手动搭建SSH(maven版)
创建maven项目


把maven项目变为动态网站,步骤如下:




项目结构图如下:

开始搭建spring+springmvc+Hibernate项目
环境版本就不多说了,直接贴出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.kaspar</groupId>
<artifactId>com.kaspar</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> <properties>
<!-- 统一源码的编码方式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一各个框架版本 -->
<struts.version>2.5.10</struts.version>
<spring.version>4.3.8.RELEASE</spring.version>
<hibernate.version>5.1.7.Final</hibernate.version>
</properties> <name>SSH Maven Webapp</name>
<url>http://maven.apache.org</url> <dependencies>
<!-- Junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring 核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Struts2 核心依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- Struts2和Spring整合依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency> <!-- Spring整合ORM框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Hibernate 核心依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- MySQL 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<!-- C3P0 依赖 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5</version>
</dependency> <!--
添加事务控制,本处使用基于AspectJ的xml声明式事务,控制更加灵活。因此需要添加aspectjweaver.jar包
-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency> </dependencies> <build>
<plugins>
<!-- 统一源代码编译输出的JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 打包时跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- 集成Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/${project.artifactId}</path>
</configuration>
</plugin>
</plugins>
</build> </project>
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 xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 配置Struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.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> <!-- 指定Spring配置文件所在路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> </web-app>
/SSH_Template_maven/WebContent/WEB-INF/view/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增商品界面</title>
</head>
<body>
<h1>新增商品</h1>
<s:actionmessage/>
<s:form action="product_save" method="post" namespace="/" theme="simple">
<table width="600px">
<tr>
<th>商品名称</th>
<td><s:textfield name="pname"/></td>
<td><font color="red"><s:fielderror fieldName="pname"/></font></td>
</tr>
<tr>
<th>商品价格</th>
<td><s:textfield name="price"/></td>
<td><font color="red"><s:fielderror fieldName="price"/></font></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="保存"/>
</th>
<th> </th>
</tr>
</table>
</s:form>
</body>
</html>
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启包扫描,并注册注解 -->
<context:component-scan base-package="com.kaspar.product.*"/> <!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接相关信息 -->
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- 配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描并加载注解过的实体类 -->
<property name="packagesToScan" value="com.kaspar.product.model"/>
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop><!-- 是否展示SQL -->
<prop key="hibernate.format_sql">true</prop><!-- 输出底层sql语句格式 -->
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 是否自动创建表结构 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property> </bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置需要进行事务管理的方法,和事务传播行为 -->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="sel*" propagation="SUPPORTS"/>
<tx:method name="find*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice> <!-- 配置切面 -->
<aop:config>
<!-- 配置切入点
* com.kaspar.service.*+.*(..)
*:表示方法的作用域,*表示所有
com.kaspar.service.*:表示org.ssh.service下的任何包
com.kaspar.service.*+:表示org.ssh.service下的任何包及其子包
*(..):*表示任何方法,(..)表示方法的任何参数
-->
<aop:pointcut expression="execution(* com.kaspar.product.*service.*.*(..))" id="pointcut"/>
<!-- 适配切入点和事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config> </beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts> <!-- 默认访问页面 -->
<package name="default" extends="struts-default" namespace="/">
<default-action-ref name="default"/>
<action name="default">
<result>/WEB-INF/view/index.jsp</result>
</action>
</package> <!-- 商品相关请求转发 -->
<!-- Struts2在2.5版本后添加strict-method-invocation(严格方法访问),默认为true,不能使用动态方法调用功能,故需设为false -->
<package name="product" extends="struts-default" namespace="/" strict-method-invocation="false">
<!-- 保存商品 -->
<action name="product_*" class="productAction" method="{1}Product">
<result>WEB-INF/view/index.jsp</result>
<result name="input">WEB-INF/view/index.jsp</result>
</action>
</package> <!-- 引入资源文件 -->
<constant name="struts.custom.i18n.resources" value="messageResource"></constant> </struts>
jdbc.properties
jdbc.url=jdbc:mysql://127.0.0.1/ssh?useSSL=false&characterEncoding=UTF-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
log4j.properties
log4j.rootCategory=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %t %c{2}:%L - %m%n
messageResource.properties
invalid.fieldvalue.price = \u5546\u54c1\u4ef7\u683c\u8f93\u5165\u683c\u5f0f\u6709\u8bef
demo项目结构图:

Product.java
package com.kaspar.product.model; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import org.hibernate.annotations.GenericGenerator; @Entity
public class Product { @Id
@GeneratedValue(generator = "pid")
@GenericGenerator(name = "pid", strategy = "native")
private int pid;// 商品ID
@Column(length = 100)
private String pname;// 商品名称
private double price;// 商品价格 public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
} public Product() { } public Product(String pname, double price) { this.pname = pname;
this.price = price; } }
ProductAction.java
package com.kaspar.product.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.kaspar.product.model.Product;
import com.kaspar.product.service.ProductService;
import com.opensymphony.xwork2.ActionSupport; /**
* 商品操作-控制层
*
*/
@Controller
@Scope("prototype")
public class ProductAction extends ActionSupport { private static final long serialVersionUID = 1L; @Autowired
private ProductService productService; private String pname;
private double price; /**
* 保存商品操作
*
* @return
*/
public String saveProduct() { Product product = new Product(pname, price);
productService.saveProduct(product); this.addActionMessage("保存成功...");
return SUCCESS; } public String getPname() {
return pname;
} public void setPname(String pname) {
this.pname = pname;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} @Override
public void validate() { if(pname == null || "".equals(pname.trim())) {
this.addFieldError("pname", "商品名称不能为空");
} } }
ProductService.java
package com.kaspar.product.service; import com.kaspar.product.model.Product; /**
* 商品操作-服务层接口
*
*/ public interface ProductService { void saveProduct(Product product); }
ProductServiceImpl.java
package com.kaspar.product.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.kaspar.product.dao.ProductDao;
import com.kaspar.product.model.Product;
import com.kaspar.product.service.ProductService; @Transactional
@Service("ProductServiceImpl")
public class ProductServiceImpl implements ProductService { @Autowired
private ProductDao productDao; @Override
public void saveProduct(Product product) { productDao.saveProduct(product); } }
ProductDao.java
package com.kaspar.product.dao; import org.springframework.stereotype.Repository; import com.kaspar.product.model.Product; /**
* 商品操作-持久层接口
*
*/
@Repository
public interface ProductDao { void saveProduct(Product product); }
ProductDaoImpl.java
package com.kaspar.product.dao.impl; import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository; import com.kaspar.product.dao.ProductDao;
import com.kaspar.product.model.Product; /**
* 商品信息-服务层实现
*
*/
@Repository("ProductDaoImpl")
public class ProductDaoImpl implements ProductDao { private HibernateTemplate template; @Autowired
public ProductDaoImpl(SessionFactory sessionFactory) { template = new HibernateTemplate(sessionFactory); } @Override
public void saveProduct(Product product) { template.save(product); } }
运行效果图:


代码是撸的(哪里撸如果还记得我一定贴链),配置是到处拼的。 日常踩坑结束。
最后后提示一点,配置没有问题,如果自动建表没有成功可以给表换一个名字试试。
Java-手动搭建SSH(maven版)的更多相关文章
- 自己记录java环境搭建之-maven
		
简述: 现需要在Eclipse中配置Maven插件,同时安装maven应用,配置Maven环境变量,建立Maven管理的工程,并用Maven导入Gson包, 编写简易Json输出程序 步骤: 1. 首 ...
 - maven3常用命令、java项目搭建、web项目搭建详细图解
		
http://blog.csdn.net/edward0830ly/article/details/8748986 ------------------------------maven3常用命令-- ...
 - 转 maven3常用命令、java项目搭建、web项目搭建详细图解
		
转自地址:http://blog.csdn.net/edward0830ly/article/details/8748986 ------------------------------maven3常 ...
 - maven3常用命令、java项目搭建、web项目搭建详细图解(转)
		
转自:http://blog.csdn.net/edward0830ly/article/details/8748986 maven3常用命令.java项目搭建.web项目搭建详细图解 2013-0 ...
 - Maven环境下搭建SSH框架之Spring整合Hibernate
		
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Spring:4.3.8.RELEASE Hibernate:5.1.7.Final MySQL:5.7.17 注意:其他版本在某些特性 ...
 - Mac下maven工程的创建,并搭建SSH环境
		
最近项目有用到maven,就特地学了一下.maven的一句话攻略就是,项目托管.帮你解决各种项目琐事:清理,导包....等等. 首先先到apach官网去下载一个maven的包,http://maven ...
 - maven搭建ssh项目及遇到的问题
		
如果采用手动添加jar包的方式搭建项目,会使效率降低,传到github上时,下载时需要下载很多jar包,用maven管理项目可以提高效率 我在搭建maven项目时遇到了 1) java.lang.No ...
 - Maven环境下搭建SSH框架
		
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Maven:3.3.9 Struts2:2.5.10 Spring:4.3.8.RELEASE Hibernate:5.1.7.Fina ...
 - intellij 创建java web项目(maven管理的SSH)
		
intellij 创建java web项目(maven管理的SSH) 环境intellij IDEA14.MAVEN.Spring.Struts2.Hibernate.Java Web.工程搭建. 1 ...
 
随机推荐
- 如何优化SpringBoot的项目的启动速度
			
日常开发SpringBoot项目启动类都用@SpringBootApplication,实际上它是下面三个注解的组合: @EnableAutoConfiguration: enable Spring ...
 - Ubuntu虚拟机下忘记密码的解决方法
			
由于好久没有用虚拟机里的ubuntu系统,导致忘记了密码.试了好多遍,密码都是错的,内心感到崩溃呀.选择只有两个:一个是重装系统,另一个是找回密码.自己不想重装系统只能找回密码了,在网上百度了好多,都 ...
 - linux 排查cpu负载过高异常
			
步骤一.找到最耗CPU的进程 工具:top 方法: 执行top -c ,显示进程运行信息列表 键入P (大写p),进程按照CPU使用率排序 图示: 如上图,最耗CPU的进程PID为10765 步骤二: ...
 - sql查询条件参数为空
			
查询某些值为空的数据 select * from usertable where name is null or page is null
 - No Suitable Driver Found For Jdbc
			
今天出现编码出现了No suitable driver found for jdbc,又是找遍了网上的资料,基本上都说是三个问题: 一是:连接URL格式出现了问题(Connection conn ...
 - abp 中log4net 集成Kafka
			
1.安装包 Install-Package log4net.Kafka.Core 2.修改log4net.config 配置文件 <?xml version="1.0" en ...
 - eventlet 模块搭建 WEB 服务器
			
eventlet这个强悍的东东,看到我同事的一些整理.故贴出来,大家一起分享~ motivation 114.113.199.11服务器上nova服务中基于python eventlet实现的定时任务 ...
 - 工具系列 | Docker基本概念
			
1.什么是docker? Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化. ...
 - com.alibaba.druid.pool.DruidPooledConnection cannot be cast to oracle.jdbc.OracleConnection 异常解决办法
			
java.lang.ClassCastException: com.alibaba.druid.pool.DruidPooledConnection cannot be cast to oracle. ...
 - Running MYSQL 5.7 By Bash On Ubuntu On Windows:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
			
root@PC-RENGUOQIANG:/usr/sbin# /etc/init.d/mysql start * Starting MySQL database server mysqld [ OK ...