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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定