spring与jdbc

1、Dao

package com.dao;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.List;

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

import com.beans.Student;

public class StudentDaoImpl extends JdbcDaoSupport implements IStudentDao {

@Override
public void insertStudent(Student student) {
	String sql = "insert into student(id,name,age) values(?,?,?)";
	this.getJdbcTemplate().update(sql, student.getId(), student.getName(), student.getAge());
}

@Override
public void deleteById(Integer id) {
	String sql = "delete from student where id=?";
	this.getJdbcTemplate().update(sql, id);
}

@Override
public void updatetudent(Student student) {
	String sql = "update student set name=?,age=?,id=?";
	this.getJdbcTemplate().update(sql, student.getName(),student.getAge(),student.getId());
}

@Override
public List<String> selectAllStudentsNames() {
	String sql = "select name from student";
	List<String> result = this.getJdbcTemplate().queryForList(sql , String.class);
	return result;
}

@Override
public String selectStudentNameById(Integer id) {
	String sql = "select name from student where id=?";
	String result = this.getJdbcTemplate().queryForObject(sql, String.class, id);
	return result;
}

@Override
public List<Student> selectAllStudents() {
	
	String sql = "select id, name, age from student";
	List<Student> students = this.getJdbcTemplate().query(sql, new StudentRowMapper());
	return students;
}

@Override
public Student selectStudentById(Integer id) {
	/*
	 * 方法1
	 * String sql1 = "select name from student where id=?"; String name =
	 * this.getJdbcTemplate().queryForObject(sql1 , String.class, id); String sql2 =
	 * "select age from student where id=?"; Integer age =
	 * this.getJdbcTemplate().queryForObject(sql2 , Integer.class, id); Student
	 * student = new Student(id, name, age);
	 * 
	 */
	
	//方法2
	String sql = "select id, name, age from student where id=?";
	Student student = this.getJdbcTemplate().queryForObject(sql, new StudentRowMapper(), id);
	return student;
}

}

2、applicationContext.xml(配置方式1)

<?xml version="1.0" encoding="UTF-8"?>

<!-- 注册dataSource:spring内部连接池 -->
<!-- 
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
	<property name="username" value="duchao"/>
	<property name="password" value="123456"/>
</bean>
 -->
<!-- 注册dataSource:DBCP连接池 -->
<!-- 
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
	<property name="username" value="duchao"/>
	<property name="password" value="123456"/>
</bean>
 -->
<!-- 注册dataSource:c3p0连接池 -->
<!-- 
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.jdbc.Driver"/>
	<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"/>
	<property name="user" value="duchao"/>
	<property name="password" value="123456"/>
</bean>
 -->
<!-- 注册dataSource:c3p0连接池 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${jdbc.driver}"/>
	<property name="jdbcUrl" value="${jdbc.url}"/>
	<property name="user" value="${jdbc.user}"/>
	<property name="password" value="${jdbc.password}"/>
</bean>

<!-- 注册jdbc.properties :方式1-->
<!-- 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location" value="classpath:jdbc.properties"/>
</bean>
 -->
<!-- 注册jdbc.properties :方式2-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 注册jdbcTemplate -->
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="myDataSource"/>
</bean>

<!-- 注册dao -->
<bean id="myDao" class="com.dao.StudentDaoImpl">
	<property name="jdbcTemplate" ref="myJdbcTemplate"/>
</bean>

<!-- 注册service -->
<bean id="studentService" class="com.service.StudentServiceImpl">
	<property name="dao" ref="myDao"/>
</bean>
## 2、applicationContext.xml(配置方式2) ***方式2与方式1的区别:方式2直接把数据源注入到dao*** <?xml version="1.0" encoding="UTF-8"?>

<!-- 注册dataSource:spring内部连接池 -->
<!-- 
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
	<property name="username" value="duchao"/>
	<property name="password" value="123456"/>
</bean>
 -->
<!-- 注册dataSource:DBCP连接池 -->
<!-- 
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
	<property name="username" value="duchao"/>
	<property name="password" value="123456"/>
</bean>
 -->
<!-- 注册dataSource:c3p0连接池 -->
<!-- 
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.jdbc.Driver"/>
	<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"/>
	<property name="user" value="duchao"/>
	<property name="password" value="123456"/>
</bean>
 -->
<!-- 注册dataSource:c3p0连接池 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${jdbc.driver}"/>
	<property name="jdbcUrl" value="${jdbc.url}"/>
	<property name="user" value="${jdbc.user}"/>
	<property name="password" value="${jdbc.password}"/>
</bean>

<!-- 注册jdbc.properties :方式1-->
<!-- 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location" value="classpath:jdbc.properties"/>
</bean>
 -->
<!-- 注册jdbc.properties :方式2-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 注册jdbcTemplate -->
<!-- 
<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="myDataSource"/>
</bean>
 -->

***<!-- 注册dao -->
<bean id="myDao" class="com.dao.StudentDaoImpl">
	<property name="dataSource" ref="myDataSource"/>
</bean>***

<!-- 注册service -->
<bean id="studentService" class="com.service.StudentServiceImpl">
	<property name="dao" ref="myDao"/>
</bean>

3、class StudentRowMapper

package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

扫描二维码关注公众号,回复: 10713409 查看本文章

import org.springframework.jdbc.core.RowMapper;

import com.beans.Student;

public class StudentRowMapper implements RowMapper {

//rs代表每一行记录,当查询出总的结果集后,框架会自动遍历这个结果集,每一次的遍历的一行数据都会被存放到
//这个方法的rs参数中。也就是说,这里的rs代表的是每一行数据,并非所有查询结果。换个角度来说,只要能执行这个方法,
//就说明rs不会是空的
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
	Student student = new Student();
	student.setId(rs.getInt("id"));
	student.setName(rs.getString("name"));
	student.setAge(rs.getInt("age"));
	return student;
}

}

发布了46 篇原创文章 · 获赞 1 · 访问量 383

猜你喜欢

转载自blog.csdn.net/weixin_43925059/article/details/105113914