zoukankan      html  css  js  c++  java
  • dao使用JdbcTemplate(注入过程)视频学习

    四个文件,bean3.xml,Service.java,Dao.java,Test.java

    在Service中注入Dao对象,在Dao中注入JdbcTemplate对象,把dataSource传递到JdbcTemplate对象里面

    bean3.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
             ">
     
            
            <!-- 配置连接池 -->
           
        <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     
        <property name="driverClassName"  value="com.mysql.jdbc.Driver" />
     
         <property name="url" value="jdbc:mysql://localhost:3306/test" />
     
         <property name="username" value="root" />
     
         <property name="password" value="123" />
     
        </bean>
            
            <!--  创建service和dao对象,在service注入dao对象-->
            <bean id="service" class="com.spring.c3p0.Service">
            <!-- 在service注入Dao对象   name:属性名   ref:id值-->
                    <property name="Dao"  ref="dao"></property>
            </bean>
            
            <bean id="dao" class="com.spring.c3p0.Dao">
                        <!-- 在dao中注入jdbcTemplate对象   name:属性名   ref:id值-->
                    <property name="jdbcTemplate"  ref="jdbcTemplate"></property>
            
            </bean>
            
            <!-- 创建jdbcTemplate对象 -->
            <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!-- 把dataSource传递到模版对象里面  ref:配置的连接池id-->
            <property  name="dataSource"  ref="dataSource"></property>
            </bean>
    </beans>

    Service.java

    package com.spring.c3p0;
    
    public class Service {
                private Dao dao;
    
                public void setDao(Dao dao) {
                    this.dao = dao;
                }
                public void add(){
                    dao.add();
                }
    }

    Dao.java

    package com.spring.c3p0;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    
    public class Dao {
        //得到JdbcTemplate对象
            private JdbcTemplate jdbcTemplate;//属性
            //set方法注入
            public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
                this.jdbcTemplate = jdbcTemplate;
            }
    
            public void add(){
                System.out.println("add.....");
                String sql="insert into user_table values(?,?)";
                jdbcTemplate.update(sql, "lilei","123");
                
            }
    }

    Test.java

    package com.spring.c3p0;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        public static void main(String[] args) {
            ApplicationContext context= new ClassPathXmlApplicationContext("bean3.xml");
            Service service=(Service) context.getBean("service");
            service.add();
    
        }
    
    }
  • 相关阅读:
    运维岗春招--part2
    python 题库|刷题
    leetcode刷题
    运维面经汇总
    python自动化运维阅读笔记
    Python编程汇总
    old_boy 运维学习-第一期
    团队博客作业-Week3
    个人对final发布产品的排名
    各组对final发布产品的排名
  • 原文地址:https://www.cnblogs.com/liurg/p/7992380.html
Copyright © 2011-2022 走看看