03SpringMVC,Spring,Hibernate整合



  1. 项目结构


web.xml的配置内容如下:


<?xml
version="1.0"
encoding="UTF-8"?>

<web-app
version="2.5"
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_2_5.xsd"
>

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<!-- struts用/*,
springmvc不能/*,
语法 *.xxx -->

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:beans.xml</param-value>

</context-param>

</web-app>

  1. springmvc.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:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

       
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

       
http://www.springframework.org/schema/mvc

       
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

       
http://www.springframework.org/schema/context

       
http://www.springframework.org/schema/context/spring-context-3.0.xsd

       
http://www.springframework.org/schema/aop

       
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

       
http://www.springframework.org/schema/tx

       
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "
>

<context:component-scan
base-package="com.rl.controller"
/>

<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property
name="prefix"
value="/WEB-INF/jsp/"></property>

<property
name="suffix"
value=".jsp"></property>

</bean>

</beans>

  1. log4j.properties的内容如下:

log4j.rootLogger=DEBUG,
Console

#Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%d
[%t]
%-5p
[%c]
-
%m%n

log4j.logger.java.sql.ResultSet=INFO

log4j.logger.org.apache=INFO

log4j.logger.java.sql.Connection=DEBUG

log4j.logger.java.sql.Statement=DEBUG

log4j.logger.java.sql.PreparedStatement=DEBUG

  1. beans.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:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

       
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

       
http://www.springframework.org/schema/mvc

       
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

       
http://www.springframework.org/schema/context

       
http://www.springframework.org/schema/context/spring-context-3.0.xsd

       
http://www.springframework.org/schema/aop

       
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

       
http://www.springframework.org/schema/tx

       
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>

<context:component-scan
base-package="com.rl"/>

<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property
name="driverClassName"
value="com.mysql.jdbc.Driver"></property>

<property
name="url"
value="jdbc:mysql://localhost:3306/springmvc"></property>

<property
name="username"
value="root"></property>

<property
name="password"
value="123456"></property>

</bean>

<bean
id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property
name="dataSource"
ref="dataSource"></property>

<property
name="hibernateProperties">

<props>

<prop
key="hibernate.Dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<prop
key="hibernate.show_sql">true</prop>

<prop
key="hibernate.hbm2ddl">update</prop>

</props>

</property>

<property
name="mappingDirectoryLocations"
value="classpath:com/rl/mapping/"></property>

</bean>

<bean
id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property
name="sessionFactory"
ref="sessionFactory"></property>

</bean>

<tx:advice
id="txAdvice"
transaction-manager="txManager">

<tx:attributes>

<tx:method
name="save*"
propagation="REQUIRED"/>

<tx:method
name="get*"
read-only="true"/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:advisor
advice-ref="txAdvice"
pointcut="execution(* com.rl.service..*.*(..))"/>

</aop:config>

</beans>

  1. Person0420的代码如下:

package com.rl.model;

import java.util.Date;

@SuppressWarnings("serial")

public
class Person0420
implements java.io.Serializable {

private Integer
personId;

private String
name;

private String
gender;

private String
address;

private Date
birthday;

/**

*

*/

public Person0420() {

}

/**

* @param personId

* @param name

* @param gender

* @param address

* @param birthday

*/

public Person0420(Integer personId, String name,
String gender,

String address, Date birthday) {

this.personId
= personId;

this.name
= name;

this.gender
= gender;

this.address
= address;

this.birthday
= birthday;

}

/**

* @return the
personId

*/

public Integer getPersonId() {

return
personId;

}

/**

* @param personId
the personId to set

*/

public
void setPersonId(Integer personId) {

this.personId
= personId;

}

/**

* @return the
name

*/

public String getName() {

return
name;

}

/**

* @param name
the name to set

*/

public
void setName(String name) {

this.name
= name;

}

/**

* @return the
gender

*/

public String getGender() {

return
gender;

}

/**

* @param gender
the gender to set

*/

public
void setGender(String gender) {

this.gender
= gender;

}

/**

* @return the
address

*/

public String getAddress() {

return
address;

}

/**

* @param address
the address to set

*/

public
void setAddress(String address) {

this.address
= address;

}

/**

* @return the
birthday

*/

public Date getBirthday() {

return
birthday;

}

/**

* @param birthday
the birthday to set

*/

