Spring整合Mybatis框架详细说明

Spring容器整合Mybatis框架

Spring框架特点:粘合剂->不生产轮子,只提供轮子运行平台。
首先可以通过MyBatis插件生产dao\mapper\pojo文件【不会的点击我】,数据库表随意
生产后的文件结构:
在这里插入图片描述

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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
	<!-- 数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>	
	<!-- SqlSessionFactory -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property>
	</bean>
	<!-- Mapper映射 -->
	<!-- <bean id="bookMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sessionFactory"></property>
		<property name="mapperInterface" value="com.uplooking.dao.BookMapper"></property>
	</bean> -->
	<!-- Mapper扫描 隐式规则:id为接口首字母小写 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.uplooking.dao"></property>
	</bean>
</beans>

MyBatis容器配置文件:MyBatis.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
	<settings>
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
</configuration>

执行测试代码:MyTest.java

public class MyTest {

	@Test
	public void test() {
		/*BasicDataSource dataSource= null;
		dataSource.setDriverClassName(driverClassName);
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);*/
	
		/*SqlSessionFactoryBean sessionFactory;
		sessionFactory.setConfigLocation(configLocation);
		sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
		sessionFactory.setDataSource(dataSource);
		sessionFactory.setMapperLocations(mapperLocations);*/
		
		/*MapperFactoryBean bean;
		bean.setSqlSessionFactory(sqlSessionFactory);
		bean.setMapperInterface(mapperInterface);*/
		
		/*MapperScannerConfigurer configurer;
		configurer.setBasePackage(basePackage);*/
		
		String configLocation = "applicationContext.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		//System.out.println(ctx.getBean("bookMapper"));
		
		BookMapper bookMapper = (BookMapper) ctx.getBean("bookMapper");
		
		System.out.println(bookMapper.selectByPrimaryKey(1));
		
		CityMapper cityMapper = (CityMapper) ctx.getBean("cityMapper");
		
		System.out.println(cityMapper.selectByPrimaryKey(11));
		
	}

}

可以通过Spring容器完成数据库的数据操作,完成了Spring容器和MyBatis框架的整合。

将applicationContext.xml和MyBatis.cfg.xml整理为一个配置文件:spring-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/shiro?characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:mappers/*Mapper.xml"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
        <property name="basePackage" value="com.uplooking.dao"></property>
    </bean>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你不懂、、、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值