Spring整合Mybatis

整合要点 主要以下两个类

1 SqlSessionFactoryBean   主要两个属性 DataSource数据库连接池   mapperLocations sql定义定义文件存放位置


2 MapperFactoryBean  对接口产生实现类 需要一个 SqlSessionFactoryBean   以及 mapperInterface 对应的 Dao接口

示例

导入对应的jar包 ioc aop dao 数据库连接池 数据库驱动 mybatis mybatis-spring
创建实体类
创建对应的mapper接口 对应的 mapper文件
在spring配置文件中配置 SqlSessionFactoryBean 对象 产生 SqlSessionFactory 依赖于dataSource 和 sql 定义
在Spring 配置文件中配置 MapperFactoryBean 用来产生Mapper 接口的实现类 依赖于 接口 和 SqlSessionFactory
创建Spring 从容器获取 Mapper 接口的实现类 测试

实体类就不创建了 直接从接口开始

查询所有的用户
public interface UserDao {
	List<User> findAll();
}

sql定义文件

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
 <!-- namespace指定和哪个Mapper映射器接口对应 -->
<mapper namespace="com.liheng.dao.UserDao">
	<!-- 定义SQL语句 -->	
      <select id="findAll"  resultType="com.liheng.bean.User">
    	select * from bank 
    </select>
    
</mapper> 

spring配置文件 配置 SqlSessionFactoryBean MapperFactoryBean

<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	<mvc:annotation-driven></mvc:annotation-driven>
	<!--开启引入外部文件  db.properties文件-->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 配置连接池对象 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.name}"></property>
		<property name="password" value="${jdbc.pass}"></property>
	</bean>

	<!-- sqlsessionFactory 可以创建 sqlsessionFactory对象 需要依赖 连接池  以及 mapper文件 -->
	 <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	 需要一个dataSource
		<property name="dataSource" ref="dataSource"></property>
		mapperLocations 该属性是定义sql文件的位置
		<property name="mapperLocations" value="classpath:com/liheng/dao/*.xml"></property>
	</bean>

<!--MapperFactoryBean 可以根据对应的Dao 接口产生实现类 需要 sqlSessionFactory 以及  Dao接口-->
	注意当前 MapperFactoryBean 只产生了一个 实体类 只支持产生一个 
	<bean id="MapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlsessionFactory"></property>
		<property name="mapperInterface" value="com.liheng.dao.UserDao"></property>
	</bean> 

批量产生Mapper实现类的方式 需要 定义 mapperScanner 类 注释 MapperFactoryBean

属性就是一个 basePackage 需要写入dao接口的包名 会批量产生
自动在容器中创建一个实现类对象 默认名字 类名首字母小写可以直接获取Bean组件

	<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
		<property name="basePackage" value="com.liheng.dao"></property>
	</bean>

测试


public class Run {

	public static void main(String[] args) {
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
		app.getBean(MapperScanner 产生的实现类);
		//获得到的对象可以直接调用方法 
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值