一、配置说明
1、所有内容写到xml格式配置文件中
2、声明bean通过<bean标签
3、<bean标签包含基本信息(id,class)和属性信息 <property name value / ref
4、引入外部的properties文件可以通过<context:property-placeholder
5.IoC具体容器实现选择ClassPathXmlApplicationContext对象
二、配置流程
1.准备项目框架
Spring-ioc-Annotation
|---src
|---|---main
|---|---|---java
|---|---|---|---com.suchuanlin
|---|---|---|---|---config
|---|---|---|---|---controller
|---|---|---|---|---dao
|---|---|---|---|---pojo
|---|---|---|---|---service
|---|---|---resources
|---|---|---|---jdbc.properties
|---|---|---|---spring-config.xml
|---|---test
|---|---|---java
|---|---|---|---com.suchuanlin.test
|---|---|---|---|---SpringIcoCXmlTest
2.准备jdbc.properties配置文件
suchuanlin.url=jdbc:mysql://localhost:3306/db_02
suchuanlin.driver=com.mysql.cj.jdbc.Driver
suchuanlin.username=root
suchuanlin.password=1234
3.准备数据库测试数据
/*创建表*/
create table user(
id int primary key auto_increment,
phone varchar(20),
username varchar(50),
password varchar(50),
gender char,
email varchar(50)
);
/*插入数据*/
insert into user (id, phone, username, password, gender, email)
values (null, '18800001111', '张三', '123456', '男', '18800001111@163.com'),
(null, '18800001112', '李四', '123456', '女', '18800001112@163.com'),
(null, '18800001113', '王五', '123456', '男', '18800001113@163.com'),
(null, '18800001114', '赵六', '123456', '女', '18800001114@163.com'),
(null, '18800001115', '田七', '123456', '男', '18800001115@163.com'),
(null, '18800001116', '陈八', '123456', '女', '18800001116@163.com'),
(null, '18800001117', '朱九', '123456', '男', '18800001117@163.com'),
(null, '18800001118', '郑十', '123456', '女', '18800001118@163.com');
/*查询表*/
select * from user;
4.准备pojo.User类
package com.suchuanlin.pojo;
public class User {
private Integer id;
private String phone;
private String username;
private String password;
private String gender;
private String email;
public User() {
}
public User(Integer id, String phone, String username, String password, String gender, String email) {
this.id = id;
this.phone = phone;
this.username = username;
this.password = password;
this.gender = gender;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", phone='" + phone + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
}
5.1.编写UserDao接口
package com.suchuanlin.dao;
import com.suchuanlin.pojo.User;
import java.util.List;
public interface UserDao {
List<User> selectAll();
}
5.2.编写UserDaoImpl类实现UserDao接口
package com.suchuanlin.dao.impl;
import com.suchuanlin.dao.UserDao;
import com.suchuanlin.pojo.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<User> selectAll() {
String sql = "select * from user";
List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
return userList;
}
}
6.1.编写UserService接口
package com.suchuanlin.service;
import com.suchuanlin.pojo.User;
import java.util.List;
public interface UserService {
List<User> selectAll();
}
6.2.编写UserServiceImpl类实现UserService接口
package com.suchuanlin.service.impl;
import com.suchuanlin.dao.UserDao;
import com.suchuanlin.pojo.User;
import com.suchuanlin.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<User> selectAll() {
List<User> userList = userDao.selectAll();
return userList;
}
}
7.编写UserController
package com.suchuanlin.controller;
import com.suchuanlin.pojo.User;
import com.suchuanlin.service.UserService;
import java.util.List;
public class UserController {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public void selectAll(){
List<User> userList = userService.selectAll();
for (User user : userList) {
System.out.println(user);
}
}
}
8.编写spring-config.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: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/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${suchuanlin.driver}"/>
<property name="url" value="${suchuanlin.url}"/>
<property name="username" value="${suchuanlin.username}"/>
<property name="password" value="${suchuanlin.password}"/>
</bean>
<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--dao配置配置jdbcTemplate-->
<bean id="userDao" class="com.suchuanlin.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--service配置dao-->
<bean id="userService" class="com.suchuanlin.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<!--controller配置service-->
<bean id="userController" class="com.suchuanlin.controller.UserController">
<property name="userService" ref="userService"/>
</bean>
</beans>
9.编写测试类测试程序
package com.suchuanlin.test;
import com.suchuanlin.controller.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringIoCXmlTest {
@Test
public void testIoCXml(){
//1.创建ioc容器
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//2.获取组件对象
UserController userController = applicationContext.getBean(UserController.class);
//3.使用组件对象
userController.selectAll();
//4.关闭容器
applicationContext.close();
}
}
10.输出结果
D:\Programs\Java\jdk-17\bin\java.exe...
10月 12, 2023 3:57:11 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
User{id=1, phone='18800001111', username='张三', password='123456', gender='男', email='18800001111@163.com'}
User{id=2, phone='18800001112', username='李四', password='123456', gender='女', email='18800001112@163.com'}
User{id=3, phone='18800001113', username='王五', password='123456', gender='男', email='18800001113@163.com'}
User{id=4, phone='18800001114', username='赵六', password='123456', gender='女', email='18800001114@163.com'}
User{id=5, phone='18800001115', username='田七', password='123456', gender='男', email='18800001115@163.com'}
User{id=6, phone='18800001116', username='陈八', password='123456', gender='女', email='18800001116@163.com'}
User{id=7, phone='18800001117', username='朱九', password='123456', gender='男', email='18800001117@163.com'}
User{id=8, phone='18800001118', username='郑十', password='123456', gender='女', email='18800001118@163.com'}
10月 12, 2023 3:57:12 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} closing ...
10月 12, 2023 3:57:12 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} closed
Process finished with exit code 0