首页 > 编程语言 > 详细

03SpringMVC,Spring,Hibernate整合

时间:2015-01-04 21:26:03      阅读:453      评论:0      收藏:0      [点我收藏+]

  1. 项目结构

技术分享

技术分享

技术分享

2  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

技术分享

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

03SpringMVC,Spring,Hibernate整合

原文:http://blog.csdn.net/tototuzuoquan/article/details/42397485

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!