User类:
- package com.songzhishu.spring.bean;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- /**
- * @BelongsProject: Spring6
- * @BelongsPackage: com.songzhishu.spring.bean
- * @Author: 斗痘侠
- * @CreateTime: 2023-10-18 20:41
- * @Description: TODO
- * @Version: 1.0
- */
- @Component
- public class User {
- @Value("张三")
- private String name;
-
- @Override
- public String toString() {
- return "User{" +
- "name='" + name + '\'' +
- '}';
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public User() {
- }
-
- public User(String name) {
- this.name = name;
- }
- }
-
配置文件: 开启组件扫描
- "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 http://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.songzhishu.spring.bean"/>
-
- beans>
测试:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:spring.xml")
- public class TestJunit4 {
- @Autowired
- private User user;
- @Test
- public void test1(){
- System.out.println(user.getName());
- }
- }
测试:
- /*@ContextConfiguration("classpath:spring.xml")
- @ExtendWith(SpringExtension.class)*/
- //写法二
- @SpringJUnitConfig(locations = "classpath:spring.xml")
- public class TestJunit5 {
- @Autowired
- private User user;
- @Test
- public void test1(){
- System.out.println(user.getName());
- }
- }
第一步:准备数据库表
第二步:IDEA中创建一个模块,并引入依赖
第三步:基于三层架构实现,所以提前创建好所有的包
第四步:编写pojo
第五步:编写mapper接口
第六步:编写mapper配置文件

第七步:编写service接口和service接口实现类
第八步:编写jdbc.properties配置文件

第九步:编写mybatis-config.xml配置文件
- "1.0" encoding="UTF-8" ?>
- configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
-
- <settings>
-
- <setting name="mapUnderscoreToCamelCase" value="true"/>
-
- <setting name="lazyLoadingEnabled" value="true"/>
-
- <setting name="cacheEnabled" value="true"/>
-
- <setting name="logImpl" value="STDOUT_LOGGING"/>
- settings>
-
- configuration>
第十步:编写spring.xml配置文件
- "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"
- xmlns:tx="http://www.springframework.org/schema/tx"
- 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
- http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
- <context:component-scan base-package="com.songzhishu.spring6"/>
-
-
- <context:property-placeholder location="jdbc.properties"/>
-
-
- <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driver}">property>
- <property name="url" value="${jdbc.url}">property>
- <property name="username" value="${jdbc.username}">property>
- <property name="password" value="${jdbc.password}">property>
- bean>
-
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="dataSource" ref="druidDataSource">property>
-
- <property name="configLocation" value="mybatis-config.xml">property>
-
- <property name="typeAliasesPackage" value="com.songzhishu.spring6.pojo">property>
- bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
- <property name="basePackage" value="com.songzhishu.spring6.mapper">property>
- bean>
-
-
-
- <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="druidDataSource">property>
- bean>
-
-
- <tx:annotation-driven transaction-manager="dataSourceTransactionManager">tx:annotation-driven>
-
- beans>
第十一步:编写测试程序,并添加事务,进行测试
Java的标准java.net.URL类和各种URL前缀的标准处理程序无法满足所有对low-level资源的访问,比如:没有标准化的 URL 实现可用于访问需要从类路径或相对于 ServletContext 获取的资源。并且缺少某些Spring所需要的功能,例如检测某资源是否存在等。而Spring的Resource声明了访问low-level资源的能力。
Spring 的 Resource 接口位于 org.springframework.core.io 中。 旨在成为一个更强大的接口,用于抽象对低级资源的访问。以下显示了Resource接口定义的方法。
Resource接口继承了InputStreamSource接口,提供了很多InputStreamSource所没有的方法。
其中一些重要的方法:
其他方法:
但是我们一般使用的它的实现类!
Resource的一个实现类,用来访问网络资源,它支持URL的绝对路径。