`

Spring对DAO的支持

阅读更多
创建数据库spring4

创建表

create table t_student(
    id int(11) primary key auto_increment,
    name varchar(20),
    age int(11)
)
insert into t_student values(1, "学生一", 11);
insert into t_student values(2, "学生二", 12);
insert into t_student values(3, "学生三", 13);
insert into t_student values(4, "学生四", 14);
insert into t_student values(5, "学生五", 15);


1. Spring对JDBC的支持

1.1) 配置数据源dbcp

1.2) 使用JdbcTemplate

新建项目Spring404

导入JdbcTemplate jar包,右键buildPath

spring-jdbc-4.0.6.RELEASE.jar
spring-tx-4.0.6.RELEASE.jar

导入dbcp jar包,右键buildPath

commons-dbcp-1.4.jar
commons-pool.jar

导入jdbc jar包,右键buildPath

mysql-connector-java-3.1.12-bin.jar

Student.java

package com.andrew.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;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", 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;
    }
}

StudentDao.java

package com.andrew.dao;
import java.util.List;
import com.andrew.model.Student;
public interface StudentDao {
    public int addStudent(Student student);
    public int updateStudent(Student student);
    public int deleteStudent(int id);
    public List<Student> findStudents();
}

StudentDaoImpl.java

package com.andrew.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.andrew.dao.StudentDao;
import com.andrew.model.Student;
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<Student> findStudents() {
        String sql = "select * from t_student";
        final List<Student> studentList = new ArrayList<Student>();
        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;
    }
}

StudentService.java

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

StudentServiceImpl.java

package com.andrew.service.impl;
import java.util.List;
import com.andrew.dao.StudentDao;
import com.andrew.model.Student;
import com.andrew.service.StudentService;
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<Student> findStudents() {
        return studentDao.findStudents();
    }
}

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring4
jdbc.username=root
jdbc.password=root

beans.xml

增加
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"

<?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.andrew.dao.impl.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean> 
    <bean id="studentService" class="com.andrew.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"></property>
    </bean> 
</beans>

JunitTest.java

package com.andrew.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.andrew.model.Student;
import com.andrew.service.StudentService;
public class JunitTest {
    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(6, "王五2", 2));
        if (updateNums == 1) {
            System.out.println("更新成功");
        }
    }
    @Test
    public void deleteStudent() {
        StudentService studentService = (StudentService) ac.getBean("studentService");
        int deleteNums = studentService.deleteStudent(6);
        if (deleteNums == 1) {
            System.out.println("删除成功");
        }
    }
    @Test
    public void findStudents() {
        StudentService studentService = (StudentService) ac.getBean("studentService");
        List<Student> studentList = studentService.findStudents();
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}

运行结果:
添加成功
更新成功
删除成功
Student [id=1, name=学生一, age=11]
Student [id=2, name=学生二, age=12]
Student [id=3, name=学生三, age=13]
Student [id=4, name=学生四, age=14]
Student [id=5, name=学生五, age=15]


1.3) JdbcDaoSupport的使用

继承JdbcDaoSupport,里面已经实现了jdbcTemplate

新建项目Spring404-02


Student.java

package com.andrew.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;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", 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;
    }
}

StudentDao.java

package com.andrew.dao;
import java.util.List;
import com.andrew.model.Student;
public interface StudentDao {
    public int addStudent(Student student);
    public int updateStudent(Student student);
    public int deleteStudent(int id);
    public List<Student> findStudents();
}

StudentDaoImpl.java(修改)

package com.andrew.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.andrew.dao.StudentDao;
import com.andrew.model.Student;
public class StudentDaoImpl extends JdbcDaoSupport implements StudentDao {
    @Override
    public int addStudent(Student student) {
        String sql = "insert into t_student values(null,?,?)";
        Object[] params = new Object[] { student.getName(), student.getAge() };
        return this.getJdbcTemplate().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 this.getJdbcTemplate().update(sql, params);
    }
    @Override
    public int deleteStudent(int id) {
        String sql = "delete from t_student where id=?";
        Object[] params = new Object[] { id };
        return this.getJdbcTemplate().update(sql, params);
    }
    @Override
    public List<Student> findStudents() {
        String sql = "select * from t_student";
        final List<Student> studentList = new ArrayList<Student>();
        this.getJdbcTemplate().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;
    }
}

StudentService.java

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

StudentServiceImpl.java

package com.andrew.service.impl;
import java.util.List;
import com.andrew.dao.StudentDao;
import com.andrew.model.Student;
import com.andrew.service.StudentService;
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<Student> findStudents() {
        return studentDao.findStudents();
    }
}

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring4
jdbc.username=root
jdbc.password=root

beans.xml(修改)

<?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="studentDao" class="com.andrew.dao.impl.StudentDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean> 
    <bean id="studentService" class="com.andrew.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"></property>
    </bean> 
</beans>

JunitTest.java(修改)

package com.andrew.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.andrew.model.Student;
import com.andrew.service.StudentService;
public class JunitTest {
    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(7, "王五2", 2));
        if (updateNums == 1) {
            System.out.println("更新成功");
        }
    }
    @Test
    public void deleteStudent() {
        StudentService studentService = (StudentService) ac.getBean("studentService");
        int deleteNums = studentService.deleteStudent(7);
        if (deleteNums == 1) {
            System.out.println("删除成功");
        }
    }
    @Test
    public void findStudents() {
        StudentService studentService = (StudentService) ac.getBean("studentService");
        List<Student> studentList = studentService.findStudents();
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}

运行结果:
添加成功
更新成功
删除成功
Student [id=1, name=学生一, age=11]
Student [id=2, name=学生二, age=12]
Student [id=3, name=学生三, age=13]
Student [id=4, name=学生四, age=14]
Student [id=5, name=学生五, age=15]


1.4) NamedParameterJdbcTemplate的使用;支持命名参数变量

org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate

新建项目Spring404-03


Student.java

package com.andrew.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;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", 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;
    }
}

StudentDao.java

package com.andrew.dao;
import java.util.List;
import com.andrew.model.Student;
public interface StudentDao {
    public int addStudent(Student student);
    public int updateStudent(Student student);
    public int deleteStudent(int id);
    public List<Student> findStudents();
}

StudentDaoImpl.java(修改)

package com.andrew.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import com.andrew.dao.StudentDao;
import com.andrew.model.Student;
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<Student> findStudents() {
        String sql = "select * from t_student";
        final List<Student> studentList = new ArrayList<Student>();
        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;
    }
}

StudentService.java

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

StudentServiceImpl.java

package com.andrew.service.impl;
import java.util.List;
import com.andrew.dao.StudentDao;
import com.andrew.model.Student;
import com.andrew.service.StudentService;
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<Student> findStudents() {
        return studentDao.findStudents();
    }
}

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring4
jdbc.username=root
jdbc.password=root

beans.xml(修改)

<?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="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"></constructor-arg>
    </bean>
    <bean id="studentDao" class="com.andrew.dao.impl.StudentDaoImpl">
        <property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
    </bean>
    <bean id="studentService" class="com.andrew.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao"></property>
    </bean>
</beans>

JunitTest.java(修改)

package com.andrew.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.andrew.model.Student;
import com.andrew.service.StudentService;
public class JunitTest {
    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<Student> studentList = studentService.findStudents();
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}

运行结果:
添加成功
更新成功
删除成功
Student [id=1, name=学生一, age=11]
Student [id=2, name=学生二, age=12]
Student [id=3, name=学生三, age=13]
Student [id=4, name=学生四, age=14]
Student [id=5, name=学生五, age=15]


2. Spring对Hibernate的支持
分享到:
评论

相关推荐

    Spring对DAO的支持.doc

    本文将从三个方面探讨Spring对DAO的支持:传统模式的DAO,Spring对JDBC的DAO支持,以及Spring对Hibernate的DAO支持。 ### 1. 传统DAO模式 在传统的DAO模式中,通常需要实现以下几个组件: - **DAO工厂**:创建DAO...

    spring几种Dao支持配置

    以下将详细阐述Spring对DAO支持的几种配置方式: 1. **JDBC DAO支持:** Spring通过`JdbcTemplate`和`SimpleJdbcInsert`等类提供了对JDBC的抽象,减少了直接使用JDBC代码的繁琐性。`JdbcTemplate`提供了一组模板...

    使用Spring实现DAO模式

    Spring框架作为Java开发中的一个重量级工具,提供了强大的支持来实现DAO模式,进而提升代码的可维护性和可扩展性。 ### Spring框架中的DAO模式实现 #### 1. 理解DAO模式 DAO模式的核心理念是将数据访问逻辑从业务...

    Spring的DAO

    Spring的DAO支持是其核心模块之一,用于简化数据访问层(DAO)的开发,通过将固定的数据访问逻辑和业务逻辑分离,提高了代码的可维护性和复用性。Spring的DAO设计模式主要体现在模板(Template)和回调(Callback)...

    dao.rar_spring-dao

    在"标签"中,"spring-dao"进一步确认了这个压缩包的核心内容,即Spring的DAO支持。Spring的DAO支持包括了JDBC模板、Hibernate和iBatis等ORM工具的集成,以及声明式和编程式事务管理。这些功能可以帮助开发者编写更...

    Spring支持DAO需要的jar包

    Spring提供了一套完整的DAO支持,包括事务管理、异常处理、数据源管理等,使得开发者可以更加专注于业务逻辑,而不是底层的数据访问细节。 1. **Spring JDBC支持**:Spring通过JDBC抽象层提供了模板类JdbcTemplate...

    spring 对dao 的操作

    标题提到的"spring 对dao 的操作",指的是Spring如何协助我们实现数据访问对象(DAO)层的编程。在这个基于SSH(Spring、Struts、Hibernate)的项目中,DAO层是连接业务逻辑与数据库的关键部分。 首先,我们需要...

    Spring DAO

    Spring框架提供了一套强大的数据访问抽象层——DAO(Data Access Object)支持,使得开发者能够在不关心具体数据库技术细节的情况下完成数据访问任务。这一机制极大地简化了开发过程,降低了代码的耦合度,并提高了...

    Spring Dao层设计

    Spring 数据库访问的支持。包括统一异常体系和Spring的Template+CallBack的解读

    spring-jdbc-dao

    在Java开发中,Spring框架以其强大的功能和易用性深受开发者喜爱,其中Spring JDBC模块提供了对数据库操作的支持。Spring JDBC DAO(Data Access Object)模式是Spring提供的一种数据库访问机制,旨在简化传统的JDBC...

    解决The type org.springframework.dao.support.DaoSupport cannot be resolved.bao报错

    这个错误通常意味着编译环境无法找到`DaoSupport`类,这是Spring DAO模块中的一个关键类,用于提供数据访问对象(DAO)的基本支持。`DaoSupport`是许多自定义DAO实现的基础,它提供了事务管理和其他便利功能。 首先...

    Struts2+hibernate+spring整合泛型DAO

    在Struts2+Hibernate+Spring的集成中,泛型DAO扮演着重要的角色,它使得DAO层对所有实体类的操作变得统一和规范。 首先,让我们详细了解一下Struts2。Struts2是基于拦截器的MVC框架,它提供了强大的动作映射、结果...

    利用JUnit和Spring-test对SpringJDBC组件DAO层测试

    本话题将深入讲解如何利用JUnit和Spring-test这两个工具对SpringJDBC组件的DAO层进行有效测试。 首先,JUnit是一个流行的Java单元测试框架,它允许开发者编写可重复运行的测试用例,以验证代码的正确性。在针对...

    spring boot服务器dao层实例化.rar_dao实例化_idea 实例ID_spring boot_spring b

    6. 使用IDEA:IDEA作为强大的Java IDE,提供了丰富的Spring Boot支持。我们可以使用它的代码补全、代码生成、调试等功能来加速开发。例如,IDEA可以通过"Generate"菜单快速生成DAO接口和实现,或者通过"Run/Debug ...

    SpringDao模式实例

    8. **Unit Testing**: Spring提供了一些工具,如`@TransactionalTest`注解和`MockMvc`,方便对DAO层进行单元测试,确保代码质量。 在实际应用中,Spring DAO模式的实现通常会结合使用上述组件,通过配置文件(XML或...

    spring对jdbc的支持jar包

    例如,Hibernate或MyBatis可以通过Spring的DAO支持进行集成,实现更高级的对象关系映射。 总的来说,Spring对JDBC的支持使得数据库操作更加高效、安全且易于维护。它提供了丰富的API和抽象,帮助开发者避免了繁琐的...

    Spring高版本对ibatis的支持

    最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 &lt;bean id="sqlMapClient" class="org.spring...

    Spring + Hibernate DAO 代码上传

    - **Spring JDBC和ORM支持**:Spring提供了JDBC抽象层,简化数据库操作,同时支持多种ORM框架,包括Hibernate。 2. **Hibernate ORM框架**: - Hibernate是一个流行的Java持久化框架,它简化了与关系数据库的交互...

    Hibernate泛型DAO(结合spring模板支持)

    阅读这份文档将进一步提升你对泛型DAO的理解和应用能力。 总之,Hibernate泛型DAO结合Spring模板,是Java开发中一种实用且高效的数据访问方式。它不仅减少了代码量,还提高了代码的可维护性,值得在实际项目中广泛...

    开源框架spring详解-----spring对JDBC的支持

    1. **Spring 对 DAO 的支持** Spring 的 DAO 支持是通过接口编程实现的,例如在给定的代码示例中,`AccountServiceImpl` 类实现了 `AccountService` 接口,并依赖于 `AccountDao` 接口。这种设计使得业务逻辑与具体...

Global site tag (gtag.js) - Google Analytics