zoukankan      html  css  js  c++  java
  • Spring的jdbc模板1

    Spring是EE开发的一站式框架,有EE开发的每一层解决方案。Spring对持久层也提供了解决方案:ORM模块和jdbc模块,ORM模块在整合其他框架的时候使用

    Spring提供了很多的模板用于简化开发:

    1.创建项目,引入jar包

    基本的六个开发包,数据库驱动包,Spring的jdbc模板的jar包,整合junit4测试的jar包,以及aop开发的jar包(在IOC的注解开发者,spring4.x版本要引入aop的jar包)

    2.创建数据库和表

    数据库test,表student

    3.使用jdbc模板保存数据

    package zcc.spring_jdbc.demo1;
    /*
     * Spring的jdbc模板的一个简单使用
     */
    
    import org.junit.Test;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    
    public class SpringDemo1 {
        
        @Test
        public void demo1() {
            //这样写每次都要创建连接池和jdbc模板,我们可以将连接池和模板交给spring管理
            //创建连接池
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3307/test");
            dataSource.setUsername("root");
            dataSource.setPassword("123456");
            //创建jdbc模板
            //JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            jdbcTemplate.setDataSource(dataSource);
            jdbcTemplate.update("insert into student values(?,?,?)", 31,"李二","男");
            
        }
    }

    可以发现,这种方式每次都创建连接池和jdbc模板,所以我们将连接池和jdbc模板交给spring管理,这样只需要new一次

    <?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:aop="http://www.springframework.org/schema/aop"
        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://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!-- 配置Spring内置连接池 ===-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <!-- 有set方法,属性注入 -->
           <property name="DriverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="Url" value="jdbc:mysql://localhost:3307/test"/>
           <property name="Username" value="root"/>
           <property name="Password" value="123456"/>
        </bean>
        <!-- 配置Spring的JDBC模版 -->
        <bean  id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
        </bean>
    </beans>

    测试类如下

    package com.ithheima.jdbc.demo1;
    
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext5.xml")
    public class JdbcDemo2 {
    @Resource(name
    ="jdbcTemplate") private JdbcTemplate jdbcTemplate;
    @Test
    public void demo2(){ jdbcTemplate.update("insert into student values(?,?,?)", 8, "周司", "男"); }
    }
  • 相关阅读:
    微信小程序------代码构成
    微信小程序------开发测试
    H5中 input消除默认,取消在手机上的点击高亮效果
    html5 WebSocket
    js和jquery如何获取图片真实的宽度和高度_javascript技巧
    如何解决Provisional headers are shown问题(转)
    JQuery怎样返回前一页
    tp5 通过IP获取对应ip的城市
    php 将富文本编辑后的内容转义为不带HTML标签的字符
    tp5 分页样式
  • 原文地址:https://www.cnblogs.com/zengcongcong/p/10401799.html
Copyright © 2011-2022 走看看