1.基本步骤
导入jar包 log4j 资源
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
2.创建dao层 service层 进行资源环境搭建
创建实体类User entity
package com.liuboss.entity;
import lombok.Data;
@Data
public class User {
private int id;
private String username;
private String password;
}
dao层
package com.liuboss.Dao;
public interface UserDao {
public void save();
}
dao接口实现层
package com.liuboss.Dao.Impl;
import com.liuboss.Dao.UserDao;
import com.liuboss.entity.User;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@Data
public class UserDaoImpl implements UserDao {
//实现5种资源的注入
private int id;
private String username;
private String password;
private List<String> list;
private Map<String,User> map;
private Properties properties;
public void save() {
System.out.println("Dao....资源保存中....");
}
}
service层
接口层
package com.liuboss.Service;
public interface UserService {
public void save();
}
service接口实现层
package com.liuboss.Service.Impl;
import com.liuboss.Dao.UserDao;
import com.liuboss.Service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("UserService...资源保存中...");
userDao.save();
}
}
resource文件下的资源配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.liuboss.Service.Impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userDao" class="com.liuboss.Dao.Impl.UserDaoImpl">
<property name="id" value="1"/>
<property name="username" value="永乐大帝"/>
<property name="password" value="123456"/>
<property name="list">
<list>
<value>李渊</value>
<value>武则天</value>
<value>李世民</value>
</list>
</property>
<property name="map">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u1" value-ref="user2"></entry>
<entry key="u1" value-ref="user3"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="1">洪武大帝</prop>
<prop key="2">永乐大帝</prop>
</props>
</property>
</bean>
<bean id="user1" class="com.liuboss.entity.User">
<property name="id" value="00001"/>
<property name="username" value="朱元璋"/>
<property name="password" value="123456"/>
</bean>
<bean id="user2" class="com.liuboss.entity.User">
<property name="id" value="00002"/>
<property name="username" value="朱棣"/>
<property name="password" value="123456"/>
</bean>
<bean id="user3" class="com.liuboss.entity.User">
<property name="id" value="00003"/>
<property name="username" value="朱高志"/>
<property name="password" value="123456"/>
</bean>
</beans>
重点---
注意 map集合注入方式 其中对应的实体类 为User实体类
<property name="map">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u1" value-ref="user2"></entry>
<entry key="u1" value-ref="user3"></entry>
</map>
</property>
<bean id="user1" class="com.liuboss.entity.User">
<property name="id" value="00001"/>
<property name="username" value="朱元璋"/>
<property name="password" value="123456"/>
</bean>
<bean id="user2" class="com.liuboss.entity.User">
<property name="id" value="00002"/>
<property name="username" value="朱棣"/>
<property name="password" value="123456"/>
</bean>
<bean id="user3" class="com.liuboss.entity.User">
<property name="id" value="00003"/>
<property name="username" value="朱高志"/>
<property name="password" value="123456"/>
</bean>