博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring3+hibernate4搭建
阅读量:5901 次
发布时间:2019-06-19

本文共 6940 字,大约阅读时间需要 23 分钟。

hot3.png

本框架以商品购物平台项目为例,用到spring3mvc和hibernate4,主要搭建步骤如下:

1、搭建spring3MVC

2、整合hiebernate4

【搭建spring3MVC】

(1)放入sping3所需的库、commons-logging-1.0.4.jar、jstl.jar
(2)配置web.xml,启动spring和mvc

spring3hibernate4
contextConfigLocation
classpath:spring-config.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
1
spring
/
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
(3)建立spring和mvc的配置文件
spring-config.xml
spring-servlet.xml
(4)创建一个商品控制器GoodsController和商品列表视图goods_list.jsp
GoodsController
/** *  */package com.it.app.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * @author cwd * */@Controller@RequestMapping(value="/goods")public class GoodsController {		@RequestMapping(value="list")	public String list(){		System.out.println("GoodsController.list:Passing through...");		return "goods_list";	}	}
goods_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title heregoods list!
【测试效果】
输入url:http://localhost:8180/spring3hibernate4/goods/list
网页输出内容:goods list!
【整合hibernate4】
(1)放入hibernate4所需的库、mysql连接驱动库 、dbcp库(commons-dbcp.jar和commons-pool.jar)
(2)在spring配置文件中加入数据源DataSource和hibernate4的相关配置
com.it.app
org.hibernate.dialect.MySQLInnoDBDialect
create
true
true
true 1, false 0
16
2
true
true
thread
(3)以商品为例建立相应的实体domain、数据访问dao、服务service,并且修改相应的控制器controller
Goods.java
/** *  */package com.it.app.domain;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;/** * @author cwd * */@Entity@Table(name="goods")public class Goods implements Serializable {	//商品编号id,商品名称name,商品类型type,商品价格price	@Id	@Column(name="id",length=32,nullable=false,unique=true)	@GenericGenerator(name="generator",strategy="uuid.hex")	@GeneratedValue(generator="generator")	private String id;	@Column	private String name;	@Column	private String type;	@Column	private double price;		public String getId() {		return id;	}	public void setId(String id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getType() {		return type;	}	public void setType(String type) {		this.type = type;	}	public double getPrice() {		return price;	}	public void setPrice(double price) {		this.price = price;	}}
GoodsDao.java
package com.it.app.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Repository;import com.it.app.domain.Goods;@Repositorypublic class GoodsDao {	@Autowired	@Qualifier("sessionFactory")	private SessionFactory sessionFactory;	public SessionFactory getSessionFactory() {		return sessionFactory;	}	public void setSessionFactory(SessionFactory sessionFactory) {		this.sessionFactory = sessionFactory;	}		public void save(){		Session session = this.getSessionFactory().getCurrentSession();		Transaction tr = session.beginTransaction();		Goods goods = new Goods();		goods.setName("spring touch");		goods.setType("book");		goods.setPrice(40.5);		session.save(goods);		tr.commit();			}		}
GoodsService.java
/** *  */package com.it.app.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.it.app.dao.GoodsDao;/** * @author cwd * */@Servicepublic class GoodsService {	@Autowired	private GoodsDao goodsDao;	public GoodsDao getGoodsDao() {		return goodsDao;	}	public void setGoodsDao(GoodsDao goodsDao) {		this.goodsDao = goodsDao;	}		public void save(){		this.goodsDao.save();	}}
修改GoodsController.java,操作数据库,修改后如下:
/** *  */package com.it.app.web.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.it.app.service.GoodsService;/** * @author cwd * */@Controller@RequestMapping(value="/goods")public class GoodsController {	@Autowired	private GoodsService goodsService;		@RequestMapping(value="list")	public String list(){		System.out.println("GoodsController.list:Passing through...");		this.goodsService.save();		return "goods_list";	}	public GoodsService getGoodsService() {		return goodsService;	}	public void setGoodsService(GoodsService goodsService) {		this.goodsService = goodsService;	}	}

【Spring集成测试】

(1)加入JUnit4测试库
(2)创建一个测试类用来测试sping+hibernate
创建商品服务测试类GoodsServiceTest.java

/** *  */package com.it.app.service;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * @author cwd * */@RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(locations = {"classpath:spring-config.xml"})  public class GoodsServiceTest {	@Autowired	private GoodsService goodsService;			@Test	public	void testSave(){		goodsService.save();	}}
最后测试类测试和web测试方式都成功,框架搭建成功!

转载于:https://my.oschina.net/tiancai/blog/83677

你可能感兴趣的文章
用实例讲DynamicResource与StaticResource的区别
查看>>
工控随笔_12_西门子_WinCC的VBS脚本_03_变量类型
查看>>
appium 报错
查看>>
phpquery中文手册
查看>>
微信nickname乱码(emoji)及mysql编码格式设置(utf8mb4)解决的过程
查看>>
【转】C++ 笔试面试题目
查看>>
同步和异步的区别
查看>>
[Leetcode] Search in Rotated Sorted Array
查看>>
委托、Lambda表达式、事件系列02,什么时候该用委托
查看>>
在ASP.NET MVC控制器中获取链接中的路由数据
查看>>
使用ASP.NET Atlas SortBehavior实现客户端排序
查看>>
LightOJ 1274 Beating the Dataset(期望)
查看>>
图像滤镜处理算法:灰度、黑白、底片、浮雕
查看>>
多线程一个错误的例子
查看>>
默认网关及route print
查看>>
Servlet如何处理一个请求?
查看>>
SQL Server 关于列的权限控制
查看>>
有哪些 Java 源代码看了后让你收获很多,代码思维和能力有较大的提升?
查看>>
Ansible 下载模块get_url、解压缩模块unarchive(学习笔记十五)
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.21. 窗口函数
查看>>