jdbctemplate

Spring JDBC最佳实践(1)

霸气de小男生 提交于 2019-11-30 03:21:17
原文地址: https://my.oschina.net/u/218421/blog/38513 Spring提供了两种使用JDBC API的最佳实践,一种是以JdbcTemplate为核心的基于Template的JDBC的使用方式,另一种则是在JdbcTemplate基础之上的构建的基于操作对象的JDBC的使用方式。 基于Template的JDBC的使用方式 该使用方式的最初设想和原型,需要追溯到Rod Johnson在03年出版的Expert One-on-One J2EE Design and Development,在该书的Practical Data Access(数据访问实践)中,Rod针对JDBC使用中的一些问题提出了一套改进的实践原型,并最终将该原型完善后在Spring框架中发布。 JDBC的尴尬 JDBC作为Java平台的访问关系数据库的标准,其成功是 有目共睹的。几乎所有java平台的数据访问,都直接或者间接的使用了JDBC,它是整个java平台面向关系数据库进行数据访问的基石。 作为一个标准,无疑JDBC是很成功的,但是要说JDBC在使用过程当中多么的受人欢迎,则不尽然了。JDBC主要是面向较为底层的数据库操作,所以在设计的过程当中 ,比较的贴切底层以提供尽可能多的功能特色。从这个角度来说,JDBC API的设计无可厚非。可是,过于贴切底层的API的设计

Multiple DataSource and JdbcTemplate in Spring Boot (> 1.1.0)

