Spring jdcbtemplate实例

来源:互联网 发布:写字机器人算法 编辑:程序博客网 时间:2024/06/09 21:32

javabean

package com.example.modle;public class Student {private int id;private String name;private int age;public Student(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public Student() {super();}public Student(String name, int age) {super();this.name = name;this.age = age;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}
Dao接口类

package com.example.Dao;import java.util.List;import com.example.modle.Student;public interface StudentDao {public int addStudent(Student student);public int deleteStudent(int id);public int updateStudent(Student student);public List<Student> findStudent();}
Dao实现类

package com.example.Dao;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 com.example.modle.Student;public class StudentDaoImpl implements StudentDao{private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic int addStudent(Student student) {String sql = "insert into student values(?,?,?)";Object []params = new Object[]{student.getId(),student.getName(),student.getAge()};return jdbcTemplate.update(sql, params);}@Overridepublic int deleteStudent(int id) {String sql = "delete from student where id=?";Object []params = new Object[]{id};return jdbcTemplate.update(sql, params);}@Overridepublic int updateStudent(Student student) {String sql = "update student set name=?,age=? where id=?";Object []params = new Object[]{student.getName(),student.getAge(),student.getId()};return jdbcTemplate.update(sql, params);}@Overridepublic List<Student> findStudent() {String sql = "select * from student";final List<Student> studentList = new ArrayList<Student>();jdbcTemplate.query(sql, new RowCallbackHandler() {@Overridepublic 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;}}

service的接口类,由于这个例子服务与Dao一样,所以接口一样

package com.example.service;import java.util.List;import com.example.modle.Student;public interface StudentService {public int addStudent(Student student);public int deleteStudent(int id);public int updateStudent(Student student);public List<Student> findStudent();}

service实现类

package com.example.service;import java.util.List;import com.example.Dao.StudentDao;import com.example.modle.Student;public class StudentServiceImpl implements StudentService{private StudentDao studentDao;public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}@Overridepublic int addStudent(Student student) {return studentDao.addStudent(student);}@Overridepublic int deleteStudent(int id) {return studentDao.deleteStudent(id);}@Overridepublic int updateStudent(Student student) {return studentDao.updateStudent(student);}@Overridepublic List<Student> findStudent() {// TODO Auto-generated method stubreturn studentDao.findStudent();}}



bean
<?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.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">    <!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><context:property-placeholder location="jdbc.properties" /><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><bean id="studentDao" class="com.example.Dao.StudentDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="studentService" class="com.example.service.StudentServiceImpl"><property name="studentDao" ref="studentDao"></property></bean></beans>
测试
package com.example.test;import java.util.List;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.example.modle.Student;import com.example.service.StudentService;public class test {@Testpublic void addtest(){ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");StudentService studentService = (StudentService) ac.getBean("studentService");int i = studentService.addStudent(new Student(1,"张三", 20));if (i == 1) {System.out.println("添加成功");}}@Testpublic void updatetest(){ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");StudentService studentService = (StudentService) ac.getBean("studentService");int i = studentService.updateStudent(new Student(2,"张三2", 20));if (i == 1) {System.out.println("修改成功");}}@Testpublic void deletetest(){ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");StudentService studentService = (StudentService) ac.getBean("studentService");int i = studentService.deleteStudent(1);if (i == 1) {System.out.println("删除成功");}}@Testpublic void findalltest(){ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");StudentService studentService = (StudentService) ac.getBean("studentService");List<Student> s = studentService.findStudent();if (s != null) {System.out.println("查找成功");for (Student student : s) {System.out.println(student);}}}}
jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcljdbc.username=scottjdbc.password=Ss123457


0 0
原创粉丝点击