jdbctemplate

spring boot 连接Mysql介绍

回眸只為那壹抹淺笑 提交于 2019-12-01 11:59:44
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis generator 配置 spring boot 接口返回值封装 spring boot输入数据校验(validation) spring boot rest 接口集成 spring security(1) – 最简配置 spring boot rest 接口集成 spring security(2) – JWT配置 spring boot 异常(exception)处理 spring boot 环境配置(profile)切换 spring boot redis 缓存(cache)集成 概述 java应用的数据库接口的层次图如下 JDBC Java应用通过JDBC接口访问数据库,JDBC(Java DataBase Connectivity/Java数据库连接)为各种数据库,如mysql

Spring JDBC - Last inserted id

你。 提交于 2019-12-01 10:57:08
问题 I'm using Spring JDBC. Is a simple way to get last inserted ID using Spring Framework or i need to use some JDBC tricks ? jdbcTemplate.update("insert into test (name) values(?)", params, types); // last inserted id ... I found something like below, but i get: org.postgresql.util.PSQLException: Returning autogenerated keys is not supported. KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement

Spring JdbcTemplate方法详解

谁都会走 提交于 2019-12-01 06:36:04
JdbcTemplate主要提供以下五类方法: execute方法: 可以用于执行任何SQL语句,一般用于执行DDL语句; update方法及batchUpdate方法: update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句; query方法及queryForXXX方法: 用于执行查询相关语句; call方法: 用于执行存储过程、函数相关语句。 JdbcTemplate类支持的回调类: 预编译语句及存储过程创建回调: 用于根据JdbcTemplate提供的连接创建相应的语句; PreparedStatementCreator :通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的PreparedStatement; CallableStatementCreator: 通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的CallableStatement; 预编译语句设值回调: 用于给预编译语句相应参数设值; PreparedStatementSetter: 通过回调获取JdbcTemplate提供的PreparedStatement,由用户来对相应的预编译语句相应参数设值; BatchPreparedStatementSetter:

Hibernate与JdbcTemplate共享事务管理

久未见 提交于 2019-12-01 04:56:39
在Spring和Hibernate的配置文件中,我们可以对类中的方法进行事务控制,也就是说某个方法中含有多个数据库的写操作,我们可以通过创建一个Spring中的HibernateTransactionManager实例,把相应的sessionFactory注入到其的sessionFactory属性中,由事务声明的方式进行事务控制。样例如下: <bean id="oaTM" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref bean="oaSessionFactory"/></property> </bean> 但是这种方式有个局限性,如果该方法中既有调用Hibernate进行存储,也有通过JdbcTemplate进行数据库的写操作,如果在方法执行过程中,写数据发生异常时,只有纳入了事务管理的通过Hibernate进行存储的数据才会回滚,而通过JdbcTemplate方式进行操作的数据不会进行回滚。 原因是Hibernate与JdbcTemplate使用的是不同DBConnection,而且JdbcTemplate未申明相应的事务管理,所以要想在既使用了Hibernate

使用泛型和反射技术简化Spring jdbcTemplate的使用

与世无争的帅哥 提交于 2019-12-01 04:56:28
最近没事儿研究了下springMVC,因为不想用hibernate,所以就是用了spring自带的jdbcTemplate。在使用的过程中发现spring jdbcTemplate需要自己实现将结果集转化为对象的操作,个人感觉很是繁琐,于是就使用泛型和反射对这个过程进行了封装,来简化jdbcTemplate的使用。废话少说,上代码: public class MyRowMapper<T> implements RowMapper { private Class<T> cls; public MyRowMapper(Class<T> cls) { this.cls = cls; } @Override public Object mapRow(ResultSet rs, int num) throws SQLException { T t = null; try { // 获取对象中的所有字段 Field[] fields = cls.getDeclaredFields(); // 实例化 t = cls.newInstance(); for(Field f : fields){ // if(f.isAnnotationPresent(NotPersistent.class)){ // continue; // } // 获取字段名称 String fieldName = f

Spring AOP根据JdbcTemplate方法名动态设置数据源

只愿长相守 提交于 2019-12-01 03:48:14
说明: 现在的场景是,采用MySQL Replication的方式在两台不同服务器部署并配置主从(Master-Slave)复制; 并需要程序上的数据操作方法访问不同的数据库,比如,update*方法访问主数据库服务器,query*方法访问从数据库服务器,从而减轻读写操作数据库的压力。 即把“增删改”和“查”分开访问两台服务器,当然两台服务器的数据库同步事先已经配置好。 然而程序是早已完成的使用Spring JdbcTemplate的架构,如何在不修改任何源代码的情况下达到此功能呢? 分析: 1. 目前有两个数据源需要配置到Spring框架中,如何统一管理这两个数据源? JdbcTemplate有很多数据库操作方法,关键的可以分为以下几类(使用简明通配符):execute(args..)、update( args.. )、batchUpdate( args.. )、query*( args.. ) 2. 如何根据这些方法名来使用不同的数据源呢? 3. 多数据源的事务管理(暂未处理) 实现: Spring配置文件applicationContext.xml(包含相关bean类的代码) 1. 数据源配置(省略了更为详细的连接参数设置): <bean id="masterDataSource" class="org.apache.commons.dbcp.BasicDataSource"

Getting “Invalid column type” excecption, while using NamedParameterJDBCTemplate for insertion

笑着哭i 提交于 2019-12-01 03:31:46
问题 I am using below code while inserting a row into database(oracle 10g xe,jar: ojdbc14.jar) String sql = "INSERT INTO SPONSOR_TB(ID,NAME,INDUSTRY_TYPE,IS_REPORTING_SPONSOR,IS_NOT_SOLICITE) VALUES(SEQ_SPONSOR_ID.NEXTVAL,:NAME1,:INDUSTRY_TYPE,:IS_REPORTING_SPONSOR,:IS_NOT_SOLICITE)"; MapSqlParameterSource paramSource = new MapSqlParameterSource(); paramSource.addValue("NAME1",sponsor.getName()); paramSource.addValue("INDUSTRY_TYPE", sponsor.getIndustryType()); paramSource.addValue("IS_NOT

配置多数据源 spring boot

元气小坏坏 提交于 2019-12-01 02:17:06
一、使用jdbc 配置多数据源 1.yml数据源配置 2.配置类 package com.v246.common.config.datasource; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io

JdbcTemplate IN Clause for String elements

江枫思渺然 提交于 2019-11-30 20:52:04
Am using NamedParameterJdbcTemplate for where Clause elements and one of them seems to be List<String> . JdbcTemplate replaces them ?,?,?...(list size) but for a IN clause with List<String> it has to be '?','?'.... Is there a way around this? Yogesh Chawla There a few other similar questions out there which might have helpful answers for you: How to execute IN() SQL queries with Spring's JDBCTemplate effectivly? To make this style of query work on my end, I have to switch from plain old JDBCTemplate to NamedParameterJdbcTemplate . Here is some example code: String query = "select * from table

在SpringBoot项目新增一个jdbcTemplate数据源

瘦欲@ 提交于 2019-11-30 16:14:35
//配置application-dev.ymlspring: datasource1: url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Test username: sa password: 123456 driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver package com.example.test.config; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties("spring