一、配置说明
1.注解负责标记IoC的类和进行属性装配
2.xml文件依然需要,需要通过<context:component-scan标签指定注解范围
3.标记IoC注解:@Component,@Service,@Controller,@Repository
4.标记DI注解:@Autowired @Qualifier @Resource @Value
5.IoC具体容器实现选择ClassPathXmlApplicationContext对象
二、配置流程
1.准备数据库测试数据
/*创建表*/
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;
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.准备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 + '\'' +
'}';
}
}
4.1.编写UserDao接口
package com.suchuanlin.dao;
import com.suchuanlin.pojo.User;
import java.util.List;
public interface UserDao {
List<User> selectAll();
}
4.2.编写UserDaoImpl实现UserDao接口
package com.suchuanlin.dao.impl;
import com.suchuanlin.dao.UserDao;
import com.suchuanlin.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<User> selectAll() {
String sql = "select * from user";
List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
return userList;
}
}
5.1编写UserService接口
package com.suchuanlin.service;
import com.suchuanlin.pojo.User;
import java.util.List;
public interface UserService {
List<User> selectAll();
}
5.2编写UserServiceImpl实现UserService接口
package com.suchuanlin.service.impl;
import com.suchuanlin.dao.UserDao;
import com.suchuanlin.pojo.User;
import com.suchuanlin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> selectAll() {
List<User> userList = userDao.selectAll();
return userList;
}
}
6.1.编写UserController类
package com.suchuanlin.controller;
import com.suchuanlin.pojo.User;
import com.suchuanlin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
public void selectAll(){
List<User> userList = userService.selectAll();
userList.forEach(user -> System.out.println(user));
}
}
7.编写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">
<!--配置自定义类注解扫描范围-->
<context:component-scan base-package="com.suchuanlin"/>
<!--引入jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--第三方类使用bean标签配置-->
<!--配置dataSource-->
<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>
</beans>
8.编写测试类测试程序
package com.suchuanlin.test;
import com.suchuanlin.controller.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringIocXmlAnnotationTest {
@Test
public void testXmlAnnotation(){
//1.获取ioc容器
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//2.获取组件对象
UserController userController = applicationContext.getBean(UserController.class);
//3.使用组件对象
userController.selectAll();
//4.关闭容器
applicationContext.close();
}
}
9.输出结果
D:\Programs\Java\jdk-17\bin\java.exe...
10月 12, 2023 5:03:28 下午 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 5:03:29 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} closing ...
10月 12, 2023 5:03:29 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} closed
Process finished with exit code 0