spring jdbcTemplate

本文介绍了一个基于Spring框架的应用示例,包括数据源配置、事务管理、定时任务配置等内容,并展示了如何利用Spring AOP进行事务传播配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 

<context:annotation-config />
 <context:component-scan base-package="com.tl,com.royzhou.jdbc" />

 

 <context:property-placeholder location="classpath:jdbc.properties" />
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${driverClassName}" />
  <property name="url" value="${url}" />
  <property name="username" value="${username}" />
  <property name="password" value="${password}" />
  <!-- 连接池启动时的初始值 -->
  <property name="initialSize" value="${initialSize}" />
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="${maxActive}" />
  <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
  <property name="maxIdle" value="${maxIdle}" />
  <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
  <property name="minIdle" value="${minIdle}" />
 </bean>

 <bean id="jdbcTemplate"
  class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

 <bean id="txManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 
 <!-- 定义事务传播属性XML配置  -->
    <tx:advice id="txAdvice" transaction-manager="txManager"> 
        <tx:attributes> 
            <tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true"/> 
            <tx:method name="*" propagation="REQUIRED"/> 
        </tx:attributes> 
    </tx:advice> 
      
    <aop:config> 
        <aop:pointcut id="transactionPointCut" expression="execution(* com.royzhou.jdbc..*.*(..))"/> 
        <aop:advisor pointcut-ref="transactionPointCut" advice-ref="txAdvice"/> 
    </aop:config>

</beans>

 

package com.royzhou.jdbc;

public class PersonBean {
 private int id;
 private String name;

 public PersonBean() {
 }
 
 public PersonBean(String name) {
  this.name = name;
 }
 
 public PersonBean(int id, String name) {
  this.id = id;
  this.name = name;
 }
 
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
 public String toString() {
  return this.id + ":" + this.name;
 }
}

 

 

package com.royzhou.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

@SuppressWarnings("unchecked")
public class PersonRowMapper implements RowMapper {
 //默认已经执行rs.next(),可以直接取数据
 public Object mapRow(ResultSet rs, int index) throws SQLException {
  PersonBean pb = new PersonBean(rs.getInt("id"),rs.getString("name"));
  return pb;
 }
}

 

 

package com.royzhou.jdbc;

import java.util.List;

public interface PersonService {
 
 public void addPerson(PersonBean person) throws Exception;
 
 public void updatePerson(PersonBean person);
 
 public void deletePerson(int id);
 
 public PersonBean queryPerson(int id);
 
 public List<PersonBean> queryPersons();
}

 

 

package com.royzhou.jdbc;

import java.sql.Types;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service("personService")
public class PersonServiceImpl implements PersonService {
    @Resource
 private JdbcTemplate jdbcTemplate;
 
 /**
  * 通过Spring容器注入datasource
  * 实例化JdbcTemplate,该类为主要操作数据库的类
  * @param ds
 
 public void setDataSource(DataSource ds) {
  this.jdbcTemplate = new JdbcTemplate(ds);
 }
  */
 public void addPerson(PersonBean person) throws Exception   {
  /**
   * 第一个参数为执行sql
   * 第二个参数为参数数据
   * 第三个参数为参数类型
   */
  jdbcTemplate.update("insert into person values(seq_person.nextval,?)", new Object[]{person.getName()}, new int[]{Types.VARCHAR});
  //throw new RuntimeException("运行期异常支持事务回滚");
  //throw new Exception("其他异常不支持事务回滚");
  
 }

 public void deletePerson(int id) {
  jdbcTemplate.update("delete from person where id = ?", new Object[]{id}, new int[]{Types.INTEGER});
 }

 
 @SuppressWarnings("unchecked")
 public PersonBean queryPerson(int id) {
  /**
   * new PersonRowMapper()是一个实现RowMapper接口的类,
   * 执行回调,实现mapRow()方法将rs对象转换成PersonBean对象返回
   */
  List<PersonBean> pbs = (List<PersonBean>)jdbcTemplate.query("select id,name from person where id = ?", new Object[]{id}, new PersonRowMapper());
  PersonBean pb = null;
  if(pbs.size()>0) {
   pb = pbs.get(0);
  }
  return pb;
 }

 
 @SuppressWarnings("unchecked")
 public List<PersonBean> queryPersons() {
  List<PersonBean> pbs = (List<PersonBean>) jdbcTemplate.query("select id,name from person", new PersonRowMapper());
  return pbs;
 }

 public void updatePerson(PersonBean person) {
  jdbcTemplate.update("update person set name = ? where id = ?", new Object[]{person.getName(), person.getId()}, new int[]{Types.VARCHAR, Types.INTEGER});
 }

