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

 

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

 

 

 

 

阅读更多
个人分类: Spring
上一篇Spring 采用注解方式配置事务
下一篇Ibatis 1-准备环境
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