Spring详解(六)

spring对jdbc的支持

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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/db_spring

jdbc.username=root

jdbc.password=123456

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowCallbackHandler;

public class StudentDaoImpl implements StudentDao{

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}

@Override

public int addStudent(Student student) {

String sql="insert into t_student values(null,?,?)";

Object []params=new Object[]{student.getName(),student.getAge()};

return jdbcTemplate.update(sql,params);

}

@Override

public int updateStudent(Student student) {

String sql="update t_student set name=?,age=? where id=?";

Object []params=new Object[]{student.getName(),student.getAge(),student.getId()};

return jdbcTemplate.update(sql,params);

}

@Override

public int deleteStudent(int id) {

String sql="delete from t_student where id=?";

Object []params=new Object[]{id};

return jdbcTemplate.update(sql,params);

}

@Override

public List findStudents() {

String sql="select * from t_student";

final List studentList=new ArrayList();

jdbcTemplate.query(sql, new RowCallbackHandler(){

@Override

public void processRow(ResultSet rs) throws SQLException {

Student student=new Student();

student.setId(rs.getInt("id"));

student.setName(rs.getString("name"));

student.setAge(rs.getInt("age"));

studentList.add(student);

}

});

return studentList;

}

}

import java.util.List;

public class StudentServiceImpl implements StudentService{

private StudentDao studentDao;

public void setStudentDao(StudentDao studentDao) {

this.studentDao = studentDao;

}

@Override

public int addStudent(Student student) {

return studentDao.addStudent(student);

}

@Override

public int updateStudent(Student student) {

return studentDao.updateStudent(student);

}

@Override

public int deleteStudent(int id) {

return studentDao.deleteStudent(id);

}

@Override

public List findStudents() {

return studentDao.findStudents();

}

import java.util.List;

import org.junit.Before;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class T {

private ApplicationContext ac;

@Before

public void setUp() throws Exception {

ac=new ClassPathXmlApplicationContext("beans.xml");

}

@Test

public void addStudent() {

StudentService studentService=(StudentService)ac.getBean("studentService");

int addNums=studentService.addStudent(new Student("王五", 1));

if(addNums==1){

System.out.println("添加成功");

}

}

@Test

public void updateStudent() {

StudentService studentService=(StudentService)ac.getBean("studentService");

int updateNums=

studentService.updateStudent(new Student(8,"王五2", 2));

if(updateNums==1){

System.out.println("更新成功");

}

}

@Test

public void deleteStudent() {

StudentService studentService=(StudentService)ac.getBean("studentService");

int deleteNums=studentService.deleteStudent(8);

if(deleteNums==1){

System.out.println("删除成功");

}

}

@Test

public void findStudents() {

StudentService studentService=(StudentService)ac.getBean("studentService");

List studentList=studentService.findStudents();

for(Student student:studentList){

System.out.println(student);

}

}

}


支持命名参数变量

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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowCallbackHandler;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

public class StudentDaoImpl implements StudentDao{

private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public void setNamedParameterJdbcTemplate(

NamedParameterJdbcTemplate namedParameterJdbcTemplate) {

this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;

}

@Override

public int addStudent(Student student) {

String sql="insert into t_student values(null,:name,:age)";

MapSqlParameterSource sps=new MapSqlParameterSource();

sps.addValue("name", student.getName());

sps.addValue("age", student.getAge());

return namedParameterJdbcTemplate.update(sql,sps);

}

@Override

public int updateStudent(Student student) {

String sql="update t_student set name=:name,age=:age where id=:id";

MapSqlParameterSource sps=new MapSqlParameterSource();

sps.addValue("name", student.getName());

sps.addValue("age", student.getAge());

sps.addValue("id", student.getId());

return namedParameterJdbcTemplate.update(sql,sps);

}

@Override

public int deleteStudent(int id) {

String sql="delete from t_student where id=:id";

MapSqlParameterSource sps=new MapSqlParameterSource();

sps.addValue("id", id);

return namedParameterJdbcTemplate.update(sql,sps);

}

@Override

public List findStudents() {

String sql="select * from t_student";

final List studentList=new ArrayList();

namedParameterJdbcTemplate.query(sql, new RowCallbackHandler(){

@Override

public void processRow(ResultSet rs) throws SQLException {

Student student=new Student();

student.setId(rs.getInt("id"));

student.setName(rs.getString("name"));

student.setAge(rs.getInt("age"));

studentList.add(student);

}

});

return studentList;

}

}

import java.util.List;

public class StudentServiceImpl implements StudentService{

private StudentDao studentDao;

public void setStudentDao(StudentDao studentDao) {

this.studentDao = studentDao;

}

@Override

public int addStudent(Student student) {

return studentDao.addStudent(student);

}

@Override

public int updateStudent(Student student) {

return studentDao.updateStudent(student);

}

@Override

public int deleteStudent(int id) {

return studentDao.deleteStudent(id);

}

@Override

public List findStudents() {

return studentDao.findStudents();

}

}

你可能感兴趣的:(Spring详解(六))