【Spring+JDBC+DBCP】JdbcTemplate使用例子

[quote]

1. BaseDAO.java

public interface BaseDAO {
public void updatePerson(Person person);
public List<Map<String, Object>> searchPersons();
public Map<String, Object> searchPerson(Person person);
}

2. PersonDAO.java

import java.util.Map;
import javax.sql.DataSource;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import springinaction.base.BaseDAO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:section5/sec5.xml"})

public class PersonDAO implements BaseDAO{

@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
private static final String SEARCH_ALL = "select * from person";
private static final String SEARCH_BY_ID = "select * from person where id = ?";
private static final String UPDATEBYID = "update person set age = ? where id = ?";

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public void updatePerson(Person person) {
jdbcTemplate.update(UPDATEBYID, new Object[]{person.getAge(), person.getId()});
}

public List<Map<String, Object>> searchPersons() {
return jdbcTemplate.queryForList(SEARCH_ALL);
}

public Map<String, Object> searchPerson(Person person) {
return jdbcTemplate.queryForMap(SEARCH_BY_ID, person.getId());
}

}

3. PersonTest.java

import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import springinaction.base.BaseDAO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:section5/sec5.xml"})

public class PersonTest {

@Autowired
@Qualifier("person")
private Person person;

@Autowired
@Qualifier("personDAO")
private BaseDAO personDAO;

List<Map<String, Object>> resultList = null;
Map<String, Object> resultmap = null;

@Test
public void updatePersonTest() {
person.setId(2);
person.setAge(3);
personDAO.updatePerson(person);

resultmap = personDAO.searchPerson(person);
System.out.println(resultmap);
}

@Test
public void searchPersonsTest() {
resultList = personDAO.searchPersons();
System.out.println(resultList);
}
}

4.sec5.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="root" />
<property name="password" value="1111" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="person" class="springinaction.section5.Person"/>

<bean id="personDAO" class="springinaction.section5.PersonDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>


[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值