jdbctemplate

Spring学习笔记-Day4

£可爱£侵袭症+ 提交于 2020-01-14 11:16:52
4.1 Spring JDBC 4.1.1 Spring jdbc Template的解析 JdbcTemplate继承自抽象了JdbcAccessor,同时实现了JdbcOperations接口。 (1)JdbcOperations接口定义了再JdbcTemplate类中可以使用的操作集合,包括增删改查等操作 (2)JdbcTemplate类的直接父类是JdbcAccessor,该类为子类提供了一些访问数据库时使用的公共属性: DataSource:主要功能是获取数据库连接,具体实现时还可以引入对数据库连接的缓冲池和分布事务的支持,它可以做诶访问数据库资源的标准接口。 SQLExceptionTranslator:负责对SQLException进行转译工作。通过必要的设置或者获取SQLExceptionTranslator中的方法可以使JdbcTemplate在需要处理SQLException是委托它来完成相关的转译工作。 4.1.2 SpringJDBC的配置 SpringJDBC模块主要由4个包组成,分别是core(核心包)、DataSource(数据包)、object(对象包)和support(支持包)。 Spring对数据库的操作都封装在这几个包中,如果想要使用SpringJDBC,据需要对其进行配置。在Spring中

spring, jdbcTemplate, postgresql - last insert id

此生再无相见时 提交于 2020-01-13 20:24:14
问题 I have: public void addJobs(Jobs jobs) { this.getJdbcTemplate().update(sqlAddJobs, new Object[] {jobs.getJobName()}); } In Postgresql DBI have a table: row_id | jobs row_id is auto increment, how can I got last insert id? My sql: INSERT INTO jobs (jobs) VALUES (?) 回答1: One easy option is to make the query like: "INSERT INTO jobs(jobs) VALUES(?) RETURNING row_id" And execute int id = getJdbcTemplate().queryForInt(sql) . 来源: https://stackoverflow.com/questions/12819077/spring-jdbctemplate

设计模式十:模板模式

会有一股神秘感。 提交于 2020-01-13 00:52:10
目录 模板模式 代码示例 模板模式 模板模式通常又被称为模板方法模式(Template Method Pattern),是指定义一个算法的骨架,并允许子类为一个或多个步骤提供实现。模板模式属于行为型设计模式。 模板模式使用场景:一次性实现一个算法不变的部分,并将可变的行为留给子类来实现;子类中公共的行为被提取出来,并集中到一个公共的父类,避免了代码的重复。 模板模式优点:提高代码的复用性和扩展性,符合开闭原则。 模板模式缺点:增加了类数目,间接的增加了系统类之间的复杂度,基于继承实现,所以父类添加抽象类方法,那么所有子类都需要修改。 代码示例 以JDBC操作为例,创建一个JdbcTemplate,封装所有的JDBC操作,现在以查询为例,先创建约束ORM逻辑的接口RowMapper: public interface RowMapper < T > { T mapRow ( ResultSet rs , int rowNum ) ; } 所有的处理流程类JdbcTemplate: public class JdbcTemplate { private DataSource dataSource ; public JdbcTemplate ( DataSource dataSource ) { this . dataSource = dataSource ; } public List

Can I set a JDBC timeout for a single query?

ぃ、小莉子 提交于 2020-01-12 13:03:20
问题 I have a web app on Tomcat, which handles DB connection pooling, and using Spring JDBCTemplate for executing queries. It's been requested that I implement a status page which will be monitored by a heartbeat process to determine if everything is healthy with the server. As part of this, I want to do a DB query to determine if the connection to the database is ok. Ideally, since it'd just be a 'select 1 from ', I'd want it to come back fast, within 10 seconds, to indicate a failure if the DB

Springboot系列-整合JdbcTemplate

。_饼干妹妹 提交于 2020-01-11 07:59:51
Springboot系列-整合JdbcTemplate 前言:在项目开发中,很多时候需要对大量数据进行持久化,数据持久化常见的几种方式如:Spring自带的Template,mybatis或者常说的JPA等,此篇博客将主要对JdbcTemplate进行整合,虽然Template不如mybatis使用方便,但是JdbcTemplate算是最简单的数据持久化方案了 1.配置JdbcTemplate 1.新建JDBC API项目,选上 Jdbc 依赖,以及数据库驱动依赖即可 2.配置pom文件导入依赖,添加 Druid 数据库连接池依赖,此处注意不是一般在 SSM 中添加的 Druid,而是为springboot提供的druid-spring-boot-starter <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org

spring之为什么要使用事务?

落花浮王杯 提交于 2020-01-08 17:57:21
问题描述:现在我们有一个数据库:spring 三张表:account、book、book_stock account存储着用户以及账户余额。book存储着书号、名字和 购买一本所需金额。book_stock存储着书号以及对应的库存。 现在我们有这么一个需求: 用户买一本书,先让书的库存减一,然后在让用户余额减去相应的金额。 我们来看如何处理。 新建一个Java project,在项目下新建一个lib文件夹,在文件夹中加入以下包: 选中这些包,点击鼠标右键,选择build path,选择add to build path。 然后建立以下的目录结构: 一、配置连接数据库 db.properties jdbc.user=root jdbc.password=123456 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///spring jdbc.initPoolSize=5 jdbc.maxPoolSize=10 在applicationContex.xml中 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org

spring之操作数据库之使用NamedParameterJdbcTemplate(具名参数)

亡梦爱人 提交于 2020-01-08 16:37:30
接上一节:https://www.cnblogs.com/xiximayou/p/12167150.html。 在applicationContext.xml中配置namedParameterJdbcTemplate。 <!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean> 在JDBCTest.java中进行测试: private ApplicationContext ctx = null; private JdbcTemplate jdbcTemplate; private EmployeeDao employeeDao; private DepartmentDao departmentDao; private NamedParameterJdbcTemplate namedParameterJdbcTemplate; { ctx =

spring框架 事务 注解配置方式

半腔热情 提交于 2020-01-06 04:43:07
user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=15 maxPoolSize=30 minPoolSize=5 <?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http:

Spring jdbcTemplate Junit

天涯浪子 提交于 2020-01-05 04:07:11
问题 I have one application with DAO as follows. I want to junit this class. My Class looks like this. public class DaoImpl implements Dao{ @Override public User getUserInfo(String userid) { return getTemplate().queryForObject(QUERY, new Object[] { userid }, new BeanPropertyRowMapper<User>(User.class)); } } My junit class looks like this @RunWith(SpringJUnit4ClassRunner.class) public class DaoImplTests{ @Autowired private Dao dao; @Mock JdbcTemplate jdbcTemplate; @Test public void testUsingMockito

How spring JdbcTemplate recognizes data types?

為{幸葍}努か 提交于 2020-01-05 03:43:25
问题 When using spring JdbcTemplate with prepared statements we can either set values of the parameters individually or just pass an array of objects. jdbctemplate.update(sql, arg1, arg2); or jdbctemplate.update(sql, new Object[]{arg1, arg2}); Both of the methods are working. But I want to know how jdbctemplate knows to cast the data to match with the type of the database column when passed as an Object array. And is there a performance difference in two methods? How can I log the final query