您的位置:首页 > 编程语言 > Java开发

Struts 2 + Spring 3 + Hibernate 整合笔记

2012-10-27 19:14 489 查看
转载地址:http://blog.psjay.com/posts/struts-2-spring-3-hibernate-integration-notes/

四天时间按看完了《轻量级Java EE企业应用实战 —— Sturts 2 + Spring + Hibernate》,于是在carino的建议下打算写一个简单的论坛程序来练练手。

但是SSH的整合配置却折腾了我不少时间(因为Spring 3的包结构发生了很大的变化),经过一系列的倒腾,终于整合成功了。放出代码。

环境:

Windows 7

Tomcat 6

数据库:MySQL

需要的jar包如下:





文件结构如下:





下面是WEB-INF/web.xml:

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 添加struts 2的支持 -->
<filter>
<filter-name>Struts 2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts 2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 启动spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

然后是WEB-INF/applicationContext.xml:

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
2728
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.0.xsd"> 
<!-- 配置Hibernate  数据源-->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/pbbs"/>
<property name="user" value="root"/>
<property name="password" value="password"/>
</bean>

<!-- 配置Hibernate SeesionFactory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>com/psjay/pbbs/model/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- 配置其他beans-->
<bean id="registerAction" class="com.psjay.pbbs.action.RegisterAction" scope="prototype">
<property name="userManager" ref="userManager"></property>
</bean>
<bean id="userManager" class="com.psjay.pbbs.service.impl.UserManagerImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.psjay.pbbs.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
</beans>

以及src/struts.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<?xml version="1.0" encoding="UTF-8" ?>

<struts>
<constant name="struts.devMode" value="true" />

<package name="user" extends="struts-default">
<action name="register" class="com.psjay.pbbs.action.RegisterAction">
<result>/register.jsp</result>
</action>
<action name="registerAction" class="com.psjay.pbbs.action.RegisterAction" method="register">
<result name="registerSuccess">/registerSuccess.jsp</result>
<result name="registerFail">/registerFail.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>

和com/psjay/pbbs/model/User.hbm.xml :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<?xml version="1.0"?>

<hibernate-mapping package="com.psjay.pbbs.model">

<class name="User" table="t_user">

<id name="id" column="u_id">
<generator class="native"/>
</id>

<property name="username" column="u_username" not-null="true"/>
<property name="password" column="u_password" not-null="true"/>

</class>

</hibernate-mapping>

相关的java类源代码:

com.psjay.pbbs.action.RegisterAction

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
2728
29
30
31

package com.psjay.pbbs.action;

import com.opensymphony.xwork2.ActionSupport;
import com.psjay.pbbs.model.User;
import com.psjay.pbbs.service.UserManager;

public class RegisterAction extends ActionSupport {

private String username;
private String password;
private String confirmPassword;

private String tip;
private UserManager userManager;

/*
* 用户注册
* */
public String register() {
if(!password.equals(confirmPassword)) {
tip = "两次输入的密码不同";
return "input";
}
if(userManager.register(new User(username,password)))
return "registerSuccess";
return "registerFail";
}

//省略了setters and getters....

}

com.psjay.pbbs.dao.UserDao

1
2
3
4
5
6
7
8
9
10
11
12

package com.psjay.pbbs.dao;

import com.psjay.pbbs.model.User;

public interface UserDao {
//根据用户名查找用户
public User findByUserName(String username);
//创建用户
public Integer save(User u);
//取得用户
public User get(User u);
}

com.psjay.pbbs.dao.impl.UserDaoImpl

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
2728
29
30
31
32
33
34
35
36
37
38
39
40

package com.psjay.pbbs.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.psjay.pbbs.dao.UserDao;
import com.psjay.pbbs.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

public boolean delete(String username) {
// TODO Auto-generated method stub
return false;
}

public boolean deleteAll() {
// TODO Auto-generated method stub
return false;
}

public User findByUserName(String username) {
Session s = this.getSession();
List<User> users = (List<User>)s.createQuery("from User user where user.username=:username").setString("username", username).list();
if(users.isEmpty()) {
return null;
}
return users.get(0);
}

public User get(User u) {
return getHibernateTemplate().get(User.class, u.getId());
}

public Integer save(User u) {
return (Integer) getHibernateTemplate().save(u);
}

}

com.psjay.pbbs.model.User

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
2728
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

package com.psjay.pbbs.model;

public class User {
//属性
private int id;
private String username;
private String password;

//无参构造函数
public User() {

}
//构造函数
public User(String username,String password) {

this.username = username;
this.password = password;
}

//重写hashCode和equals方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}

//省略setters and getters..
}

com.psjay.pbbs.service.UserManager

1
2
3
4
5
6
7

package com.psjay.pbbs.service;

import com.psjay.pbbs.model.User;

public interface UserManager {
public boolean register(User u);
}

com.psjay.pbbs.service.impl.UserManagerImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1617
18
19
20
21
22
23
24
25
26

package com.psjay.pbbs.service.impl;

import com.psjay.pbbs.dao.UserDao;
import com.psjay.pbbs.model.User;
import com.psjay.pbbs.service.UserManager;

public class UserManagerImpl implements UserManager {

private UserDao userDao;

public boolean register(User u) {
if (userDao.findByUserName(u.getUsername()) != null) {
return false;
}
userDao.save(u);
return true;
}
//getters and setters
public UserDao getUserDao() {
return userDao;
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

这样,就实现了一个简单的注册小程序(当然还很不健壮)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: