zoukankan      html  css  js  c++  java
  • SSH整合 第三篇 Spring的加入

    1、思路和想法。

    目前理解到的,觉得是的,可能的,应该这样的………………

    Spring的两大核心是IoC和AOP

    Ioc:帮助实例化对象,加载到容器中,在注入到需要用到的地方。这样就可以减少在不同的方法/类中新建对象了。同时,实现类改变了(基于接口),在xml中改了就好。比较适合单例编程。那么我们将Hibernate常常用到的SessionFactory交给Spring。

    AOP:与数据库打交道,事务管理是必须的,什么ACID之类的。那么AOP就比较适合了。

    2、整合

    继续在之前的工程加上spring的jar。

    1)、Spring-3.2.0

    2)、使用数据源。

    选用dbcp

    commons-dbcp-xxx.jar

    commons-pool-xxx.jar

     

    3)、SessionFactory

    在spring的配置文件中,配置SessionFactory(交给Spring)管理。Spring配置文件这里命名为applicationContext.xml

     1 <!-- sessionFactory -->
     2 <bean id="sessionFactory"
     3     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     4     <!-- 数据源 -->
     5     <property name="dataSource" ref="dataSource" />
     6     <!-- 注解的实体类 ,扫描包 -->
     7     <property name="packagesToScan">
     8         <list>
     9             <value>com.xzw.ssh.pojo</value>
    10         </list>
    11     </property>
    12 
    13     <!-- sessionFactory的一些其他设置。 -->
    14     <property name="hibernateProperties">
    15         <props>
    16             <!-- 通过getCurrentSession创建的session会绑定到当前线程 -->
    17             <prop key="current_session_context_class">thread</prop>
    18             <!--方言 -->
    19             <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop> 
    20             <!--输出sql -->
    21             <prop key="hibernate.show_sql">true</prop>      
    22             <!-- sql格式化输出-->
    23             <prop key="hibernate.format_sql">true</prop>    
    24         </props>
    25     </property>
    26 </bean>
    applicationContext

    4)、mysql.properties

    连接到数据库的基本属性。本测试放在classpath下的db文件

    jdbc.driver=com.mysql.jdbc.Driver

    jdbc.url=jdbc:mysql://localhost:3306/nwssh

    jdbc.username=root

    jdbc.password=root

    5)、datasource的xml配置。

    在applicationContext.xml加上。

     1 <!-- 加载配置文件 db/mysql.properties -->
     2 <context:property-placeholder location="classpath:db/mysql.properties" />
     3 
     4 <!-- 使用dbcp数据源 -->
     5 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
     6     destroy-method="close">
     7     <property name="driverClassName" value="${jdbc.driver}" />
     8     <property name="url" value="${jdbc.url}" />
     9     <property name="username" value="${jdbc.username}" />
    10     <property name="password" value="${jdbc.password}" />
    11     <property name="maxActive" value="15" />
    12     <property name="maxIdle" value="3" />
    13 </bean>
    加上部分

    6)、测试SessionFactory

    加上mysql的jbdc包,配置好database,就可以先测试一下SessionFactory是否能用了。

    到目前的配置文件是

     1 <beans xmlns="http://www.springframework.org/schema/beans"
     2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
     3     xmlns:context="http://www.springframework.org/schema/context"
     4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     7         http://www.springframework.org/schema/mvc 
     8         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     9         http://www.springframework.org/schema/context 
    10         http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    11         http://www.springframework.org/schema/aop 
    12         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    13         http://www.springframework.org/schema/tx 
    14         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    15     <!-- 加载配置文件 db/mysql.properties -->
    16     <context:property-placeholder location="classpath:db/mysql.properties" />
    17 
    18     <!-- 使用dbcp数据源 -->
    19     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    20         destroy-method="close">
    21         <property name="driverClassName" value="${jdbc.driver}" />
    22         <property name="url" value="${jdbc.url}" />
    23         <property name="username" value="${jdbc.username}" />
    24         <property name="password" value="${jdbc.password}" />
    25         <property name="maxActive" value="15" />
    26         <property name="maxIdle" value="3" />
    27     </bean>
    28 
    29     <!-- sessionFactory -->
    30     <bean id="sessionFactory"
    31         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    32         <!-- 数据源 -->
    33         <property name="dataSource" ref="dataSource" />
    34         <!-- 注解的实体类 ,扫描包 -->
    35         <property name="packagesToScan">
    36             <list>
    37                 <value>com.xzw.ssh.pojo</value>
    38             </list>
    39         </property>
    40 
    41         <!-- sessionFactory的一些其他设置。 -->
    42         <property name="hibernateProperties">
    43             <props>
    44                 <!-- 通过getCurrentSession创建的session会绑定到当前线程 -->
    45                 <prop key="current_session_context_class">thread</prop>
    46                 <!--方言 -->
    47                 <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
    48                 <!--输出sql -->
    49                 <prop key="hibernate.show_sql">true</prop>
    50                 <!-- sql格式化输出 -->
    51                 <prop key="hibernate.format_sql">true</prop>
    52             </props>
    53         </property>
    54     </bean>
    55 
    56 
    57 </beans>
    applicationContext

    java测试代码

     1 public class H_A_S_Test {
     2     
     3     //得到spring容器
     4     private ApplicationContext applicationContext =
     5             
     6             new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
     7     
     8     @Test
     9     public void test1() throws Exception {
    10         //得到SessionFactory
    11         SessionFactory sessionFactory =  (SessionFactory) applicationContext.getBean("sessionFactory");
    12         
    13         Session session =sessionFactory.openSession();
    14         
    15         User user = (User) session.get(User.class, "u1");
    16         
    17         System.out.println(user.getUsername());
    18     }
    19 }
    java测试代码

    如果上面的测试能得到对应的User就成功了。

  • 相关阅读:
    php header
    How to Share a Theme With All Users in a Windows 7 Computer?
    240多个jQuery插件
    Delphi读写UTF8、Unicode格式文本文件
    虚拟网关解决ARP批处理版
    从煎蛋网上抓取妹子图
    在【批处理】中过滤特殊字符
    反序列化(临时)
    python发邮件实例
    反序列化(临时)
  • 原文地址:https://www.cnblogs.com/jway1101/p/5796478.html
Copyright © 2011-2022 走看看