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

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud