• 2021-4-17 周报博客


    个人博客

    1.学到的东西

    06-JdbcTemplate基本使用-spring产生模板对象代码实现(抽取jdbc.properties)(应用)

    将数据库的连接信息抽取到外部配置文件中,和spring的配置文件分离开,有利于后期维护

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test
    jdbc.username=root
    jdbc.password=root
    

    配置文件修改为:

    <?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"
           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
    ">
    
        <!--加载jdbc.properties-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <!--数据源对象-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <!--jdbc模板对象-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    </beans>
    

    07-JdbcTemplate基本使用-常用操作-更新操作(应用)

    package com.itheima.test;
    
    import com.itheima.domain.Account;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import java.util.List;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class JdbcTemplateCRUDTest {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
        
    	//修改更新
        @Test
        public void testUpdate(){
            jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
        }
    	//删除
        @Test
        public void testDelete(){
            jdbcTemplate.update("delete from account where name=?","tom");
        }
    
    }
    
    

    2.明日计划

    继续学习

    3.遇到的问题

  • 相关阅读:
    python 查看安装包列表
    Centos7 linux下通过源码安装redis以及使用
    python 安装pip
    python3.7.2 pip 出现locations that require TLS/SSL异常处理方法
    Xshell报错“The remote SSH server rejected X11 forwarding request.”
    ERROR: child process failed, exited with error number 100
    centos7 (ifconfig不能使用) -bash: ifconfig: command not found
    gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
    Centos7 安装python3.7.2
    Python安装常见问题:zipimport.ZipImportError: can't decompress data; zlib not available 解决办法
  • 原文地址:https://www.cnblogs.com/gongyunlong-blogs/p/14912115.html
Copyright © 2020-2023  润新知