Spring JdbcTemplate用法整理

时间:2022-09-02 12:27:12

Spring JdbcTemplate用法整理:

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="dbcpds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@10.1.7.148:1521:ora11"></property>
<property name="username" value="tds1ml1"></property>
<property name="password" value="tds1ml1"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
<bean id="sprds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@10.1.7.148:1521:ora11"></property>
<property name="username" value="tds1ml1" />
<property name="password" value="tds1ml1" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dbcpds"></constructor-arg>
</bean>
<bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dbcpds"></constructor-arg>
</bean>
<bean id="jdbcSpitterDAO" class="com.stono.sprjdbc.JdbcSpitterDAO">
<property name="template" ref="jdbcTemplate"></property>
<property name="namedJdbcTemplate" ref="namedJdbcTemplate"></property>
</bean>
<bean id="jdbcSpitter2DAO" class="com.stono.sprjdbc.JdbcSpitter2Dao">
<property name="dataSource" ref="dbcpds"></property>
</bean>
</beans>

DAO Interface:

package com.stono.sprjdbc;
import java.util.Map;
public interface SpitterDAO {
void insertSpitter(SpitterObj obj);
void namedInsertSpitter(Map<String, Object> map);
void updateSpitter(SpitterObj obj);
void deleteSpitter(SpitterObj obj);
SpitterObj getSpitter(String ljdm);
}

POJO:

package com.stono.sprjdbc;
public class SpitterObj {
private String ljdm;
private String ljpym;
public String getLjdm() {
return ljdm;
}
public void setLjdm(String ljdm) {
this.ljdm = ljdm;
}
public String getLjpym() {
return ljpym;
}
public void setLjpym(String ljpym) {
this.ljpym = ljpym;
}
}

Use JdbcTemplate:

package com.stono.sprjdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
public class JdbcSpitterDAO implements SpitterDAO {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
private NamedParameterJdbcTemplate namedJdbcTemplate;
public void setNamedJdbcTemplate(NamedParameterJdbcTemplate namedJdbcTemplate) {
this.namedJdbcTemplate = namedJdbcTemplate;
}
private static String INSERT_SQL = "insert into ljzdtemp(ljdm,ljpym) values(?,?)";
private static String GET_SQL = "select * from ljzdtemp where ljdm = ?";
private static String UPDATE_SQL = "update ljzdtemp set ljpym=? where ljdm = ?";
private static String DEL_SQL = "delete ljzdtemp where ljdm = ?";
private static String NAMED_INSERT_SQL = "insert into ljzdtemp(ljdm,ljpym) values(:ljdm,:ljpym)";
@Override
public void insertSpitter(SpitterObj obj) {
template.update(INSERT_SQL, obj.getLjdm(), obj.getLjpym());
}
@Override
public void updateSpitter(SpitterObj obj) {
template.update(UPDATE_SQL, "aa", "a");
}
@Override
public void deleteSpitter(SpitterObj obj) {
template.update(DEL_SQL, "a");
}
@Override
public SpitterObj getSpitter(String ljdm) {
return template.queryForObject(GET_SQL, new RowMapper<SpitterObj>() {
@Override
public SpitterObj mapRow(ResultSet rs, int rowNum) throws SQLException {
SpitterObj obj = new SpitterObj();
obj.setLjdm(rs.getString(4));
obj.setLjpym(rs.getString(5));
return obj;
}
}, 'a');
}
@Override
public void namedInsertSpitter(Map<String, Object> map) {
namedJdbcTemplate.update(NAMED_INSERT_SQL, map);
}
}

Extends NamedParameterJdbcDaoSuport:

package com.stono.sprjdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
public class JdbcSpitter2Dao extends NamedParameterJdbcDaoSupport implements SpitterDAO {
private static String INSERT_SQL = "insert into ljzdtemp(ljdm,ljpym) values(?,?)";
private static String GET_SQL = "select * from ljzdtemp where ljdm = ?";
private static String UPDATE_SQL = "update ljzdtemp set ljpym=? where ljdm = ?";
private static String DEL_SQL = "delete ljzdtemp where ljdm = ?";
private static String NAMED_INSERT_SQL = "insert into ljzdtemp(ljdm,ljpym) values(:ljdm,:ljpym)";
@Override
public void insertSpitter(SpitterObj obj) {
getJdbcTemplate().update(INSERT_SQL, obj.getLjdm(), obj.getLjpym());
}
@Override
public void namedInsertSpitter(Map<String, Object> map) {
getNamedParameterJdbcTemplate().update(NAMED_INSERT_SQL, map);
}
@Override
public void updateSpitter(SpitterObj obj) {
getJdbcTemplate().update(UPDATE_SQL, "aa", "a");
}
@Override
public void deleteSpitter(SpitterObj obj) {
getJdbcTemplate().update(DEL_SQL, "a");
}
@Override
public SpitterObj getSpitter(String ljdm) {
return getJdbcTemplate().queryForObject(GET_SQL, new RowMapper<SpitterObj>() {
@Override
public SpitterObj mapRow(ResultSet rs, int rowNum) throws SQLException {
SpitterObj obj = new SpitterObj();
obj.setLjdm(rs.getString(4));
obj.setLjpym(rs.getString(5));
return obj;
}
}, 'a');
}
}

AppBean:

