关闭

Spring+JDBC组合开发 - 使用JdbcTemplate进行insert/update/delete/query操作

标签: springjdbcintegerobjectinsertdelete
28317人阅读 评论(0) 收藏 举报
分类:

 

1. dao类

 

package com.jxhuang.transaction_xml.dao;

 

import java.util.List;

 

import javax.sql.DataSource;

 

import org.springframework.jdbc.core.JdbcTemplate;

 

import com.jxhuang.transaction_xml.bean.Person;

 

 

public class PersonDaoImpl implements PersonDao {

      JdbcTemplate template ;

 

      public void setDataSource (DataSource dataSource) {

            this . template = new JdbcTemplate(dataSource);

      }

 

      @Override

      public void delete(Integer personId) {

            // TODO Auto-generated method stub

            template .update( "delete from t_person where id=?" ,

                                    new Object[] { personId },

                                    new int [] { java.sql.Types. INTEGER });

      }

 

      @SuppressWarnings ( "unchecked" )

      @Override

      public List<Person> getAllPersons() {

            // TODO Auto-generated method stub

            return template .query( "select * from t_person" , new PersonMapper());

      }

 

      @Override

      public Person getPerson(Integer personId) {

            // TODO Auto-generated method stub

            return (Person) template .queryForObject(

                        "select * from t_person where id=?" , new Object[] { personId },                    new int [] { java.sql.Types. INTEGER }, new PersonMapper());

      }

 

      @Override

      public void insert(Person person) {

            // TODO Auto-generated method stub

            template .update( "insert into t_person(name) values(?)" ,

                        new Object[] { person.getName() },

                        new int [] { java.sql.Types. VARCHAR });

      }

 

      @Override

      public void update(Person person) {

            // TODO Auto-generated method stub

            template .update( "update t_person set name=? where id=?" , new Object[] {        person.getName(), person.getId() }, new int [] {

                        java.sql.Types. VARCHAR , java.sql.Types. INTEGER });

      }

 

}

 

 

2.dataSource的配置:

见:

 

http://blog.csdn.net/camper001/archive/2011/01/09/6125354.aspx

 

 

 

 

0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:226411次
    • 积分:3166
    • 等级:
    • 排名:第10872名
    • 原创:110篇
    • 转载:61篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论