亡梦爱人 提交于 2019-11-30 02:46:16
I would like to inject a specific JdbcTemplate in a Spring Boot project. I tried to follow this example for multiple DataSource configuration : http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-available-now My code does compile and run, but only the DataSource with the @Primary annotation is taken into account, no matter what I put as @Qualifier in the SqlService class. My relevant code is the following : DatabaseConfig.java : @Configuration public class DatabaseConfig { @Bean(name = "dsSlave") @ConfigurationProperties(prefix="spring.mysql_slave") public DataSource slaveDataSource() {

jdbctemplate调用存储过程

房东的猫 提交于 2019-11-30 01:34:45
项目需要使用原生态的jdbc调用存储过程,写法如下,备忘一下 首先声明一个存储过程 CREATE DEFINER = `root`@`localhost` PROCEDURE `P_SVNTASKADD`(in par1 varchar(1000),in par2 varchar(100),in par3 varchar(200),in par4 varchar(200),in par5 varchar(100),in par6 varchar(2500),out parout int) begin insert into svnTask (svnUrl,svnType,developer,dba,createTime,modifyTime,filesNum,filesString,state) values(par1,par2,par3,par4,now(),now(),par5,par6,0); select MAX(taskId) into parout from svntask; end; 接下来在java中使用如下代码调用: String sql = "{call P_SVNTASKADD(?,?,?,?,?,?,?)}"; Object obj = jdbcTemplate.execute(sql,new CallableStatementCallback(){

使用Spring JDBC连接数据库(以SQL Server为例)

こ雲淡風輕ζ 提交于 2019-11-29 23:53:12
一、配置Spring JDBC 1.导入相关jar包 (略) 2.配置文件applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <!-- 驱动类名 --> <property name="url" value="jdbc:sqlserver://127.0.0

spring jdbctemplate nullpointerexception

不想你离开。 提交于 2019-11-29 23:39:01
dao经junit测试没问题, 但在dao1调用dao2时,出现空指针异常。 描述一下情况: diray类中有个type属性,这个type是个类, 我在dao方法中在加载diray类时同时加载type类, diray.setType(new TypeDaoImpl().byId(1)); 这种写法是错误的,没有使用spring的注入特性,这里我自己新new了实例, 但,typeDaoImpl继承了spirng的jdbcDaoSupport,我单纯的实例化TypeDaoImpl时,DataSource 和 jdbcTemplate并没有实例化,导致jdbctemplate类报空指针异常。 想到这里,豁然开朗, private TypeDao typeDao; private setTypeDao(TypeDao typeDao){ this.typeDao=typeDao; } diray.setType( typeDao .byId(1)); 对spirng的注入认识不清楚。低级错误,低级错误。。。 来源: oschina 链接: https://my.oschina.net/u/135304/blog/27946

How to get Map data using JDBCTemplate.queryForMap

偶尔善良 提交于 2019-11-29 22:57:05
How to load data from JDBCTemplate.queryForMap() and it returns the Map Interface.How the maintained the query data internally in map.I trying to load but i got below exception i.e., org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result Code:- public List getUserInfoByAlll() { List profilelist=new ArrayList(); Map m=new HashMap(); m=this.jdbctemplate.queryForMap("SELECT userid,username FROM USER"); Set s=m.keySet(); Iterator it=s.iterator(); while(it.hasNext()){ String its=(String)it.next(); Object ob=(Object)m.get(its); log.info("UserDAOImpl::getUserListSize()"+ob);

Java Web学习(六)

巧了我就是萌 提交于 2019-11-29 19:57:45
功能介绍 登录页面提供一个用户名/密码输入表单,用户填写并提交表单后,服务端检查是否有匹配的用户名/密码。若没有,则返回到登录页面,并给出提示。若匹配,则记录用户的登录日志,更新用户的最近登录时间和IP,并给用户增加5个积分,然后重定向到欢迎页面。 分析 ①首先由两个实体对象类User、Log.实体对象类一般都有对应的数据表。 ②在持久层有两个DAO类,UserDAO和LogDAO。UserDAO负责查询用户名/密码是否存在,并更新用户信息。LogDAO用来记录登录日志。 在业务层有一个UserService服务类、 在视图层有一个LoginController类,和两个jsp页面login.jsp和main.jsp。 用一个时序图来描述登录模块的整体交互过程 环境准备 ①创建数据库表 CREATE DATABASE sampleDB DEFAULT CHARACTOR SET UTF8; USER sampleDB; CREATE TABLE t_user (user_id int auto_increment primay kay, user_name varchar(30), credits int, password varchar(30), last_visit datetime, last_ip varchar(30))ENGINE=InnoDB; CREATE

Spring JDBCTemplate VS Hibernate in terms of performance [closed]

不想你离开。 提交于 2019-11-29 19:32:28
In our project we have to decide between Spring JDBCTemplate and Hibernate. I want to know which is better in terms of performance and implementation and design. and how? If you do all you can to make both implementations very fast, the JDBC template will probably be a bit faster, because it doesn't have the overhead that Hibernate has. But it will probably take much more time and lines of code to implement. Hibernate has its learning curve, and you have to understand what happens behind the scenes, when to use projections instead of returning entities, etc. But if you master it, you'll gain

Spring的数据库开发

删除回忆录丶 提交于 2019-11-29 16:05:26
spring的jdbcTemplate操作(用在dao层) spring框架是一个一站式框架,在每一层都提供了解决技术:在Dao层是使用了jdbcTemplate。 spring针对不同的持久化技术都提供了不同的模板。 Spring JDBC   Spring的JDBC模板负责提供数据库资源的管理和错误处理,大大简化了开发人员对数据库操作,使得开发人员可以从繁琐的数据库操作中解脱出来。 Spring jdbcTemplate的解析   针对数据库的操作,Spring框架提供了jdbcTemplate类,该类是Spring框架数据层的基础,其他更高层次的抽象类是构建在JdbcTemplate类之上,可以说,JdbcTemplate是Spring JDBC的核心类。   JdbcTemplata类的继承关系十分简单,它继承了JdbcAccessor抽象类,同时实现了JdbcOperations接口。   JdbcAccessor的设计中,对DataSource数据源进行了管理和配置,JdbcOperation接口定义中,定义了通过JDBC操作数据库的基本方法,而核心类JdbcTemplate提供了这些接口方法的实现。 Spring JDBC的配置   Spring JDBC模板主要是有四个包组成,分别是core(核心包),dataSource(数据源包),object(对象包)

How to set custom connection properties on DataSource in Spring Boot 1.3.x with default Tomcat connection pool

折月煮酒 提交于 2019-11-29 13:37:45
问题 I need to set some specific Oracle JDBC connection properties in order to speed up batch INSERT s ( defaultBatchValue ) and mass SELECT s ( defaultRowPrefetch ). I got suggestions how to achieve this with DBCP (Thanks to M. Deinum) but I would like to: keep the default Tomcat jdbc connection pool keep application.yml for configuration I was thinking about a feature request to support spring.datasource.custom_connection_properties or similar in the future and because of this tried to pretent