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

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删