spring 整合JDBC

spring 整合JDBC

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

使用Spring提供的三个JDBC模板类(JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate)操作数据库

一、JdbcTemplate是Spring中最主要的JDBC模板, 利用JDBC和简单的索引參数查询对数据库进行简单訪问

二、NamedParameterJdbcTemplate可以在查询的时候把值绑定到SQL里的命名參数,而不是索引參数

    NamedParameterJdbcTemplate内部包括了一个JdbcTemplate,所以JdbcTemplate能做的事情NamedParameterJdbcTemplate都能干;

    NamedParameterJdbcTemplate相对于JdbcTemplate主要添加�了參数能够命名的功能。

三、SimpleJdbcTemplate利用Java5的特性,比方自己主动装箱、通用和可变參数列表来简化JDBC模板的使用

    SimpleJdbcTemplate内部包括了一个NamedParameterJdbcTemplate;所以NamedParameterJdbcTemplate能做的事情SimpleJdbcTemplate都能干,

    SimpleJdbcTemplate相对于NamedParameterJdbcTemplate主要添加�了JDK5.0的泛型和可变长度參数支持。

1、使用JDBC Template

JdbcTemplate是一个辅助类,封装了JDBC的操作,直接使用JdbcTemplate非常easy~Template仅仅依赖于数据源。

本样例使用的DriverManagerDataSource,是一个“伪”数据源,仅仅是模拟了形式,并没事实现数据源的功能。

   DriverManagerDataSource ds = new DriverManagerDataSource();

   ds.setDriverClassName(“com.mysql.jdbc.Driver”);

   ds.setUrl(“jdbc:mysql://localhost:3306/sampledb”);

   ds.setUsername(“root”);

   ds.setPassword(“”);

  

   JdbcTemplate jdbc = new JdbcTemplate();

   jdbc.setDataSource(ds);

   String sql = “create table t_user1(user_id int primary key,user_name varchar(60))”;

   jdbc.execute(sql);

2、JdbcDaoSupport,&& 让Spring JDBC更“Spring”

假设在代码中重复声明JdbcTemplate,代码会被污染的非常严重,那和JDBC没差别了就~

所以Spring提供了JdbcDaoSupport,全部DAO继承这个类,就会自己主动获得JdbcTemplate(前提是注入DataSource)。

另外:Spring的xml配置能够非常好的用在这里,在xml中配置,基本流程例如以下:

(1)声明DataSrouce的Bean,这里用BasicDataSource,就是DBCP数据源(Tomcat用的那个)

(2)声明模板JdbcTemplate的Bean,并把DataSource注入之

(3)声明Dao,class为集成自JdbcDaoSupport的东西,并注入JdbcTemplate。

上述Bean生成过程能够用Spring IDE……相当的方便。别的不说了,Code。

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-2.5.xsd”>

<bean id=”dataSource”

   class=”org.apache.commons.dbcp.BasicDataSource” abstract=”false”

   lazy-init=”default” autowire=”default” dependency-check=”default”

   destroy-method=”close”>

   <property name=”driverClassName”>

    <value>com.mysql.jdbc.Driver</value>

   </property>

   <property name=”url”>

    <value>jdbc:mysql://localhost:3306/sampledb</value>

   </property>

   <property name=”username”>

    <value>root</value>

   </property>

   <property name=”password”>

    <null />

   </property>

</bean>

<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>

   <property name=”dataSource”>

    <ref bean=”dataSource” />

   </property>

</bean>

<bean id=”Dao” abstract=”true”>

   <property name=”jdbcTemplate”>

    <ref bean=”jdbcTemplate” />

   </property>

</bean>

<bean id=”forumDAO” class=”dao.jdbc.ForumDAO” parent=”jdbcTemplate”

   abstract=”false” lazy-init=”default” autowire=”default”

   dependency-check=”default”>

</bean></beans>

ForumDAO

package dao.jdbc;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class ForumDAO extends JdbcDaoSupport 

{

     public void initDb()

     {

        String sql = “create table t_user1(user_id int primary key,user_name varchar(60))”;

        getJdbcTemplate().execute(sql);

     }

}

Main

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class Main 

{

public static void main(String args[])

{

   DriverManagerDataSource ds = new DriverManagerDataSource();

   ds.setDriverClassName(“com.mysql.jdbc.Driver”);

   ds.setUrl(“jdbc:mysql://localhost:3306/sampledb”);

   ds.setUsername(“root”);

   ds.setPassword(“”);

  

   JdbcTemplate jdbc = new JdbcTemplate();

   jdbc.setDataSource(ds);

   String sql = “create table t_user1(user_id int primary key,user_name varchar(60))”;

   jdbc.execute(sql);

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118983.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 一个bug的生命周期是多久_bug严重程度

    一个bug的生命周期是多久_bug严重程度前言:基本定义:BUG从发现到这个bug关闭,是一个完整的生命周期。一:从具体状态上来讲状态有这几种:1:new-bug被第一次发现的时候,确认是一个问题,将bug进行记录。2:assigned-当这个bug被指派给某个开发时,bug的状态变为assigned(已指派)。3:open-在被指派的开发进行bug修复的时候,表示开发正在处理这个bug。4:fixed-当被指派的开发将该bug修复完成的时候,表示bug已修复完成。5:pendingreset-待测试,开发修.

    2022年10月21日
  • idea右键没有run选项,无法运行main方法及启动springboot工程

    idea右键没有run选项,无法运行main方法及启动springboot工程idea右键没有run选项,无法运行main方法及启动springboot工程在idea中有的时候会从外部导入项目,导入成功后打开某个类,需要运行里面的main方法.按如常操作单击右键发现没有run选项。解决办法:选中你java文件所在的package单击右键选中MarkDirectoryas,然后选中SourcesRoot。例如我下图中的Thread1.java中的mai’n方法无法运…

    2022年5月6日
  • 除了we tool还有哪些免费安全好用的微信群发软件?这两个软件比we tool好用!

    除了we tool还有哪些免费安全好用的微信群发软件?这两个软件比we tool好用!除了wetool还有哪些安全好用的微信群发软件?群发工具是社群运营使用频率最高的工具,无论是给群内推送消息,还是给个人推送消息。按键精灵:点击左侧链接下载按键精灵是一款模拟鼠标键盘动作的软件。通过制作脚本,可以让按键精灵代替您的双手,自动执行一系列鼠标键盘动作。按键精灵免费版简单易用,不需要任何编程知识就可以作出功能强大的脚本。只要您在电脑前用双手可以完成的动作,按键精灵都可以替您完成。按键精灵用途广泛,具有大量脚本资源。简单百宝箱:点击左侧链接下载简单百宝箱是一个绿色和安全的游戏

    2022年6月4日
  • 利用Regsvr32绕过Applocker的限制策略

    利用Regsvr32绕过Applocker的限制策略

    2022年4月2日
  • Ubuntu 18.04通过deb安装cuda 10.2

    Ubuntu 18.04通过deb安装cuda 10.2wgethttps://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pinsudomvcuda-ubuntu1804.pin/etc/apt/preferences.d/cuda-repository-pin-600wgethttps://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installer

    2022年5月9日
  • 物联网操作系统HelloX开发人员入门指南

    物联网操作系统HelloX开发人员入门指南

    2022年1月21日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号