noaman_wgs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1 WEB环境下配置Spring

    因为是在WEB环境中应用Spring,所以要先配置web.xml:

    (1)WebContent-WEB-INF-lib包中,加入Spring包下的required的所有jar包;

  (2)WebContent-WEB-INF下新建web.xml,配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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="WebApp_ID" version="2.5">
 3     
 4     <!-- 配置spring配置文件.xml的名称和位置路径 -->
 5     <context-param>
 6         <param-name>contextConfigLocation</param-name>
 7         <param-value>classpath:applicationContext*.xml</param-value>
 8     </context-param>
 9     
10     <!-- Bootstraps the root web application context before servlet initialization -->
11     <listener>
12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13     </listener>
14     
15 
16 </web-app>
View Code

    (3)src下新建Source-folder包:conf,新建applicationContext.xml(暂时不需要写)

 

2 加入Hibernate

  (1)建立持久化类,生成对应的 .hbm.xml 文件, 生成对应的数据表;

  (2)Spring整合Hibernate

  (3) 步骤:

    ①加入Hibernate的jar包,加入MySQL和c3p0驱动;

    ②conf报下配置hibernate.cfg.xml文件

  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         
 8         <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
 9         <property name="hibernate.show_sql">true</property>
10         <property name="hibernate.format_sql">true</property>
11         <property name="hibernate.hbm2ddl.auto">update</property>
12     </session-factory>
13 </hibernate-configuration>
View Code

    ③src下新建实体类:com.atguigu.ssh.entities:Department.java和Employee.java  

Department.java:
  
 1 package com.atguigu.ssh.entities;
 2 
 3 public class Department {
 4     
 5     private Integer id;
 6     private String departmentName="";
 7     public Integer getId() {
 8         return id;
 9     }
10     public void setId(Integer id) {
11         this.id = id;
12     }
13     public String getDepartmentName() {
14         return departmentName;
15     }
16     public void setDepartmentName(String departmentName) {
17         this.departmentName = departmentName;
18     }
19     
20     
21 }
View Code
Employee.java:
  
 1 package com.atguigu.ssh.entities;
 2 
 3 import java.util.Date;
 4 
 5 public class Employee {
 6     private Integer id;
 7     
 8     private String lastName="";
 9     private String email;
10     
11     private Date birth;
12     private Date createTime;
13     
14     private Department department;
15 
16     public Integer getId() {
17         return id;
18     }
19 
20     public void setId(Integer id) {
21         this.id = id;
22     }
23 
24     public String getLastName() {
25         return lastName;
26     }
27 
28     public void setLastName(String lastName) {
29         this.lastName = lastName;
30     }
31 
32     public String getEmail() {
33         return email;
34     }
35 
36     public void setEmail(String email) {
37         this.email = email;
38     }
39 
40     public Date getBirth() {
41         return birth;
42     }
43 
44     public void setBirth(Date birth) {
45         this.birth = birth;
46     }
47 
48     public Date getCreateTime() {
49         return createTime;
50     }
51 
52     public void setCreateTime(Date createTime) {
53         this.createTime = createTime;
54     }
55 
56     public Department getDepartment() {
57         return department;
58     }
59 
60     public void setDepartment(Department department) {
61         this.department = department;
62     }
63     
64 }
View Code

     ④生成对应的.hbm.xml持久化类映射文件:Department.hbm.xml,Employee.hbm.xml

     ⑤conf包下新建db.properties文件: 

  
1 jdbc.user=root
2 jdbc.password=920614
3 jdbc.driverClass=com.mysql.jdbc.Driver
4 jdbc.jdbcUrl=jdbc:mysql:///spring_ssh
5 
6 jdbc.initPoolSize=5
7 jdbc.maxPoolSize=10
8 #...
View Code

    ⑥再次配置Spring配置文件:applicationContext.xml

  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xmlns:p="http://www.springframework.org/schema/p"
 8     xmlns:util="http://www.springframework.org/schema/util"
 9     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
11         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
12         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
14     
15     <!-- 导入资源文件 -->
16     <context:property-placeholder location="classpath:db.properties"/>
17     
18     <!-- 导入C3P0数据源 -->
19     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
20         <property name="user" value="${jdbc.user}"></property>
21         <property name="password" value="${jdbc.password}"></property>
22         <property name="driverClass" value="${jdbc.driverClass}"></property>
23         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
24         
25         <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
26         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
27     </bean>
28     
29     <!--配置Hibernate 的sessionFactory的实例  -->
30     <bean id="sessionFactory" 
31           class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
32         <property name="dataSource" ref="dataSource"></property>
33         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
34         <property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property>
35     </bean>    
36     
37     <!--配置Spring声明式事务  -->
38     <!--1 配置hibernate事务管理器  -->
39     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
40         <property name="sessionFactory" ref="sessionFactory"></property>
41     </bean>
42     
43     <!--2 配置事务属性  -->    
44     <tx:advice id="txAdvice" transaction-manager="transactionManager">
45         <tx:attributes>
46             <tx:method name="get*" read-only="true"/>
47             <tx:method name="*"/>
48         </tx:attributes>
49     </tx:advice>
50      
51      <!--3 配置事务切入点  -->
52     <aop:config>
53         <aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))" id="txPointCut"/>
54         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
55     </aop:config>
56 
57 
58 </beans>
View Code

 

SSH_框架整合1整体框架图1:

  

  

posted on 2016-09-18 17:37  noaman_wgs  阅读(222)  评论(0编辑  收藏  举报