<?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 http://www.springframework.org/schema/context/spring-context.xsd"> <!--<context:component-scan base-package="com.tjise.bean"/>--> <!-- 加载数据库连接信息的属性文件 --> <context:property-placeholder location="classpath:jdbc-config.properties"/> <!-- 配置Druid数据源的Bean --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置SessionFactory的Bean --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 指定MyBatis配置文件的位置 --> <property name="configLocation" value="classpath:mybatis.xml"/> <!-- 给实体类起别名 --> <property name="typeAliasesPackage" value="com.tjise.entity"/> </bean> <!-- 配置mapper接口的扫描器,将Mapper接口的实现类自动注入到IoC容器中 实现类Bean的名称默认为接口类名的首字母小写 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- basePackage属性指定自动扫描mapper接口所在的包 --> <property name="basePackage" value="com.tjise.mapper"/> </bean> </beans>
时间: 2025-05-11 22:26:00 浏览: 21
上述代码是一个基于Spring框架的XML配置文件,主要用于配置数据库连接、MyBatis以及相关的组件扫描功能。以下是对其内容的具体解析:
### 1. 基本结构说明
```xml
<?xml version="1.0" encoding="UTF-8"?>
```
这是标准的XML声明头,表明这是一个版本为1.0并采用UTF-8编码的XML文档。
```xml
<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="...省略...">
```
`<beans>`标签用于定义Spring容器的基本元素集合,并通过命名空间(如`xmlns:context`)引入额外的功能模块。`xsi:schemaLocation`指定了对应的XSD文件路径,以验证XML的有效性。
---
### 2. 数据库属性加载
#### `<context:property-placeholder>`
```xml
<context:property-placeholder location="classpath:jdbc-config.properties"/>
```
该标签会从指定位置读取外部化的配置文件,在这里是从类路径下的`jdbc-config.properties`文件中获取键值对形式的数据库相关信息(例如驱动程序、URL等)。这有助于解耦硬编码和配置项之间的绑定关系。
---
### 3. 配置Druid数据源
#### `<bean id="dataSource">`
```xml
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
```
这个部分创建了一个名为`dataSource`的数据源Bean实例,使用的是阿里巴巴开源的高性能数据库连接池工具——**Druid**。各个参数均来源于之前提到过的`jdbc-config.properties`配置文件中的占位符变量`${}`。
---
### 4. MyBatis SqlSessionFactory Bean配置
#### `<bean id="sessionFactory">`
```xml
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="typeAliasesPackage" value="com.tjise.entity"/>
</bean>
```
此片段负责初始化MyBatis的核心工厂对象`SqlSessionFactory`。其中关键点包括:
- **dataSource**: 引用了前面已定义好的`dataSource` Bean。
- **configLocation**: 指向了位于资源目录下具体的MyBatis主配置文件(`mybatis.xml`)。
- **typeAliasesPackage**: 扫描给定包内所有符合条件的类,并为之分配简短别名供SQL映射文件引用。
---
### 5. Mapper接口扫描注册
#### `<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">`
```xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tjise.mapper"/>
</bean>
```
为了方便操作DAO层持久化逻辑,通常会借助代理模式自动生成Mapper接口对应实现类并将它们注入IOC容器之中。上述设置即为此目的服务,只需告知需要搜索的目标基础包即可完成自动化处理过程。(这里的示例设成了`com.tjise.mapper`)
---
以上就是整个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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.itheihei" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/images/**" location="/images/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:annotation-driven/> </beans><?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.itheihei"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>@Controller public class AccountController { @Autowired AccountService accountService; @RequestMapping("findAllAccounts") public String findAllAccounts() { System.out.println("方法成功执行"); return "list"; } } <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> 查看所有用户信息 </body> </html><?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> contextConfigLocation classpath:spring-mvc.xml </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>为什么会404









