java ssh maven_Java-手动搭建SSH(maven版)

创建maven项目

7cd0d85946dad3f80a6753c4fc3e876c.png

58682d03a5d5f3304b0254d48c559136.png

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

09568411e0a22265664dd7f241fdbd61.png

9951b7c9ce6f6726e2c82e61e0fa2292.png

70f5e002bfbc7a3b6c297089074aa0a1.png

3211f7204cb9a6179864920c1e435196.png

项目结构图如下:

5627a3b6dde5e8997f819e5667683a15.png

开始搭建spring+springmvc+Hibernate项目

环境版本就不多说了,直接贴出pom.xml文件

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

新增商品界面

新增商品

商品名称
商品价格

View Code

applicationContext.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

true

true

update

org.hibernate.dialect.MySQLDialect

View Code

struts.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

invalid.fieldvalue.price = \u5546\u54c1\u4ef7\u683c\u8f93\u5165\u683c\u5f0f\u6709\u8bef

View Code

demo项目结构图:

3730619b8e7f938b25878a574947e6d8.png

Product.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.kaspar.product.service;importcom.kaspar.product.model.Product;/*** 商品操作-服务层接口

**/

public interfaceProductService {voidsaveProduct(Product product);

}

View Code

ProductServiceImpl.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.kaspar.product.dao;importorg.springframework.stereotype.Repository;importcom.kaspar.product.model.Product;/*** 商品操作-持久层接口

**/@Repositorypublic interfaceProductDao {voidsaveProduct(Product product);

}

View Code

ProductDaoImpl.java

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

运行效果图:

a4f93f6a9952b573d90f789d3ed23589.png

021bceef65474cda533a4ac7b485cf09.png

代码是撸的(哪里撸如果还记得我一定贴链),配置是到处拼的。 日常踩坑结束。

最后后提示一点,配置没有问题,如果自动建表没有成功可以给表换一个名字试试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值