spring-ioc三层架构测试
src/main/java/com.atguigu.pojo/Student.java类
package com.atguigu.pojo;
public class Student {
private Integer id;
private String name;
private String pwd;
private String plone;
private String address;
public Integer getId() { return id; }
public String getPwd() { return pwd; }
public void setPwd(String pwd) { this.pwd = pwd; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getPlone() { return plone; }
public void setPlone(String plone) { this.plone = plone; }
public String getAddress() { return address; }
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" + "id=" + id + ", name='" + name + '\'' + ", phone=" + plone + ", address='" + address + '\'' + '}';
}
}
src/main/resources/spring-02.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:comtext="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/context https://www.springframework.org/schema/context/spring-context.xsd">
引入外部配置文件
<comtext:property-placeholder location="classpath:jdbc.properties"/>
druid
类型:com.alibaba.druid.pool.DruidDataSource,表示使用阿里巴巴的Druid连接池作为数据源。
属性:url、driverClassName、username、password,分别表示数据库连接的URL、驱动类、用户名和密码。
作用:用于创建数据库连接池,管理数据库连接的获取和释放,提高数据库操作的性能和效率。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${atguitu.url}"/>
<property name="driverClassName" value="${atguigu.driver}"/>
<property name="username" value="${atguigu.username}"/>
<property name="password" value="${atguigu.password}"/>
</bean>
jdbcTemplate
类型:org.springframework.jdbc.core.JdbcTemplate,是Spring框架提供的用于简化数据库操作的工具类。
属性:dataSource,引用了上面定义的dataSource bean,表示将dataSource注入到jdbcTemplate中,使jdbcTemplate能够使用dataSource管理数据库连接。
作用:简化了数据库操作,提供了一系列执行SQL语句、查询结果映射等功能,避免了手动编写繁琐的JDBC代码,提高了开发效率。
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
dao 配置 jdbcTemplate
<bean id="studentDao" class="com.atguigu.dao.impl.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
service 配置 dao
<bean id="studentService" class="com.atguigu.service.Impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
controller 配置 service
<bean id="studentController" class="com.atguigu.controller.StudentController">
<property name="studentService" ref="studentService"></property>
</bean>
</beans>
src/main/java/com.atguigu.dao/StudentDao接口
package com.atguigu.dao;
import com.atguigu.pojo.Student;
import java.util.List;
public interface StudentDao {
// 数据库查询全部数据
List<Student> queryAll();
}
src/main/java/com.atguigu.dao/impl/StudentDaoImpl.java
package com.atguigu.dao.impl;
import com.atguigu.dao.StudentDao;
import com.atguigu.pojo.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Student> queryAll(){
String sql = "select id, name, pwd, plone, address from my_user";
List<Student> stuList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
System.out.println("stuList= " + stuList);
return stuList;
}
}
src/main/java/com.atguigu.service/StudentService接口
package com.atguigu.service;
import com.atguigu.pojo.Student;
import java.util.List;
public interface StudentService {
// 查询所有用户数据业务
List<Student> findAll();
}
src/main/java/com.atguigu.service/impl/StudentServiceImpl.java
package com.atguigu.service.Impl;
import com.atguigu.dao.StudentDao;
import com.atguigu.pojo.Student;
import com.atguigu.service.StudentService;
import java.util.List;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public List<Student> findAll() {
List<Student> stuList = studentDao.queryAll();
System.out.println("studentSerice= " + stuList);
return stuList;
}
}
src/main/java/com.atguigu.controller/StudentController
package com.atguigu.controller;
import com.atguigu.pojo.Student;
import com.atguigu.service.Impl.StudentServiceImpl;
import com.atguigu.service.StudentService;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class StudentController {
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void findAll(){
List<Student> all = studentService.findAll();
System.out.println("最终用户数据= " + all);
}
}
测试方法如下
@Test
public void testQueryAll(){
// 1、创建Ioc容器
ClassPathXmlApplicationContext applicationContext
= new ClassPathXmlApplicationContext("spring-02.xml");
// 2、获取组件对象
StudentController controller = applicationContext.getBean(StudentController.class);
// 3、使用组件对象
controller.findAll();
// 4、关闭容器
applicationContext.close();
}
原文地址:https://blog.csdn.net/pig_ning/article/details/137456977
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!