public
void setBirthday(Date birthday) {

this.birthday
= birthday;

}

}

  1. Person0420.hbm.xml的内容如下:

<?xml
version="1.0"
encoding="utf-8"?>

<!DOCTYPE
hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<class
name="com.rl.model.Person0420"
table="person_0420"
catalog="springmvc">

<id
name="personId"
type="java.lang.Integer">

<column
name="PERSON_ID"
/>

<generator
class="identity"
/>

</id>

<property
name="name"
type="java.lang.String">

<column
name="NAME"
length="10"
/>

</property>

<property
name="gender"
type="java.lang.String">

<column
name="GENDER"
length="1"
/>

</property>

<property
name="address"
type="java.lang.String">

<column
name="ADDRESS"
length="50"
/>

</property>

<property
name="birthday"
type="java.util.Date">

<column
name="BIRTHDAY"
length="0"
/>

</property>

</class>

</hibernate-mapping>

  1. 创建数据库和表所需的SQL语句:

DROP DATABASE springmvc;

CREATE DATABASE springmvc DEFAULT CHARSET utf8;

USE springmvc;

CREATE TABLE person_0420(

PERSON_ID INT AUTO_INCREMENT PRIMARY KEY,

NAME VARCHAR(10) NOT NULL,

GENDER VARCHAR(1) NOT NULL,

ADDRESS VARCHAR(50) NOT NULL,

birthday DATE

);

  1. PersonDao的代码如下:

package com.rl.dao;

import com.rl.model.Person0420;

public
interface
PersonDao {

public
void save(Person0420 person);

}

  1. PersonDaoImpl 的内容如下:

package com.rl.dao.impl;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.springframework.stereotype.Repository;

import com.rl.dao.PersonDao;

import com.rl.model.Person0420;

@Repository

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {

@Autowired

public void setMySessionFactory(SessionFactory sessionFactory){

super.setSessionFactory(sessionFactory);

}

public void save(Person0420 person) {

this.getHibernateTemplate().save(person);

}

}

  1. PersonService的内容如下:

package com.rl.service;

import com.rl.model.Person0420;

public
interface PersonService {

public
void save(Person0420 person);

}

  1. PersonServiceImpl的内容如下:

package com.rl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.rl.dao.PersonDao;

import com.rl.model.Person0420;

import com.rl.service.PersonService;

@Service

public class PersonServiceImpl implements PersonService {

@Autowired

PersonDao personDao;

public void save(Person0420 person) {

personDao.save(person);

}

}

  1. PersonController的内容如下:

package com.rl.controller;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.propertyeditors.CustomDateEditor;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.ServletRequestDataBinder;

import org.springframework.web.bind.annotation.InitBinder;

import org.springframework.web.bind.annotation.RequestMapping;

import com.rl.model.Person0420;

import com.rl.service.PersonService;

@Controller

@RequestMapping("/person")

public class PersonController {

@Autowired

PersonService personService;

@RequestMapping("/toForm.do")

public String toForm() {

return "form";

}

@RequestMapping("/save.do")

public String save(Person0420 person) {

personService.save(person);

return "success";

}

@InitBinder

public void initBinder(ServletRequestDataBinder binder) {

binder.registerCustomEditor(Date.class, new CustomDateEditor(

new SimpleDateFormat("yyyy-MM-dd"), true));

}

}

14 form.jsp的内容如下:


<%@
page
language="java"
import="java.util.*"
pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base
href="<%=basePath%>">

<title>My
JSP ‘index.jsp‘ starting page</title>

<meta
http-equiv="pragma"
content="no-cache">

<meta
http-equiv="cache-control"
content="no-cache">

<meta
http-equiv="expires"
content="0">

<meta
http-equiv="keywords"
content="keyword1,keyword2,keyword3">

<meta
http-equiv="description"
content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<form
action="person/save.do"
method="post"
>

name:<input
name="name"
type="text"><br>

gender:<input
name="gender"
type="text"><br>

address:<input
name="address"
type="text"><br>

birthday:<input
name="birthday"
type="text"><br>

<input
value="submit"
type="submit">

</form>

</body>

</html>

  1. success.jsp的内容如下:

<%@
page
language="java"
import="java.util.*"
pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base
href="<%=basePath%>">

<title>My
JSP ‘index.jsp‘ starting page</title>

<meta
http-equiv="pragma"
content="no-cache">

<meta
http-equiv="cache-control"
content="no-cache">

