目录
ApplicationContext.xml配置文件图文解析
Spring+mybatis的整合步骤
1.引入依赖:在项目的构建管理工具中引入Spring和MyBatis的依赖
2.在ApplicationContext.xml配置dataSource数据源 比如连接驱动,连接数据源的url和连接数据库的用户名和密码
3.在ApplicationContext.xml配置文件中创建一个sqlSessionFactory对象,并将数据源和MyBatis的配置文件注入到该对象中
4.配置中使用MapperScannerConfigurer配置MyBatis的扫描器
5.定义Mapper接口,并在XML文件中编写对应的SQL语句
6.在Service层中注入Mapper接口的实例,并调用其方法进行数据库操作
ApplicationContext.xml配置文件图文解析
使用mybatis+spring实现增删改查
整合需要用到的配置文件
ApplicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1.引入properties文件-->
<context:property-placeholder location="连接数据的配置文件(jdbc.proerities)"></context:property-placeholder>
<!-- 2.配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<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>
<!--3.创建sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <!--name此处的name是指上面数据源的id-->
<property name="configLoc