准备工作:struts2.3.34+spring4.2.2+hibernate3.3.2
导入需要的开发包:
struts开发包---注意:javassist-3.18.1-GA.jar包与hibernate中的重复(只保留高版本即可)
hibernate开发包
此外还需要在hibernate解压包中找到lib/optional/c3p0文件夹,然后将c3p0-0.9.2.1.jar和mchange-commons-java-0.2.3.4.jar拷贝到工程lib目录下
spring开发包
除了javadoc.jar和sources.jar包名结尾的包,其他包全部拷贝到工程项目lib中
最后别忘了将mysql驱动包拷贝到lib中
2、web.xml
Spring提供了ContextLoaderListener,该监听器实现了ServletContextListener接口,他在Web应用程序启动时被触发。当他创建时会自动查找WEB-INF/下的applicationContext.xml,所以当只有一个配置文件且文件名为applicationContext.xml时,则只需要在web.xml文件中配置ContextLoaderListener监听器即可.当有多个配置文件需要载入,则应该使用元素指定配置文件的文件名,ContextLoaderListener加载时,会查找名为contextConfigLocation的初始化参数。当Web应用程序启动时先读取web.xml文件,然后创建spring容器,之后根据配置文件内容,装配Bean实例。
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 sklm_2
4
5
12 home.jsp
13
14
15
16
17 org.springframework.web.context.ContextLoaderListener
18
19
20
21 contextConfigLocation
22 classpath:applicationContext.xml
23
24
25
26
27 struts2
28 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
29
30
31 struts2
32 /*
33
34
35
web.xml
3、实体和映射文件
(1)User.java
1 packagecn.xyp.web.entity;2
3 importjava.sql.Date;4
5 public classUser {6
7 privateLong user_id;8 privateString user_name;9 privateString user_password;10 private intuser_age;11 privateString user_sex;12 privateString user_address;13 privateDate user_create_time;14 publicUser(){}15
16 public User(String user_name, String user_password, intuser_age, String user_sex, String user_address,17 Date user_create_time) {18 this.user_name =user_name;19 this.user_password =user_password;20 this.user_age =user_age;21 this.user_sex =user_sex;22 this.user_address =user_address;23 this.user_create_time =user_create_time;24 }25
26 publicLong getUser_id() {27 returnuser_id;28 }29 public voidsetUser_id(Long user_id) {30 this.user_id =user_id;31 }32 publicString getUser_name() {33 returnuser_name;34 }35 public voidsetUser_name(String user_name) {36 this.user_name =user_name;37 }38 publicString getUser_password() {39 returnuser_password;40 }41 public voidsetUser_password(String user_password) {42 this.user_password =user_password;43 }44 public intgetUser_age() {45 returnuser_age;46 }47 public void setUser_age(intuser_age) {48 this.user_age =user_age;49 }50 publicString getUser_sex() {51 returnuser_sex;52 }53 public voidsetUser_sex(String user_sex) {54 this.user_sex =user_sex;55 }56 publicString getUser_address() {57 returnuser_address;58 }59 public voidsetUser_address(String user_address) {60 this.user_address =user_address;61 }62 publicDate getUser_create_time() {63 returnuser_create_time;64 }65 public voidsetUser_create_time(Date user_create_time) {66 this.user_create_time =user_create_time;67 }68
69 @Override70 publicString toString(){71 return "User [user_id="+user_id+", user_name="+user_name+", user_password="+user_password+", user_age="+user_age+", "
72 + "user_sex="+user_sex+", user_address="+user_address+", user_create_time="+user_create_time+"]";73 }74
75 }
user.java
(2)、User.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
User.hbm.xml
4、DAO
(1)UserDAO.java接口类
1 packagecn.xyp.web.dao;2
3 importjava.util.List;4
5 importcn.xyp.web.entity.User;6
7 public interfaceUserDAO {8
9 /**
10 * 向数据库中添加用户11 *@paramuser12 */
13 public voidadd(User user);14
15 /**
16 * 刷新user在数据库中的信息17 *@paramuser18 */
19 public voidupdate(User user);20
21 /**
22 * 根据被给定的id号删除对应的user23 *@paramid24 */
25 public void delete(longid);26
27 /**
28 * 根据被给定的name和password查找user29 *@paramname30 *@parampassword31 *@return
32 */
33 publicUser findByName(String name, String password);34
35 /**
36 * 根据被给定的id号查找user37 *@paramid38 *@return
39 */
40 public User findById(longid);41
42 /**
43 * 从数据库中查找所有信息44 *@return
45 */
46 public ListfindAll();47 }
UserDAO
(2)UserDAOImpl.java 接口实现类
1 packagecn.xyp.web.dao;2
3 importjava.util.List;4
5 importorg.hibernate.HibernateException;6 importorg.hibernate.Query;7 importorg.hibernate.Session;8 importorg.hibernate.SessionFactory;9 importorg.hibernate.Transaction;10
11 importcn.xyp.web.entity.User;12
13 public class UserDAOImpl implementsUserDAO {14
15 privateSessionFactory sessionFactory;16
17 //构造方法注入SessionFactory对象
18 publicUserDAOImpl(SessionFactory sessionFactory){19 this.sessionFactory =sessionFactory;20 }21
22 //使用SessionFactory对象返回Session对象
23 publicSession currentSession(){24 returnsessionFactory.openSession();25 }26
27 @Override28 public voidadd(User user) {29 Session session = null;30 try{31 session =currentSession();32 Transaction tx =session.beginTransaction();33 session.save(user);34 tx.commit();35 } catch(HibernateException e) {36 e.printStackTrace();37 }finally{38 session.close();39 }40
41 }42
43 @Override44 public voidupdate(User user) {45 Session session = null;46 try{47 session =currentSession();48 Transaction tx =session.beginTransaction();49 session.save(user);50 tx.commit();51 } catch(HibernateException e) {52 e.printStackTrace();53 }finally{54 session.close();55 }56
57 }58
59 @Override60 public void delete(longid) {61 Session session = null;62 try{63 session =currentSession();64 Transaction tx =session.beginTransaction();65 User user = (User)session.get(User.class, id);66 session.delete(user);67 tx.commit();68 } catch(HibernateException e) {69 e.printStackTrace();70 }finally{71 session.close();72 }73
74 }75
76 @Override77 publicUser findByName(String name, String password) {78 Session session = null;79 User user = null;80 try{81 session =currentSession();82 Transaction tx =session.beginTransaction();83 String hsql = "from User m where m.user_name = :uname and m.user_password= :upassword";84 Query query =session.createQuery(hsql);85 query.setParameter("uname", name);86 query.setParameter("upassword", password);87 user =(User) query.uniqueResult();88 tx.commit();89 }catch(HibernateException e){90 e.printStackTrace();91 }finally{92 session.close();93 }94
95 returnuser;96 }97
98 @Override99 public User findById(longid) {100 Session session = null;101 User user = null;102 try{103 session =currentSession();104 Transaction tx =session.beginTransaction();105 String hsql = "from User u where u.user_id= :id";106 Query query =session.createQuery(hsql);107 user = (User) query.setParameter("id", id);108 tx.commit();109 } catch(HibernateException e) {110 e.printStackTrace();111 }finally{112 session.close();113 }114 returnuser;115 }116
117 @Override118 public ListfindAll() {119 Session session = null;120 List list = null;121 try{122 session =currentSession();123 Transaction tx =session.beginTransaction();124 String hsql = "from User";125 Query query =session.createQuery(hsql);126 list =query.list();127 tx.commit();128 } catch(HibernateException e) {129 e.printStackTrace();130 }finally{131 session.close();132 }133 returnlist;134 }135
136 }
UserDAOImpl
5、业务逻辑层
(1)UserService.java接口类
1 packagecn.xyp.web.service;2
3 importjava.util.List;4
5 importcn.xyp.web.entity.User;6
7 public interfaceUserService {8
9 public voidadd(User user);10 public voidupdate(User user);11 public void delete(longid);12 publicUser findByName(String name, String password);13 public User findById(longid);14 public ListfindAll();15
16 }
UserService.java
(2)UserServiceImpl.java接口实现类
1 packagecn.xyp.web.service;2 /**
3 * 设值注入DAO层组件4 */
5 importjava.util.List;6
7 importcn.xyp.web.dao.UserDAO;8 importcn.xyp.web.entity.User;9
10 public class UserServiceImpl implementsUserService {11
12 privateUserDAO userDao;13
14 public voidsetUserDao(UserDAO userDao){15 this.userDao =userDao;16 }17
18 @Override19 public voidadd(User user) {20
21 if(userDao.findById(user.getUser_id())==null){22 userDao.add(user);23 }24 }25
26 @Override27 public voidupdate(User user) {28 if(userDao.findById(user.getUser_id())==null){29 userDao.update(user);30 }31
32 }33
34 @Override35 public void delete(longid) {36 if(userDao.findById(id)==null){37 userDao.delete(id);38 }39
40 }41
42 @Override43 publicUser findByName(String name, String password) {44 User user =userDao.findByName(name, password);45 returnuser;46 }47
48 @Override49 public User findById(longid) {50 User user =userDao.findById(id);51 returnuser;52 }53
54 @Override55 public ListfindAll() {56 List list =userDao.findAll();57 returnlist;58 }59
60 }
UserServiceImpl.java
6、Action
(1)UserRegisterAction.java类(用户注册类)
1 packagecn.xyp.web.action;2 /**
3 *设置注入业务逻辑组件4 */
5 importcom.opensymphony.xwork2.ActionSupport;6
7 importcn.xyp.web.entity.User;8 importcn.xyp.web.service.UserService;9
10 public class UserRegisterAction extendsActionSupport {11
12 /**
13 *14 */
15 private static final long serialVersionUID = -4501870315633237256L;16 privateUser user;17 privateUserService userService;18 publicUser getUser(){19 returnuser;20 }21 public voidsetUser(User user){22 this.user =user;23 }24
25 //注入业务逻辑组件
26 public voidsetUserService(UserService userService){27 this.userService =userService;28 }29
30 publicString execute(){31 userService.add(user);32 returnSUCCESS;33 }34 }
UserRegisterAction.java
(2)UserLoginAction.java类(用户登录类)
1 packagecn.xyp.web.action;2 /**
3 * 设置用户登录注入4 */
5 importcom.opensymphony.xwork2.ActionSupport;6
7 importcn.xyp.web.entity.User;8 importcn.xyp.web.service.UserService;9
10 public class UserLoginAction extendsActionSupport {11
12 /**
13 *14 */
15 private static final long serialVersionUID = 7448295814371243220L;16
17 privateUser user;18 privateUserService userService;19 publicUser getUser() {20 returnuser;21 }22 public voidsetUser(User user) {23 this.user =user;24 }25 //注入业务逻辑组件
26 public voidsetUserService(UserService userService) {27 this.userService =userService;28 }29
30 publicString execute(){31 String b = null;32 try{33 User user_ =userService.findByName(user.getUser_name(), user.getUser_password());34 if(user_ != null){35 b =SUCCESS;36 }37 } catch(Exception e) {38 b =ERROR;39 }40 returnb;41 }42
43 }
UserLoginAction.java
(3)UserDeleteAction.java类(删除用户类)
1 packagecn.xyp.web.action;2
3 importcom.opensymphony.xwork2.ActionSupport;4
5 importcn.xyp.web.service.UserService;6
7 public class UserDeleteAction extendsActionSupport {8
9 /**
10 * 删除用户11 */
12 private static final long serialVersionUID = -2132782345900294714L;13 privateUserService userService;14 private longid;15
16 public voidsetUserService(UserService userService) {17 this.userService =userService;18 }19 public longgetId() {20 returnid;21 }22 public void setId(longid) {23 this.id =id;24 }25
26 publicString execute(){27 userService.delete(getId());28 returnSUCCESS;29 }30
31 }
UserDeleteAction.java
(4)UserQueryAction.java(查询所有用户)
1 packagecn.xyp.web.action;2 /**
3 * 查找数据库中的所有信息4 */
5 importjava.util.List;6 importjavax.servlet.http.HttpServletRequest;7 importorg.apache.struts2.ServletActionContext;8 importcom.opensymphony.xwork2.ActionSupport;9 importcn.xyp.web.entity.User;10 importcn.xyp.web.service.UserService;11
12 public class UserQueryAction extendsActionSupport {13
14 /**
15 *16 */
17 private static final long serialVersionUID = 1483788569708897545L;18 privateUserService userService;19
20 public voidsetUserService(UserService userService) {21 this.userService =userService;22 }23
24 publicString execute(){25 List list =userService.findAll();26 HttpServletRequest request =ServletActionContext.getRequest();27 request.setAttribute("userList", list);28 returnSUCCESS;29 }30
31 }
UserQueryAction.java
(5)UserUpdateAction.java(更新用户)
1 packagecn.xyp.web.action;2
3 importcom.opensymphony.xwork2.ActionSupport;4
5 importcn.xyp.web.entity.User;6 importcn.xyp.web.service.UserService;7
8 public class UserUpdateAction extendsActionSupport {9
10 /**
11 * 更新用户信息12 */
13 private static final long serialVersionUID = 6174197960048716176L;14
15 privateUserService userService;16 privateUser user;17 private longid;18 publicUserService getUserService() {19 returnuserService;20 }21 public voidsetUserService(UserService userService) {22 this.userService =userService;23 }24 publicUser getUser() {25 returnuser;26 }27 public voidsetUser(User user) {28 this.user =user;29 }30 public longgetId() {31 returnid;32 }33 public void setId(longid) {34 this.id =id;35 }36
37 publicString showUser(){38 User user =userService.findById(id);39 setUser(user);40 returnSUCCESS;41 }42
43 publicString execute(){44 userService.update(user);45 returnSUCCESS;46 }47
48 }
UserUpdateAtion.java
7、struts.xml
spring托管将Struts2中的Action的实例化工作交由Spring容器统一管理,同时使Struts2中的Action实例能够访问Spring提供的业务逻辑资源,spring容器依赖注入的优势也可以体现,Struts2提供的spring插件struts2-spring-plugin-2.3.16.3.jar配合使得Struts2的action由Spring来负责进行实例化.
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
4 "http://struts.apache.org/dtds/struts-2.5.dtd">
5
6
7
8
9
10
11
12
13
14 /userQuery
15
16
17
18 /welcome.jsp
19 /register.jsp
20
21
22
23 /displayAll.jsp
24
25
26
27 /userQuery
28
29
30
31 /update.jsp
32
33
34
35 /userQuery
36
37
38
39
struts.xml
8、applicationContext.xml
spring 框架提供了多种数据源类,可以使用spring提供的DriverManagerDataSource类还可以使用第三方数据源,如C3P0的ComboPooledDataSource数据源类,注意相应的两个jar包c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar.
Hibernate4已经完全实现自己的事物管理,所以spring4不提供HibernateDaoSupport和HibernateTemplete的支持。可以在应用程序的spring上下文中,像配置其他bean那样来配置HibernateSession工厂。如果要使用XML文件定义对象与数据库之间的映射,则需要在spring中配置LocalSessionFactoryBean。hibernateProperties属性配置了Hibernate如何进行操作的细节。"hibernate.current_session_context_class"是为当前Session提供一个策略,Session由org.springframework.orm.hibernate4.SpringSessionContext.currentSession得到。将sessionFactory注入到其他Bean中,如注入到DAO组件中,使其获得SessionFactory的引用后,就可以实现对数据库的访问。
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p="http://www.springframework.org/schema/p"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xmlns:jee="http://www.springframework.org/schema/jee"
8 xmlns:tx="http://www.springframework.org/schema/tx"
9 xsi:schemaLocation="10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd11 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd13 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 cn/xyp/web/entity/User.hbm.xml
34
35
36
37
38 true
39 true
40 false
41 org.springframework.orm.hibernate3.SpringSessionContext
42 org.hibernate.dialect.MySQLDialect
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
applicationContext.xml
9、jsp
(1)displayAll.jsp显示所有信息
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
显示会员信息9
10
11
12
会员信息
13
14
15
会员id会员名密码会员年龄会员性别会员联系方式会员地址注册会员日期删除修改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
displayAll.jsp
(2) login.jsp用户登录
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
会员登陆9
10
11
12
13
14
15
16
17
18
19
login.jsp
(3)register.jsp用户注册
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
会员注册页面9
10
11
12
13
14
15
16
17
18 --%>
19
20
21
22
23
24
25
register.jsp
(4)update.jsp用户信息更改
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
修改会员信息9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
update.jsp
(5)welcome.jsp欢迎页面
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
欢迎来到时空联盟9
10 body{
11 background-image:url('WebContent/WEB-INF/img/shikong.png');
12 background-position:center top;
13 background-repeat:no-repeat;
14
15 }
16
17
18
19
Hello World!
20
21
welcome.jsp
本文参考自https://www.cnblogs.com/zhaozihan/p/5893785.html