package com.stono.sprjdbc;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class AppBeans14 {
public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans14.xml");
BasicDataSource dataSource = (BasicDataSource) context.getBean("dbcpds");
Connection connection = dataSource.getConnection();
System.out.println(connection);
DriverManagerDataSource sprDataSource = (DriverManagerDataSource) context.getBean("sprds");
Connection connection2 = sprDataSource.getConnection();
System.out.println(connection2);
// 普通sql查询
// String sql = "select * from ljzdtemp";
// PreparedStatement prepareStatement = connection.prepareStatement(sql);
// PreparedStatement prepareStatement = connection2.prepareStatement(sql);
// ResultSet resultSet = prepareStatement.executeQuery();
// while(resultSet.next()){
// System.out.println(resultSet.getString(1));
// }
SpitterDAO dao = (SpitterDAO) context.getBean("jdbcSpitterDAO");
// JdbcTemplate 插入
// SpitterObj obj = new SpitterObj();
// obj.setLjdm("a");
// obj.setLjpym("a");
// dao.insertSpitter(obj);
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("ljdm", "a");
// map.put("ljpym", "b");
// dao.namedInsertSpitter(map);
SpitterDAO dao2 = (SpitterDAO) context.getBean("jdbcSpitter2DAO");
Map<String, Object> map = new HashMap<String, Object>();
map.put("ljdm", "b");
map.put("ljpym", "c");
dao2.namedInsertSpitter(map);
}
}

dbcp连接池需要的jar包:

commons-dbcp-1.4.jar commons-pool-1.6.jar

Spring JdbcTemplate用法整理的更多相关文章

  1. &lpar;转&rpar;Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  2. Java Spring AOP用法

    Java Spring AOP用法 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制 ...

  3. Java Spring IOC用法

    Java Spring IOC用法 Spring IoC 在前两篇文章中,我们讲了java web环境搭建 和 java web项目搭建,现在看下spring ioc在java中的运用,开发工具为In ...

  4. &lbrack;转&rsqb;Spring JdbcTemplate 查询分页

    原文:http://blog.csdn.net/xiaofanku/article/details/4280128 现在进行的项目由于数据库的遗留原因(设计的不堪入目)不能用hibernate.所以用 ...

  5. spring jdbcTemplate query

    1. spring jdbcTemplate query需要实现mapRow方法 package com.cdv.apolloagent.jdbc.dao.impl; import java.sql. ...

  6. Spring JdbcTemplate 的使用与学习&lpar;转&rpar;

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

  7. Spring JdbcTemplate的queryForList&lpar;String sql &comma; Class&lt&semi;T&gt&semi; elementType&rpar;易错使用--转载

    原文地址: http://blog.csdn.net/will_awoke/article/details/12617383 一直用ORM,今天用JdbcTemplate再次抑郁了一次. 首先看下这个 ...

  8. spring jdbcTemplate源码剖析

    本文浅析 spring jdbcTemplate 源码,主要是学习其设计精髓.模板模式.巧妙的回调 一.jdbcTemplate 类结构 ①.JdbcOperations : 接口定义了方法,如 &l ...

  9. 使用Spring JDBCTemplate简化JDBC的操作

    使用Spring JDBCTemplate简化JDBC的操作 接触过JAVA WEB开发的朋友肯定都知道Hibernate框架,虽然不否定它的强大之处,但个人对它一直无感,总感觉不够灵活,太过臃肿了. ...

随机推荐

  1. Java简单示例-用户登录、单个页面的增删改查及简单分页

    index.html  -登录->stulist.jsp (index.html传递到LoginServlet,进行登录检测及写入session,NO返回index.html界面,OK 跳转到s ...

  2. WinForm应用只运行一次

    一.WinForm应用只能运行一次 static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [ST ...

  3. js中的offsetWidth岁的BUG

    ---恢复内容开始--- 在js使用offsetWidth来操作控件的运动是会遇到: var oDiv = document.getElementById('div1') oDiv.style.wid ...

  4. ubuntu 搭建Erlang开发环境

    首先,打好库: sudo apt-get install build-essential sudo apt-get install libncurses5-dev sudo apt-get insta ...

  5. Struts2接收参数的几种方式

    一.用Action属性 在action里定义要接收的参数,并提供相应的set和get方法. 如: public class LoginAction extends ActionSupport { pr ...

  6. Vocabulary & Phrase

    Vocabulary A ANSI    美国国家标准学会,American National Standards Institute的缩写 a couple of    [口语]少数的,几个 a s ...

  7. Loadrunner 11在win7录制中失败&OpenCurlyDoubleQuote;the recording of the application was terminated by windows due to window data execution prevention feature&quot&semi;

    在录制过程中遇到如下问题: 查了下如何Enable, Disable Data Execution Prevention (DEP) in Windows 10/8/7 (http://www.the ...

  8. Storm并发机制详解

    本文可作为 <<Storm-分布式实时计算模式>>一书1.4节的读书笔记 在Storm中,一个task就可以理解为在集群中某个节点上运行的一个spout或者bolt实例. 记住 ...

  9. DockerToolbox在Win7上的安装和设置

    为什么使用Docker Toolbox Docker在Windows上使用有两种方式,一是利用VirtualBox建立linux虚拟机,在linux虚拟机中安装docker服务端和客户端,二是利用Wi ...

  10. python&lowbar;format格式化输出、while else、逻辑运算符、编码初识

    1.格式化输出 .%d  %s  格式化输出:% 占位符,d 表示替换整型数,s表示要替换字符串. name = input('请输入名字:') age = input('请输入年龄:') sex = ...