springmvc+hibernate基础学习一

首先我们了解一下springmvc,程序员都是很懒的,所以框架的出现拯救了一大批程序员,曾近经繁琐低效的jsp+servlet到现在简洁高效的struts,springmvc,jsf等框架的转变已经成了趋势,我这里使用的就是现在流行的springmvc,它将视图层,控制层,模块层结合在一起,它与struts2的区别在于它是利用注解对方法的拦截访问,封装了更多的东西,配置文件也极大减少。hibernate也没什么可说,它是对数据库操作的封装,负责数据的持久化工作。
这里主要是对配置文件和注解的学习。
这里就用最基本的例子‘账户管理’来学习,这里列出部分代码。
基础项目结构图:
这里写图片描述
所用到的库(jar包):
http://download.csdn.net/detail/yabaj/9757453
配置文件
web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>   
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="MyWebApp" version="2.5"> 
<display-name>SHTest</display-name>
<welcome-file-list>
    <welcome-file>user/login.jsp</welcome-file>
</welcome-file-list>

<!-- 加载所有的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/spring-*.xml</param-value>
</context-param>

<!-- 配置Spring监听 -->
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<listener>  
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
</listener>  

<!-- 配置字符集 -->
<filter>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
        <param-name>encoding</param-name>  
        <param-value>UTF-8</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

<filter>
   <filter-name>hiddenHttpMethodFilter</filter-name>
   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>hiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>  
  <filter-name>openSession</filter-name>  
  <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
</filter>  
<filter-mapping>  
  <filter-name>openSession</filter-name>  
  <url-pattern>/*</url-pattern>  
</filter-mapping>

<!-- <servlet>  
    <servlet-name>test1</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>test1</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping> -->

<!-- 配置SpringMVC -->
<servlet>
    <servlet-name>SHTest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SHTest</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

spring-hibernate.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:aop="http://www.springframework.org/schema/aop"  
  xmlns:cache="http://www.springframework.org/schema/cache"  
  xmlns:context="http://www.springframework.org/schema/context"  
  xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  xmlns:jee="http://www.springframework.org/schema/jee"  
  xmlns:jms="http://www.springframework.org/schema/jms"  
  xmlns:lang="http://www.springframework.org/schema/lang"  
  xmlns:mvc="http://www.springframework.org/schema/mvc"  
  xmlns:oxm="http://www.springframework.org/schema/oxm"  
  xmlns:task="http://www.springframework.org/schema/task"  
  xmlns:tx="http://www.springframework.org/schema/tx"  
  xmlns:util="http://www.springframework.org/schema/util"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd  
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd  
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd  
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd  
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--扫描映射  -->  
<context:component-scan base-package="com"/>

<!-- 配置数据库 -->
<bean id="propertyConfigurer"   
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">
            <list>  
                <value>classpath:/config/db.properties</value>  
            </list>  
        </property>  
    </bean>

<!-- 配置hibernate相关数据库的操作 -->  
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
   <property name="username" value="${username}"/>
   <property name="password" value="${password}"/>
   <property name="driverClassName" value="${driverClassName}"/> 
   <property name="url" value="${url}"/>
</bean>  

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
   <property name="dataSource" ref="dataSource"/>
   <property name="mappingDirectoryLocations">  
        <list>  
            <value>  
            <!-- 读取和实体相关的xml -->  
                classpath:/com/bean/  
            </value>  
        </list>  
     </property>  
   <!-- 找到实体包(pojo) -->
  <!--  <property name="namingStrategy">
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean>
   </property> -->
   <!-- <property name="packagesToScan" value="com.bean"></property> -->
   <property name="hibernateProperties">  
   <props>  
   <!-- 配置不同数据库的方言 -->  
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
       <!-- 其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none".  
其它几个参数的意思validate(载hibernate时,验证创建数据库表结构),create(每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因),create-drop(加载hibernate时创建,退出是删除表结构),update(加载hibernate自动更新数据库结构)-->  
            <prop key="hibernate.hbm2ddl.auto">update</prop>   
            <!-- 是否显示sql语句 -->  
            <prop key="hibernate.show_sql">true</prop>  
            <prop key="hiberante.format_sql">true</prop>
            <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
        </props>  
    </property>
    <!-- 自动扫描制定位置下的实体进行映射  -->   
    <property name="packagesToScan" value="com.bean"/> 
</bean>  
 <!-- 定义事务管理器(声明式的事务) -->    
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    <property name="sessionFactory" ref="sessionFactory"></property>  
</bean>

<!--aop管理事务-->
<!-- <aop:config>
    <aop:pointcut expression="execution(* com.service.*(..))" id="daoCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoCut"/>
</aop:config>

<tx:advice transaction-manager="txManager" id="txAdvice">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
</tx:advice> -->

<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">  
    <!-- 配置事务管理器 -->    
    <property name="transactionManager" ref="transactionManager"></property>  
    <!-- 配置事务管理器 -->    
    <property name="transactionAttributes">  
     <props>  

  <!-- 下面就开始配置各个模块所必须的部分,在各自的applicationContext-XXX-beans.xml配置的对于事务管理的详细信息。  

首先就是配置事务的传播特性,如下:  

 配置事务传播特性   -->

          <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>  
          <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>  
          <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>  
          <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>  
          <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>  
          <prop key="get*">PROPAGATION_NEVER</prop>  
      </props>  
      </property>  
</bean>

<!-- 应该是开启事物 -->  
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    
      http://www.springframework.org/schema/context    
      http://www.springframework.org/schema/context/spring-context-3.2.xsd    
      http://www.springframework.org/schema/mvc    
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    
      http://www.springframework.org/schema/tx    
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd      
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- HandlerMapping -->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> -->

<!-- HandlerAdapter -->
<!-- 定义适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> -->

<!-- 注解扫描包 -->
<!-- <context:component-scan base-package="config"
        use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation"
      expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan> -->

<context:component-scan base-package="com">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan>

<!-- 开启注解 -->  
<mvc:annotation-driven />

<!-- 开启AOP自动代理功能 -->  
<aop:aspectj-autoproxy proxy-target-class="true"/>

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

<!-- ViewResolver -->  
<!-- 定义视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>


<!-- 处理器 -->  
<!-- 定义Handler对象 -->
<!-- <bean name="/hello" class="com.controller.HelloWorldController"/>  -->
</beans>

后续见下一篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值