Struts2 Spring3 集合 使用springjdbc操作数据(一)

2024-05-26 21:38

本文主要是介绍Struts2 Spring3 集合 使用springjdbc操作数据(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

刚来公司不久,由于公司项目是买了的,现在不想受他们的限制,所以需要重新打一个框架了,废话不多说了 直接上代码吧

首先struts2 -----2.3.28

spring3------3.2.6

所有集成的包,仅做参考。

首先还配置web.xml吧

	<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 添加spring监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring配置文件路径 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>

注意的是吧资源文件放在classpath下  他会找到的

看下我的项目结构


首先配置struts 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="false" /><constant name="struts.devMode" value="true" /><!<span style="color:#ff0000;">-- 将action托管给spring </span>--><constant name="struts.objectFactory" value="spring"></constant> <package name="default" namespace="/" extends="struts-default"><default-action-ref name="index" /><global-results><result name="error">/WEB-INF/jsp/error.jsp</result></global-results><global-exception-mappings><exception-mapping exception="java.lang.Exception" result="error"/></global-exception-mappings><action name="user" class="com.core.database.action.UserAction"><!-- 定义逻辑视图和物理资源之间的映射 --><result name="success">/WEB-INF/jsp/a.jsp</result><result name="error">/error.jsp</result></action></package></struts>

这里只是简单的配置了一下

接下来就是数据操作了  建库 建表  我这里只有一个表  简单案列嘛   你懂得

user        id    name 

然后就是spring 配置了(重点)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsdhttp://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"><!-- 属性文件读入 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath*:jdbc-mysql.properties</value></list></property></bean><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">      <property name="driverClass" value="${jdbc.driverClassName}" />      <property name="jdbcUrl" value="${jdbc.url}" />      <property name="user" value="${jdbc.username}" />      <property name="password" value="${jdbc.password}" />    <property name="minPoolSize" value="10" />  <property name="maxPoolSize" value="20" />  <property name="maxIdleTime" value="1800" />  <property name="acquireIncrement" value="2" />  <property name="maxStatements" value="0" />  <property name="initialPoolSize" value="2" />  <property name="idleConnectionTestPeriod" value="60" />  <property name="acquireRetryAttempts" value="30" />  <property name="breakAfterAcquireFailure" value="true" />  <property name="testConnectionOnCheckout" value="true" />  </bean>  <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref local="dataSource" /></property></bean><bean id="txManager" abstract="true"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager" ref="transactionManager" /><property name="transactionAttributes"><props><prop key="add*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="save*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="edit*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="update*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="del*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="handle*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="notify*">PROPAGATION_REQUIRED,-DataAccessException</prop><prop key="get*">PROPAGATION_REQUIRED,readOnly</prop><prop key="query*">PROPAGATION_REQUIRED,readOnly,-DataAccessException</prop><prop key="find*">PROPAGATION_REQUIRED,readOnly</prop><prop key="is*">PROPAGATION_REQUIRED,readOnly</prop></props></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg ref="dataSource" /></bean><import resource="bean.xml"/> 
</beans>


使用的是c3p0 数据源 用JdbcTemplate 来操作数据

UserDao

public interface UserDao {public List<User> getUserAll();
}

UserDaoImpl

public class UserDaoImpl implements UserDao{private JdbcTemplate template;public JdbcTemplate getTemplate() {return template;}public void setTemplate(JdbcTemplate template) {this.template = template;}@Overridepublic List<User> getUserAll() {String sql = "select * from user ";return (List<User>) template.query(sql, new BeanPropertyRowMapper(User.class));}

需要在spring文件配置 dao

    <bean id="userDao" class="com.core.database.dao.impl.UserDaoImpl"><property name="template"><ref bean="jdbcTemplate" /></property></bean>

好接下来就是测试,见证奇迹的时候道了


	private static Log log = LogFactory.getLog(SpringTest.class);public static void main(String[] args) {ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");log.info("spring配置文件ok");UserDao ormDao = (UserDao) ac.getBean("userDao");
//		DataSources d = new DataSUserDaoImpl b = new UserDaoImpl();System.out.println(ormDao.getUserAll().size());}

是不是可以查询了~~~不可以的就找bug吧

接下来就是action了

public class UserAction extends ActionSupport{/*** */private static final long serialVersionUID = 1L;private UserDao userDao;public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}public String execute() throws Exception{System.out.println(userDao.getUserAll().size());return SUCCESS;}
}

继承的是 ActionSupport 具体为什么就不多说了  配置在上面已经给出了

接下来最后一步 在spring 配置文件中配置 action  让他管理

    <bean id="userDao" class="com.n525m.core.database.dao.impl.UserDaoImpl"><property name="template"><ref bean="jdbcTemplate" /></property></bean><bean id="user" class="com.n525m.core.database.action.UserAction">  <property name="userDao" ref="userDao" />  </bean>  


恩 基本就是这样了,一个简单的就搭建完成了,


这篇关于Struts2 Spring3 集合 使用springjdbc操作数据(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1005664

相关文章

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

OpenCV在Java中的完整集成指南分享

《OpenCV在Java中的完整集成指南分享》本文详解了在Java中集成OpenCV的方法,涵盖jar包导入、dll配置、JNI路径设置及跨平台兼容性处理,提供了图像处理、特征检测、实时视频分析等应用... 目录1. OpenCV简介与应用领域1.1 OpenCV的诞生与发展1.2 OpenCV的应用领域2

在Java中使用OpenCV实践

《在Java中使用OpenCV实践》用户分享了在Java项目中集成OpenCV4.10.0的实践经验,涵盖库简介、Windows安装、依赖配置及灰度图测试,强调其在图像处理领域的多功能性,并计划后续探... 目录前言一 、OpenCV1.简介2.下载与安装3.目录说明二、在Java项目中使用三 、测试1.测

Spring Bean初始化及@PostConstruc执行顺序示例详解

《SpringBean初始化及@PostConstruc执行顺序示例详解》本文给大家介绍SpringBean初始化及@PostConstruc执行顺序,本文通过实例代码给大家介绍的非常详细,对大家的... 目录1. Bean初始化执行顺序2. 成员变量初始化顺序2.1 普通Java类(非Spring环境)(

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么