2019独角兽企业重金招聘Python工程师标准>>>
概念
Spring中的jdbcTemplate的主要作用是实现数据的交互,下面我们就在dao层中如何使用jdbctemplate写测试案例
项目目录如下
基于xml实现jdbctemplate
这里我们使用的是JdbcDaoSupport这个类,主要是为了减少重复每次在dao层都要用的Jdbctemplate set方法
导入jar包
org.springframework
spring-context
4.3.6.RELEASE
org.springframework
spring-jdbc
5.1.6.RELEASE
org.springframework
spring-tx
5.1.6.RELEASE
mysql
mysql-connector-java
5.1.6
学生实体类
package com.cc.entity;
import java.io.Serializable;
/**
* 学生实体类
*/
public class Student implements Serializable {
private int id;
private String stuno;
private String name;
private int classid;
public int getId() {
return id;
}
public void setId(int id) { this.id = id; }
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", stuno='" + stuno + '\'' +
", name='" + name + '\'' +
", classid=" + classid +
'}';
}
}
方法接口
package com.cc.dao;
import com.cc.entity.Student;
/**
* 定义接口
*/
public interface studentDao {
/**
* 根据id查找学生
* @param
* @return
*/
Student findStudentbyId(Integer id);
/**
* 根据名称查找用户
*/
Student findStudentbyName(String name);
/**
* 更新账户
*/
void updateStudent(Student student);
}
方法实现类
package com.cc.dao;
import com.cc.entity.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
public class StudentDaoImpl extends JdbcDaoSupport implements studentDao {
@Override
public Student findStudentbyId(Integer id) {
List students =getJdbcTemplate().query("select * from student where id=?",new BeanPropertyRowMapper(Student.class),id);
return students.isEmpty()?null:students.get(0);
}
@Override
public Student findStudentbyName(String name) {
List students =getJdbcTemplate().query("select * from student where name=?",new BeanPropertyRowMapper(Student.class),name);
if(students.isEmpty()){
return null;
}
if(students.size()>1){
throw new RuntimeException("返回结果集不唯一");
}
return students.get(0);
}
@Override
public void updateStudent(Student student) {
getJdbcTemplate().update("update student set stuno=?,name=?,classid=? where id=?",
student.getStuno(),student.getName(),student.getClassid(),student.getId());
}
}
配置文件
测试类
import com.cc.dao.StudentDaoImpl;
import com.cc.entity.Student;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testjdbctemplate {
@Autowired
private StudentDaoImpl student;
@Test
public void testfindbyId(){
ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentDaoImpl student=ac.getBean("studentDao", StudentDaoImpl.class);
Student stu =student.findStudentbyId(1);
System.out.println(stu);
}
@Test
public void testfindbyname(){
ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentDaoImpl student=ac.getBean("studentDao", StudentDaoImpl.class);
Student stu=student.findStudentbyName("陈多多");
System.out.println(stu);
stu.setName("陈多糖");
student.updateStudent(stu);
}
}
测试结果如下