我是如何整合SSH的(一)

概述

在之前一段时间内,我断断续续在课余时间学习了struts2、hibernate3、spring和其他javaEE的关键技术。由于之间一直呆在实验室帮老师做一个其他的项目,并没有很认真地学习javaEE,掌握的知识可能并不是很牢靠,所以通过整合SSH这个过程来提高我对JavaEE的认知。

Spring整合Hibernate3之注册案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//定义User
private Integer id;
private String username;
private String pwd;
private Integer age;
private Date birthday;

//User.hbx.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ssh.bean.User" table="user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="pwd"></property>
<property name="age"></property>
<property name="birthday" type="date"></property>
</class>
</hibernate-mapping>

//UserDaoImpl ,继承自HibernateDaoSupport,可以使用hibernate的模版
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public void addUser(User u) {
this.getHibernateTemplate().save(u);
}
}

//UserService层,这一层和以前没有任何改变
public class UserServiceImpl implements UserService {

private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void register(User u) {
this.userDao.addUser(u);
}
}

//用注册一个用户来测试Spring是否已经整合了hibernate
public class SSHTest {
@Test
public void testAdd(){
User u = new User();
u.setUsername("jessyon");
u.setPwd("1234");
u.setAge(18);
u.setBirthday(new Date());

String xmlPath="applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.register(u);
}

//和单独使用hibernate一样,需要在src下创建一个hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- 1.基本4项 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">jessyon6233834</property>

<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- sql语句 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>

<!-- 6 取消bean校验 -->
<property name="javax.persistence.validation.mode">none</property>

<!-- 添加映射文件 -->
<mapping resource="com/ssh/bean/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

//Spring的配置都保存在applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 1 获得session工厂
* 加载hibernate.cfg.xml 获得工厂
configLocation : 确定配置文件位置
-->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!-- 2 模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- dao UserDao类里用到了hibernateTemplate,所以需要将之注入到dao里面-->
<bean id="userDao" class="com.ssh.daoimpl.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>

<bean id="userService" class="com.ssh.serviceimpl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>

<!-- 事务管理 ,需要将管理事务的session注入-->
<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="register"/>
</tx:attributes>
</tx:advice>

<!-- aop 执行service中的方法前,添加事务管理。如果不加这句话,就相当于执行了rollback,没有commit-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssh.service..*.*(..))"/>
</aop:config>
</beans>

//也可以不使用hibernate.cfg.xml,那么applicationContext需要微调

//使用c3p0作为数据源(连接池)
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="jessyon6233834"></property>
</bean>

//session工厂的内容变多,不仅需要注入数据源,还要将hibernate.cfg.xml里的内容注入
<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.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/ssh/bean/*.hbm.xml"></property>
</bean>