<meta
http-equiv="expires"
content="0">

<meta
http-equiv="keywords"
content="keyword1,keyword2,keyword3">

<meta
http-equiv="description"
content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

success<br>

</body>

</html>

浏览器中的访问地址是:http://localhost:8080/ssh/person/toForm.do

时间: 2024-12-25 18:12:38

03SpringMVC,Spring,Hibernate整合的相关文章

spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread

spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106) at org.hibernate.internal.SessionFactoryImpl.getC

Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置

Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservletmysql 在公司一直没有什么机会直接折腾SSH“原生态”的SSH当今比较流行的轻量级的框架,用着公司的框架也是郁闷异常,今天没事整整原来用过的一个项目的配置,发现就算是自己曾经用过的东西,如果较长时间不返过去重新学习,许多你半熟不熟的知识就是异常陌生.下面贴上我的一些配置,暂且权当备份吧. web

Spring+Hibernate整合

因为整合spring和hibernate所以,需要用到spring里面复写Hibernate的类以有DI和IOC特性 db.sql hibernate_basic数据库 表 person 字段 pid pname psex Person.java 1 package cn.edu.spring_hibernate; 2 3 public class Person { 4 private Long pid; 5 private String pname; 6 private String psex

spring mvc+spring + hibernate 整合(三)

前面我们已整合了spring + hibernate,并建立了个用户的增加的实例,通过了单元测试,能正常增加数据.今天我们就来集成spring mvc以便建立web界面来输入数据并提交,后台再保存入库.关于spring mvc的一些基础理论及使用方法,网上有很多的资料,我这里就不多说了,下面我们进入实战.     因为我们项目会用到很多的图片.js代码.css样式文件等.我们在webapp目录下建立个static目录,统一对这些文件进行管理 一:web.xml配置 它提供了我们应用程序的配置设定

Struts+Spring+Hibernate整合入门详解

标签: strutshibernatespringbeanactionimport 2007-08-12 16:05 36280人阅读 评论(13) 收藏 举报 分类: STRUTS&SPRING&HIBERNATE(12) Java 5.0 Struts 2.0.9 spring 2.0.6 hibernate 3.2.4 作者:  Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念       Struts:作为基于 MVC 模式的 Web 应用最经典框架,两个项目

spring mvc+spring + hibernate 整合(二)

在上篇文章中,我建立了工程并配置了spring + hibernate.今天我们检验下上篇文章的成果,如何检查呢?那就是进行单元测试.本篇文章就让大家和我一起来就前面的建的工程进行单元测试.     本项目使用Junit进行单元测试,需要引用单元测试的包,在的工程建立中已有了如何引入单元测试的依赖,这里就不多说了.要进行单元单元测试,我们就按下面的步骤进行 一:建立实体 本例是刚弄开始学习,所以表不弄得太复杂,我们就以用户登录为例:建立一个用户对象,拥有用户名和密码两个属性,密码开始也使用名文的

Strut2 spring hibernate 整合

一.创建web项目工程 wzz 点击finish 2.添加spring Jar包   AOP,Core,Persistence Core ,web jar 点击next 点击Finish 3.配置Database Driver 我使用的是JTDS jar包,jtds在配置url地址时与使用sql Seriver的url地址有点不太一样,然后直接点击Finish即可 4.添加hibernate 配置 点击next 选择使用Spring的applicationContext.xml,点击next 选

java框架篇---spring hibernate整合

在会使用hibernate 和spring框架后 两个框架的整合就变的相当容易了, 为什么要整合Hibernate?1.使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2.使用Spring管理Session对象  HibernateTemplate3.使用Spring的功能实现声明式的事务管理 第一步:搭建hibernate环境(包括引入hibernate的jar,包配置数据源,建立类和表的映射),为什么这么做.我觉得hiberna

spring mvc+spring + hibernate 整合(一)

一:创建maven工程,引入需要的依赖包,创建项目的工程包 二:整合spring和hibernate 1.建立sql server数据库访问的jdbc配置文件. 这个配置文件主要是用来将sql server的连接参数放在外面配置,当参数有变动时只需要修改配置文件,不需要修改web-inf下的spring.xml文件 不建立也可以,如果不建立则spring.xml中的数据源配置就如建立数据源配置中的代码1. jdbcUrl = jdbc:sqlserver://localhost:1433;Dat