Spring S2S集成开发

[size=small][color=olive][color=red][b]

struts2.0 和 Spring 集成开发

知识点:DAO层的应用和基本的配置介绍

提示:S2集成Spring时struts2-spring-plugin-2[1].0.6.jar要加入到项目中去
1、首先搭建基本的struts2.0 的web 运行环境 和 Spring 的环境
2、配置 spring 在web 的运行环境:[/b][/color][/color][/size]
如:
[size=small][color=red][b] 在web.xml 中配置:
<!-- 配置Spring 在web上的运行环境 -->[/b][/color][/size]
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


[size=small][color=olive][b] 说明:
org.springframework.web.context.ContextLoaderListener 该类是:
org.springframework.web-3.0.1.Release-a.jar 下的
理解:好似事件中的监听,等待触发;

3、在struts.xml 中配置sturts2.0 与 spring 的管理关系
首先配置常量用来说明:struts 的对象由spring 来管理:
<constant name="struts.objectFactory" value="spring"></constant>

然后:action 中class 的值为:需要访问的Action 在Spring容器中bean 的ID,
通过容器来注入并实现调用;
例如:[/b][/color][/size]

<action name="user_*" method="{1}" class="useraction"></action>	

[size=small][color=red][b]说明:useraction 对应:[/b][/color][/size]
<bean id="useraction" class="com.svse.action.UserAction" scope="prototype"></bean>

[size=small][color=olive][b]注意:Action会被多次的调用,而且直接关系到界面和数据库数据的同步,所以此处必须明确的指出该bean 使用代理模式,以保证数据的同步性:即设置:scope="prototype";
[/b][/color][/size]
[size=small][color=red][b]4、在applicationContext.xml 中配置连接数据库的数据源:[/b][/color][/size]
<bean id="datasources" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="url" value="jdbc:sqlserver://localhost:1433;database=TESTDB"></property>
<property name="username" value="sa"></property>
<property name="password" value="svse"></property>
</bean>
说明:org.apache.commons.dbcp.BasicDataSource 这个类属于:commons-dbcp.jar包

<!-- 注入DAO层 : dataSource 是UserDao从JdbcDaoSupport 中继承而来,用于处理数据的 -->
<!-- 依赖注入数据源,马上就可以操作数据库了 -->
<bean id="userDao" class="com.svse.dao.UserDao">
<property name="dataSource" ref="datasources"></property>
</bean>


[size=small][color=olive][b]5、在DAO层 ,对数据进行增、删、该、查[/b][/color][/size]
<!--
package com.svse.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.svse.entity.UserEntity;

/**
*
* @author Administrator
* Spring 中DAO层,操作数据库需要继承JdbcDaoSupport *
*/
public class UserDao extends JdbcDaoSupport {

/**
* 增加
* @param user
*/
public void addUser(UserEntity user) {
this.getJdbcTemplate().update("insert into t_user values(?,?,?)",
new Object[]{user.getU_name(),user.getSex(),user.getHib()});
}

/**
* 删除
* @param uId
*/
public void delUser(int uId) {
this.getJdbcTemplate().update("delete from t_user where u_id=?",new Object[]{new Integer(uId)});
}

/**
* 查询所有
* @return list
*/
public List findAllUser() {

return this.getJdbcTemplate().query("select * from t_user",new RowMapper(){
/**
* rs : 保存了查询出来的结果集 , 此处可以直接进行获取,并且不需要进行while循环读取
* @return user : 此处返回的使用个UserEntity的实体对象,返回后由RwoMapper自动的封装成
* 一个集合,最后的返回给业务逻辑层
*/
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
UserEntity user = new UserEntity();
user.setU_id(rs.getInt("u_id"));
user.setU_name(rs.getString("u_name"));
user.setSex(rs.getString("u_sex"));
user.setHib(rs.getString("u_hid"));
return user;
}});
}

/**
* 查询一个
* @param uId
* @return user
*/
public UserEntity findUserById(int uId) {
//此处直接返回一个UserEntity 的实体对象,需要进行强转;
return (UserEntity)this.getJdbcTemplate().query("select * from t_user where u_id =?" ,
new Object[]{new Integer(uId)},
new ResultSetExtractor(){
/**
* @param rs : 一个结果集 , 此处获取rs中的值需要while判断取值
*/
public Object extractData(ResultSet rs)
throws SQLException, DataAccessException {
UserEntity user = null;
while(rs.next()){
user = new UserEntity();
user.setU_id(rs.getInt("u_id"));
user.setU_name(rs.getString("u_name"));
user.setSex(rs.getString("u_sex"));
user.setHib(rs.getString("u_hid"));
}
return user;
}});
}

/**
* 修改
* @param user
*/
public void uppUser(UserEntity user) {
this.getJdbcTemplate().update(
"update t_user set u_name=?,u_sex=? ,u_hid=? where u_id=?",
new Object[] {user.getU_name(),user.getSex(),user.getHib(),new Integer(user.getU_id())});
}

}

-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值