读书人

Spring之SimpleJdbcTemplate的运用

发布时间: 2012-09-13 09:51:53 作者: rapoo

Spring之SimpleJdbcTemplate的使用

1.数据库脚本

?

3.Dao接口

?

6.jdbc.properties

package org.monday.springjdbc;import java.util.ArrayList;import java.util.List;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {private ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");private UserDao userDao=(UserDao) ctx.getBean("userDao");@Testpublic void testInsert() {User user = new User(1,"zy_1");userDao.insert(user);}@Testpublic void testUpdate() {User user = new User(5,"zy_test");userDao.update(user);}@Testpublic void testDeleteUser() {User user =new User();user.setId(5);userDao.delete(user);}@Testpublic void testDeleteInt() {userDao.delete(4);}@Testpublic void testFindById() {User user=userDao.findById(1);System.out.println(user);}@Testpublic void testFindByName() {int id=userDao.findByName("zy_1");System.out.println(id);}@Testpublic void testFindAll() {List<User> list=userDao.findAll();System.out.println(list);}@Testpublic void testCount() {int count = userDao.count();System.out.println(count);}@Testpublic void testBatchInsert() {List<User> users = new ArrayList<User>();users.add(new User(2,"zy_2"));users.add(new User(3,"zy_3"));users.add(new User(4,"zy_4"));users.add(new User(5,"zy_5"));userDao.batchInsert(users);}}

?

?

附:自己写这篇文章的时候,用的是Spring2.5.而今天用了Spring3.1后发现,SimpleJdbcTemplate和SimpleJdbcDaoSupport都被标记为@Deprecated (过时了),后来一看源码,有这么一句话,就明白了。

?

@deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.JdbcTemplate} and
?{@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. The JdbcTemplate and? NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.

?

大意就是: 从Spring 3.1开始,JdbcTemplate和NamedParameterJdbcTemplate提供了SimpleJdbcTemplate的功能。

?

那既然这样的话,SimpleJdbcTemplate和SimpleJdbcDaoSupport被标记为过时,就明白了。

?

?

?

读书人网 >其他数据库

热点推荐