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

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu