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就成功了。

时间: 2024-08-18 11:47:28

SSH整合 第三篇 Spring的加入的相关文章

SSH整合 第四篇 Spring的IoC和AOP

这篇主要是在整合Hibernate后,测试IoC和AOP的应用. 1.工程目录(SRC) 2.IoC 1).一个Service测试类 1 /* 2 * 加入spring容器 3 */ 4 private ApplicationContext applicationContext = 5 6 new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); 7 public static void

SSH整合 第五篇 struts2的到来

struts2的好处,web层的显示,同时Action类相当于MVC模式的C.整合进来的话,是通过与Spring整合,减少重复代码,利用IoC和AOP. 1.struts-2.5.2.jar 以上是struts-2.5.2基本的包 ,另外着色的两个之前有加入了,不再加入. 2.整合包 struts2-spring-plugin-2.5.2(struts2有提供) 3.Action要用注解的话 加入struts2-convention-plugin-2.5.2 在Action上加上注解时 有NoC

ssh整合之五struts和spring整合

1.首先,我们需要先分析一下,我们的spring容器在web环境中,只需要一份就可以了 另外,就是我们的spring容器,要在我们tomcat启动的时候就创建好了(包括其中的spring的对象),怎么保证我们的spring容器能创建呢? 我们可以配合监听器来创建我们的spring容器,然后我们怎么实现我们的监听器呢? 当ServletContext创建成功,就说明tomcat正常启动了,我们使用监听器监听我们的ServletContext,如果创建成功,加载配置文件,创建spring容器 2.我

ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点

Web层用Structs2的action Service层用Spring的IoC和aop以及JdbcTemplate或者Transaction事务(创建对象及维护对象间的关系) Dao层用Hibernate的crude操作 看上去是Structs2和Spring(把Structs2的action交给Spring整合,action处理表单,通常都是多实例用Spring <bean id="" class="" scope="prototype"

ssh整合之三hibernate和spring整合

1.拷贝我们的spring事务控制所需的jar包 2.在spring容器中配置我们的hibernateTemplate以及事务管理器 <?xml version="1.0" encoding="UTF-8"?> <!-- spring的配置文件:导入约束 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt

从.Net到Java学习第三篇——spring boot+mybatis+mysql

环境:mysql5.7 新建mysql数据库demo,然后执行如下sql脚本进行数据表创建和数据初始化: -- ---------------------------- -- Table structure for `tb_area` -- ---------------------------- DROP TABLE IF EXISTS `tb_area`; CREATE TABLE `tb_area` ( `area_id` int(2) NOT NULL AUTO_INCREMENT CO

SSH整合基础

1.Spring整合Hibernate 整合步骤 导包 配置appliactionContext.xml 创建实体类和映射关系文件 创建DAO接口及实现类 声明DAO组件,注入SessionFactory 2.Spring整合Struts2 导包 配置web.xml 配置applicationContext.xml,开启注解扫描 创建并声明Action 配置Action 创建JSP 3.整合的基本原理 Struts原本使用StrutsObjectFactory来读取struts.xml,根据ac

从.Net到Java学习第四篇——spring boot+redis

从.Net到Java学习第一篇——开篇 从.Net到Java学习第二篇——IDEA and start spring boot 从.Net到Java学习第三篇——spring boot+mybatis+mysql 接上一篇,本篇使用到的框架redis.FastJSON. 环境准备 安装redis,下图是我本机的redis绿色版,你可以网上自行下载安装,如果不知道如何怎么操作,可以移步到我的另一篇文章:ASP.NET Redis 开发 以管理员身份打开CMD窗口: C:\Users\zouqj>e

Spring(八)SSH整合简述

一.Spring与Struts2的整合 1.1.整合步骤 1.2.配置web.xml 1.3.配置spring配置文件applicationContext.xml 1.4.配置struts配置文件 1.5.Action继承ActionSupport类 二.spring与hibernate整合 2.1.步骤 2.2.注入sessionFactory <--数据源--> <bean id="dataSource" class="org.springframewo