JAVA 핵심 노트 | [xxx] Spring 의 JdbcTemplate

6131 단어
JdbcTemplate
사용법
App.java
import com.mj.dao.JdbcTempDao;
import com.mj.dao.UserDao;
import com.mj.model.ModelUser;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.jws.WebParam;
import java.io.FileNotFoundException;

public class App {



    public static void main(String args[]) throws FileNotFoundException{
        /*
        //ClassPathXmlApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/Bean.xml");
        BeanSay sa = (BeanSay) context.getBean("BeanSay");
        sa.setMsg("__Spring");
        sa.talk();

        //FileSystemXmlApplicationContext
        ApplicationContext fileContext = new FileSystemXmlApplicationContext("/src/com/mj/xml/Bean.xml");
        BeanSay sa1 = (BeanSay) fileContext.getBean("BeanSay");
        sa1.setMsg("=Spring====");
        sa1.talk();

        ApplicationContext animalContext = new ClassPathXmlApplicationContext("com/mj/xml/Bean.xml");
        BeanAnimal ani =  (BeanAnimal) animalContext.getBean("Animal");
        ani.setAnimalName("dog");
        ani.showAnimal();

        BeanAnimal animal =  (BeanAnimal) animalContext.getBean("Animal");
        animal.setAnimalName("pig");
        animal.showAnimal();

        // init method   destroy method
        BeanLife life = (BeanLife) context.getBean("BeanLife");
        life.showName();

        //BeanDog     BeanAnimal
        BeanDog dog = (BeanDog)context.getBean("BeanDog");
        dog.showAnimal();
        //       
        BeanUser user = (BeanUser)context.getBean("BeanUser");
        user.showUser();
        */


        /*
        ApplicationContext annotationCtx = new AnnotationConfigApplicationContext(BeanConfig.class);
        IBean beanPerson = (IBean) annotationCtx.getBean("beanPerson");
        beanPerson.show();

        IBean beanStu = (IBean)annotationCtx.getBean("beanStu");
        beanStu.show();

        IBean beanTch = (IBean)annotationCtx.getBean("beanTch");
        beanTch.show();
        */

        /*
        // Setter   
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/BeanDL.xml");
        BeanShowHelper hps = (BeanShowHelper) context.getBean("BeanShowHelper");
        hps.show();

        //      
        BeanHideHelper bhh = (BeanHideHelper) context.getBean("BeanHideHelper");
        bhh.show();
        */

        // Jdbc
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/jdbc/BeanJdbc.xml");
        UserDao ud = (UserDao) context.getBean("UserDao");
        ModelUser modelU = new ModelUser();
        modelU.setName("mjjjjjj");
        ud.insert(modelU);

        ModelUser rsU = ud.findByID(4);
        System.out.println( rsU.getName());

        // JdbcTemplate
        ModelUser tempU = new ModelUser();
        tempU.setName("=JdbcTemplate=");
        JdbcTempDao jt = (JdbcTempDao)context.getBean("JdbcTempDao");
        jt.insert(tempU);



    }
}


JdbcTempDao.java
package com.mj.dao;

import com.mj.model.ModelUser;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

public class JdbcTempDao {

    private DataSource dataSource;

    private JdbcTemplate jdbctem = null;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public void insert(ModelUser u){
        jdbctem = new JdbcTemplate(dataSource);
        String sql = "INSERT INTO user (name) VALUES(?)";
        jdbctem.update(sql, new Object[]{u.getName()});


    }


}


ModelUser.java
package com.mj.model;

public class ModelUser {
    private int id;
    private  String name;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}


BeanJdbc.xml




    
    

        
        
        
        
    

    
        
    

    
    
        
    



운행 하 다.
[mysql:][db:Person]>select * from user;
+----+----------------+
| id | name           |
+----+----------------+
|  1 | jgg            |
|  2 | MJ             |
|  3 | mjjjjjj        |
|  4 | mjjjjjj        |
|  5 | mjjjjjj        |
|  6 | mjjjjjj        |
|  7 | mjjjjjj        |
|  8 | mjjjjjj        |
|  9 | mjjjjjj        |
| 10 | mjjjjjj        |
| 11 | mjjjjjj        |
| 12 | =JdbcTemplate= |
+----+----------------+
12 rows in set (0.00 sec)

좋은 웹페이지 즐겨찾기