spring:一个简单实例之对DAO的支持
来源:互联网 发布:待业在家知乎 编辑:程序博客网 时间:2024/05/22 09:43
spring 对 DAO 的支持~~
1、在 MySQL 中创建 db_spring 数据库,新建 t_student 表
DROP TABLE IF EXISTS `t_student`;CREATE TABLE `t_student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;insert into `t_student`(`id`,`name`,`age`) values (1,'张三',12),(2,'李四',12),(3,'王五',12),(4,'赵六',12),(5,'孙器',12),(6,'阙八',12);
2、创建 spring 项目
3、引入 jar 包
4、新建 student 实体类
package com.java.model;public class Student {private int id;private String name;private int age;public Student() {super();}public Student(String name, int age) {super();this.name = name;this.age = 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 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 + "]";}}
5、创建 studentDao 和其实现类 studentDaoImpl
package com.java.dao;import java.util.List;import com.java.model.Student;public interface StudentDao {public int addStudent(Student student);public int updateStudent(Student student);public int deleteStudent(int id);public List<Student> findStudents();}
package com.java.dao.impl;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.java.dao.StudentDao;import com.java.model.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 t_student values(null,?,?)";Object []params=new Object[]{student.getName(),student.getAge()};return jdbcTemplate.update(sql,params);}@Overridepublic 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);}@Overridepublic int deleteStudent(int id) {String sql="delete from t_student where id=?";Object []params=new Object[]{id};return jdbcTemplate.update(sql,params);}@Overridepublic List<Student> findStudents() {String sql="select * from t_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;}}
6、加入配置文件
<?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.java.dao.impl.StudentDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean> <bean id="studentService" class="com.java.service.impl.StudentServiceImpl"><property name="studentDao" ref="studentDao"></property></bean> </beans>
7、jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/db_springjdbc.username=rootjdbc.password=root
8、service 层
package com.java.service;import java.util.List;import com.java.model.Student;public interface StudentService {public int addStudent(Student student);public int updateStudent(Student student);public int deleteStudent(int id);public List<Student> findStudents();}package com.java.service.impl;import java.util.List;import com.java.dao.StudentDao;import com.java.model.Student;import com.java.service.StudentService;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 updateStudent(Student student) {return studentDao.updateStudent(student);}@Overridepublic int deleteStudent(int id) {return studentDao.deleteStudent(id);}@Overridepublic List<Student> findStudents() {return studentDao.findStudents();}}
8、测试类
package com.java.test;import java.util.List;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.java.model.Student;import com.java.service.StudentService;public class Test {private ApplicationContext ac;@Beforepublic void setUp() throws Exception {ac=new ClassPathXmlApplicationContext("beans.xml");}@Testpublic void addStudent() {StudentService studentService=(StudentService)ac.getBean("studentService");int addNums=studentService.addStudent(new Student("王五", 1));if(addNums==1){System.out.println("添加成功");}}@Testpublic void updateStudent() {StudentService studentService=(StudentService)ac.getBean("studentService");int updateNums=studentService.updateStudent(new Student(8,"王五2", 2));if(updateNums==1){System.out.println("更新成功");}}@Testpublic void deleteStudent() {StudentService studentService=(StudentService)ac.getBean("studentService");int deleteNums=studentService.deleteStudent(8);if(deleteNums==1){System.out.println("删除成功");}}@Testpublic void findStudents() {StudentService studentService=(StudentService)ac.getBean("studentService");List<Student> studentList=studentService.findStudents();for(Student student:studentList){System.out.println(student);}}}
到此结束啦~~
阅读全文
0 0
- spring:一个简单实例之对DAO的支持
- spring:一个简单实例之对事务的支持
- spring对DAO的支持
- spring对DAO的支持
- spring对DAO的支持
- spring对Dao的支持
- 小曹学spring--Spring对DAO的支持
- Spring JDBC-Spring对DAO的支持
- Spring对JDBC的DAO支持
- 【转】 Spring对DAO的支持
- 8 Spring对DAO的支持
- 第十章 Spring对DAO的支持
- 什么是Spring的DAO,Spring对JDBC/Hibernate的支持
- Spring学习5:Spring对DAO的支持
- Spring学习笔记(七)Spring对Dao的支持
- 实例简述Spring AOP之对AspectJ语法的支持
- 实例简述Spring AOP之对AspectJ语法的支持
- 实例简述Spring AOP之对AspectJ语法的支持
- springboot学习笔记3(全局错误页面配置)
- Linux最常用的命令
- 使用Spring boot 创建web工程
- 月是故乡明
- Error retrieving parent for item: No resource found that matches the given name 'android:TextAppeara
- spring:一个简单实例之对DAO的支持
- 写在创业五周年
- 项目1-残脑小结(1)
- REST是什么
- 【bzoj5018】[Snoi2017]英雄联盟
- PAT甲级题解目录
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (上)
- 写代码应该练成的代码格式
- BZOJ 4530 [Bjoi2014]大融合 [LCT]