创建maven项目
把maven项目变为动态网站,步骤如下:
项目结构图如下:
开始搭建spring+springmvc+Hibernate项目
环境版本就不多说了,直接贴出pom.xml文件
4.0.0
com.kaspar
com.kaspar
war
0.0.1-SNAPSHOT
UTF-8
2.5.10
4.3.8.RELEASE
5.1.7.Final
SSH Maven Webapp
http://maven.apache.org
junit
junit
4.12
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.apache.struts
struts2-core
${struts.version}
org.apache.struts
struts2-spring-plugin
${struts.version}
org.springframework
spring-orm
${spring.version}
org.hibernate
hibernate-core
${hibernate.version}
mysql
mysql-connector-java
5.1.42
com.mchange
c3p0
0.9.5
org.aspectj
aspectjweaver
1.8.10
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
1.8
org.apache.maven.plugins
maven-surefire-plugin
2.19.1
true
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/${project.artifactId}
View Code
web.xml
/p>
"http://java.sun.com/dtd/web-app_2_3.dtd" >
version="3.0">
index.jsp
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
View Code
/SSH_Template_maven/WebContent/WEB-INF/view/index.jsp
新增商品
商品名称 | ||
---|---|---|
商品价格 | ||
View Code
applicationContext.xml
true
true
update
org.hibernate.dialect.MySQLDialect
View Code
struts.xml
/p>
"http://struts.apache.org/dtds/struts-2.5.dtd">
/WEB-INF/view/index.jsp
WEB-INF/view/index.jsp
WEB-INF/view/index.jsp
View Code
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
View Code
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
View Code
messageResource.properties
invalid.fieldvalue.price = \u5546\u54c1\u4ef7\u683c\u8f93\u5165\u683c\u5f0f\u6709\u8bef
View Code
demo项目结构图:
Product.java
packagecom.kaspar.product.model;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importorg.hibernate.annotations.GenericGenerator;
@Entitypublic classProduct {
@Id
@GeneratedValue(generator= "pid")
@GenericGenerator(name= "pid", strategy = "native")private int pid;//商品ID
@Column(length = 100)private String pname;//商品名称
private double price;//商品价格
public intgetPid() {returnpid;
}public void setPid(intpid) {this.pid =pid;
}publicString getPname() {returnpname;
}public voidsetPname(String pname) {this.pname =pname;
}public doublegetPrice() {returnprice;
}public void setPrice(doubleprice) {this.price =price;
}publicProduct() {
}public Product(String pname, doubleprice) {this.pname =pname;this.price =price;
}
}
View Code
ProductAction.java
packagecom.kaspar.product.action;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Controller;importcom.kaspar.product.model.Product;importcom.kaspar.product.service.ProductService;importcom.opensymphony.xwork2.ActionSupport;/*** 商品操作-控制层
**/@Controller
@Scope("prototype")public class ProductAction extendsActionSupport {private static final long serialVersionUID = 1L;
@AutowiredprivateProductService productService;privateString pname;private doubleprice;/*** 保存商品操作
*
*@return
*/
publicString saveProduct() {
Product product= newProduct(pname, price);
productService.saveProduct(product);this.addActionMessage("保存成功...");returnSUCCESS;
}publicString getPname() {returnpname;
}public voidsetPname(String pname) {this.pname =pname;
}public doublegetPrice() {returnprice;
}public void setPrice(doubleprice) {this.price =price;
}
@Overridepublic voidvalidate() {if(pname == null || "".equals(pname.trim())) {this.addFieldError("pname", "商品名称不能为空");
}
}
}
View Code
ProductService.java
packagecom.kaspar.product.service;importcom.kaspar.product.model.Product;/*** 商品操作-服务层接口
**/
public interfaceProductService {voidsaveProduct(Product product);
}
View Code
ProductServiceImpl.java
packagecom.kaspar.product.service.impl;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importcom.kaspar.product.dao.ProductDao;importcom.kaspar.product.model.Product;importcom.kaspar.product.service.ProductService;
@Transactional
@Service("ProductServiceImpl")public class ProductServiceImpl implementsProductService {
@AutowiredprivateProductDao productDao;
@Overridepublic voidsaveProduct(Product product) {
productDao.saveProduct(product);
}
}
View Code
ProductDao.java
packagecom.kaspar.product.dao;importorg.springframework.stereotype.Repository;importcom.kaspar.product.model.Product;/*** 商品操作-持久层接口
**/@Repositorypublic interfaceProductDao {voidsaveProduct(Product product);
}
View Code
ProductDaoImpl.java
packagecom.kaspar.product.dao.impl;importorg.hibernate.SessionFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.orm.hibernate5.HibernateTemplate;importorg.springframework.stereotype.Repository;importcom.kaspar.product.dao.ProductDao;importcom.kaspar.product.model.Product;/*** 商品信息-服务层实现
**/@Repository("ProductDaoImpl")public class ProductDaoImpl implementsProductDao {privateHibernateTemplate template;
@AutowiredpublicProductDaoImpl(SessionFactory sessionFactory) {
template= newHibernateTemplate(sessionFactory);
}
@Overridepublic voidsaveProduct(Product product) {
template.save(product);
}
}
View Code
运行效果图:
代码是撸的(哪里撸如果还记得我一定贴链),配置是到处拼的。 日常踩坑结束。
最后后提示一点,配置没有问题,如果自动建表没有成功可以给表换一个名字试试。