Spring 学习(五) Spring与JDBC结合实现数据库的增删改查

By | 2014/08/26

实体类Person.java

[code]

package cn.ytu.aop.jdbc;

public class Person {

private String id;
private String name;

public String getId() {
return id;
}

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

public String getName() {
return name;
}

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

[/code]

数据库接口 PersonDao

[code]

package cn.ytu.aop.jdbc;

import java.util.List;

public interface PersonDao {
//插入数据
public void insertPerson(Person person);
//数据的修改
public void updatePerson(Person person);
//数据的删除
public void deletePerson(String id);
//根据id查询
public Person findPerson(String id);
//查询所有
public List<Person> listPerson();

}

[/code]

数据库实现 PersonDaoImpl.java

[code]

package cn.ytu.aop.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao {

@Override
public void insertPerson(Person person) {
String sql = "insert into person(id,name) values (?,?)";
Object params[] = { person.getId(), person.getName() };
this.getJdbcTemplate().update(sql, params);

}

@Override
public void updatePerson(Person person) {
String sql = "update set name = ? where id=?";
Object params[] = { person.getName(), person.getId() };
this.getJdbcTemplate().update(sql, params);
}

@Override
public void deletePerson(String id) {
String sql = "delete from person where id = ?";
Object params[] = { id };
this.getJdbcTemplate().update(sql, params);
}

@Override
public Person findPerson(String id) {

Person person = (Person) this.getJdbcTemplate().queryForObject(
"select * from person where id = ?", new Object[] { id },
new RowMapper() {

@Override
public Object mapRow(ResultSet rs, int rownum)
throws SQLException {
Person person = new Person();
person.setId(rs.getString("id"));
person.setName(rs.getString("name"));
return person;
}
});
return person;
}

@Override
public List<Person> listPerson() {
List<Person> list = this.getJdbcTemplate().query(
"select * from person", new RowMapper() {

@Override
public Object mapRow(ResultSet rs, int arg1)
throws SQLException {
Person person = new Person();
person.setId(rs.getString("id"));
person.setName(rs.getString("name"));
return person;
}
});
return list;
}
}

[/code]

配置文件 applicationContext.xml

[code]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!– 配置数据库的连接池 方式一
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property
name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username"
value="root" /> <property name="password" value="root" /> </bean>
–>
<!– 配置数据库连接池 方式二 –>
<bean id="datasource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:cn/ytu/aop/jdbc/jdbc.properties</value>
</property>
</bean>

<bean id="personDao" class="cn.ytu.aop.jdbc.PersonDaoImpl">
<property name="dataSource">
<ref bean="datasource2" />
</property>
</bean>

</beans>

[/code]

 

附:数据库配置信息jdbc.properties

driverClassName=com.mysql.jdbc.Driver

url=jdbc\:mysql\://localhost\:3306/test

username=root

password=root

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据