 public JdbcTemplate getJdbcTemplate() {
  return jdbcTemplate;
 }

 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  this.jdbcTemplate = jdbcTemplate;
 }
}

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">
 <context:annotation-config />
 <context:component-scan base-package="com.tl,com.royzhou.jdbc" />
 
 <!-- spring任务 调度 --> 
    <task:executor id="executor" pool-size="5" /> 
    <task:scheduler id="scheduler" pool-size="10" /> 
    <task:annotation-driven executor="executor" scheduler="scheduler" />
 <!--
  <bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations"> <value>classpath:jdbc.properties</value>
  </property> </bean>
 -->
 <!-- jndi连接池配置 -->
 <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName">
   <value>java:comp/env/jdbc/gsjg</value>
  </property>
 </bean>

 
 
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="jndiDataSource" />
  <property name="mappingLocations">
   <list>
       <value>classpath:/com/tl/bean/*.hbm.xml</value>
   </list>
  </property>
  
  <!--

     <property name="annotatedClasses">
      <list>
         <value>com.tl.bean.system.User</value>
      </list>
  </property>
 
  -->

  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <!--  使用ehcache,适合项目用 -->
          <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
          <!--  查询方法使用二级缓存-->
          <prop key="hibernate.cache.use_query_cache">true</prop>
          <!--  最优化二级缓存-->
          <prop key="hibernate.cache.use_structured_entries">true</prop>
          <!--  完全禁用二级缓存开关,对那些在类的映射定义中指定cache的类,默认开启二级缓存-->
          <prop key="cache.use_second_level_cache">true</prop>
          <!-- prop key="hibernate.hbm2ddl.auto">create</prop>
          <prop key="hibernate.default_schema">
           ${dbunit.schema}
          </prop> -->
   </props>
  </property>
  
  <property name="packagesToScan">
   <list>
    <value>com.tl.bean</value>
   </list>
  </property>
 </bean>
 
 <!-- spring定时器  start-->
 <bean id="dayDataJob" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass">
   <value>com.servlet.DayDataQuartzTask</value>
  </property>
 </bean>
 <!-- 调度cron工作   -->
 <bean id="dayDataJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <ref bean="dayDataJob"/>
  </property>
  <property name="cronExpression">
   <value>0 30 0 * * ?</value>
  </property>
 </bean>
 <!-- 启动工作  -->
 <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="dayDataJobTrigger"/>
   </list>
  </property>
 </bean>
 <!-- spring定时器  end -->

 <!-- Hibernate 模板//-->
 <bean id="hibernateTemplate"
  class="org.springframework.orm.hibernate3.HibernateTemplate">
 <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 

 <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <!-- 事务处理的AOP配置 //
 <bean id="txProxyTemplate" abstract="true"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
 <property name="txManager" ref="txManager"/>
 <property name="transactionAttributes">
 <props>
 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
 <prop key="save">PROPAGATION_REQUIRED</prop>
 <prop key="update">PROPAGATION_REQUIRED</prop>
 <prop key="delete*">PROPAGATION_REQUIRED</prop>
 </props>
 </property>
 </bean>-->
 
 <context:component-scan base-package="org.lxh" />
 <tx:annotation-driven transaction-manager="txManager"/>
</beans>

 

@Controller("fwjkAction")
public class FwjkAction extends BaseAction implements ModelDriven<FwjkEntity>
{
 private FwjkEntity model = new FwjkEntity();
 
    @Resource
    private PersonService personService;
 public PersonService getPersonService() {
  return personService;
 }
 public void setPersonService(PersonService personService) {
  this.personService = personService;
 }

 

web.xml配置

<context-param>
  <param-name>contextConfigLocation</param-name>
  <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
  <param-value>classpath:beans.xml,classpath:bean-sqlserver.xml</param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  <!-- default: /WEB-INF/applicationContext.xml -->
 </listener>
内容概要:本文详细介绍了扫描单分子定位显微镜(scanSMLM)技术及其在三维超分辨体积成像中的应用。scanSMLM通过电调透镜(ETL)实现快速轴向扫描,结合4f检测系统将不同焦平面的荧光信号聚焦到固定成像面,从而实现快速、大视场的三维超分辨成像。文章不仅涵盖了系统硬件的设计与实现,还提供了详细的软件代码实现,包括ETL控制、3D样本模拟、体积扫描、单分子定位、3D重建和分子聚类分析等功能。此外,文章还比较了循环扫描与常规扫描模式,展示了前者在光漂白效应上的优势,并通过荧光珠校准、肌动蛋白丝、线粒体网络和流感A病毒血凝素(HA)蛋白聚类的三维成像实验,验证了系统的性能和应用潜力。最后,文章深入探讨了HA蛋白聚类与病毒感染的关系,模拟了24小时内HA聚类的动态变化,提供了从分子到细胞尺度的多尺度分析能力。 适合人群:具备生物学、物理学或工程学背景,对超分辨显微成像技术感兴趣的科研人员,尤其是从事细胞生物学、病毒学或光学成像研究的科学家和技术人员。 使用场景及目标:①理解和掌握scanSMLM技术的工作原理及其在三维超分辨成像中的应用;②学习如何通过Python代码实现完整的scanSMLM系统,包括硬件控制、图像采集、3D重建和数据分析;③应用于单分子水平研究细胞内结构和动态过程,如病毒入侵机制、蛋白质聚类等。 其他说明:本文提供的代码不仅实现了scanSMLM系统的完整工作流程,还涵盖了多种超分辨成像技术的模拟和比较,如STED、GSDIM等。此外,文章还强调了系统在硬件改动小、成像速度快等方面的优势,为研究人员提供了从理论到实践的全面指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值