Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证用户名和密码
来源:互联网 发布:php页面传递数组 编辑:程序博客网 时间:2024/06/05 06:44
注:本系列文章部分内容来自王健老师编写ssh整合开发教程
使用Spring的AOP进行项目的事务管理,已经成为很多企业的首先,Spring做为优秀的开源项目,其在数据库连接、事务管理方面的优势已经显现,当然后期我们还会使用Hibernate进行数据操作,但仍不能丢弃Spring作为中间层的重要意义。
第一步:书写DDL(数据定义语言)语句,创建mysql数据库和表格,如下图-1所示:
提示:在ssh项目下,新建一个ssh.sql文件,其中存放所有的建库、建表语句。
图-1
第二步:创建一个资源文件,位于WEB-INF/conf目录下,输入数据库的url等信息如下图-2所示:
、
图-2
第三步:将连接mysql的jar文件拷贝到WEB-INF/lib目录下,由于连接数据路,以后使用AOP进行事务方面的控制,所以需要将以下几个包拷贝到WEB-INF/lib目录下。
图-3
第四步:在applicationContext.xml中配置数据库的连接如下图-4所示:
图-4
第五步:发布项目,并启动程序,如果能正常启动,说明数据库连接成功,调试程序,直到正常启动。
第六步:修改OneDaoImpl.java类,执行数据查询,修改后的代码如下:
图-5
第七步:在applicationContext.xml中,给OneDaoImple注入DataSource属性如下图-6所示:
图-6
第八步:发布程序,并测试登录,输入如下:
图-7
第九步:配置数据库事务,配置的高级部分,如果能够掌握可成为领域的行家:
提示:在上面的配置过程当中,虽然已经可以正常操作数据库进行增删改查操作,但是当用户发生异常时,不能使用户的操作回滚到原始状态,即没有事务,所以为了保证用户操作的原子性,应该给我们的数据添加事务。
9.1、增加applicationContext.xml的文档类型头定义,以支持aop的配置如下图-8所示:
图-8
提示:为了防止出错,可以使用以下文档类型头直接覆盖applicationContext.xml的文档类型头:(建议直接拷贝)
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
9.2、配置事务控制,在applicationContext.xml中增加以下配置:
图-9
提示:1、按照1~3的顺序书写。
2、绿色双向箭头为引用关系
3、椭圆的圈中,execution(* com..*Service.*(..))是指对com包中的,所有的以Service结尾的接口当中的所有方法,无论接受什么样的参数,返回什么样的数据类型,都进行事务控制。
第十步:发布项目,并启动,如果启动不成功应该检查配置文件是否正确,直到能正常启动为止。
总结:到目前为止,Spring已经整合完毕,下一步的工作如下:
1、增加一个功能,查询显示所有用户。
2、整合Hibernate,并同样完成查询显示所有用户的功能。
3、增加删除、增加、修改用户的功能(使用Hibernate)。
源代码下载地址:
http://download.csdn.net/detail/u014548782/7216361
- Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证用户名和密码
- Struts2+Spring+Hibernate step by step 03 整合Spring之一(在DAO层验证用户名和密码)
- Struts2+Spring+Hibernate step by step 11 ssh整合之拦截器验证用户是否登录
- Struts2+Spring+Hibernate step by step 06 整合Hibernate
- SSH2 Step by Step- Step 4 - Struts2、Spring和Hibernate的整合
- Struts2+Spring+Hibernate step by step 07 ssh整合之增加用户
- Struts2+Spring+Hibernate step by step 08 ssh整合之删除用户
- Struts2+Spring+Hibernate step by step 09 ssh整合之查询用户
- Struts2+Spring+Hibernate step by step 10 ssh整合之修改用户
- Struts2+Spring+Hibernate step by step 01 创建Struts2项目
- Struts2+Spring+Hibernate step by step 02 模拟用户登录(非数据库)
- SSH2 Step by Step- Step 3 Struts2和Hibernate的整合
- Struts2+Spring+Hibernate step by step 05 使用Spring查询显示所有用户
- Spring MVC Step by Step
- Spring Aop Step-By-Step 学习笔记
- Spring MVC step by step 1--DispatcherServlet
- Spring MVC: step by step 2-SimpleUrlHandlerMapping
- Spring mvc: step by step 4-MultiActionController
- POJ 1182 食物链
- JAVA容器(七)---HashMap的实现原理
- 匈牙利算法
- c++ const知多少
- DNS使用的是TCP协议还是UDP协议
- Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证用户名和密码
- 谈下一代企业和下一代技术
- ZOJ题目分类
- String类其他操作方法
- poj 1977 Odd Loving Bakers(矩阵)
- android之ContentProvider和Uri详解
- FZU--2155 盟国
- Mondrian之XMLA服务
- 浅谈gcc和g++的相同点与不同点