博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis学习笔记(17)-spring和mybatis整合
阅读量:6261 次
发布时间:2019-06-22

本文共 7633 字,大约阅读时间需要 25 分钟。

hot3.png

mybatis学习笔记(17)-spring和mybatis整合

标签: mybatis springmvc


[TOC]


本文主要将如何将spring和mybatis整合,只是作简单的示例,没有使用Maven构建。并展示mybatis与spring整合后如何进行原始dao开发和mapper代理开发。

整合思路

需要spring通过单例方式管理SqlSessionFactory

spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)

持久层的mapper都需要由spring进行管理。

整合环境

创建一个新的java工程(接近实际开发的工程结构)

jar包:

  • mybatis3.2.7的jar包
  • spring3.2.0的jar包
  • mybatis和spring的整合包:早期ibatis和spring整合是由spring官方提供,mybatis和spring整合由mybatis提供。

mybatis与spring整合工程结构图

sqlSessionFactory

在applicationContext.xml配置sqlSessionFactory和数据源

sqlSessionFactory在mybatis和spring的整合包下。

原始dao开发(和spring整合后)

  • User.xml

在SqlMapconfig.xml中加载User.xml

  • dao(实现类继承SqlSessionDaoSupport)
public interface UserDao {    //根据id查询用户信息    public User findUserById(int id) throws Exception;}

dao接口实现类需要注入SqlSessoinFactory,通过spring进行注入。这里spring声明配置方式,配置dao的bean:

让UserDaoImpl实现类继承SqlSessionDaoSupport

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{    @Override    public User findUserById(int id) throws Exception {        //继承SqlSessionDaoSupport,通过this.getSqlSession()得到sqlSessoin        SqlSession sqlSession = this.getSqlSession();        User user = sqlSession.selectOne("test.findUserById",id);        return user;    }}
  • 配置dao

在applicationContext.xml中配置dao

  • 测试程序
public class UserDaoImplTest {	private ApplicationContext applicationContext;	//在setUp这个方法得到spring容器	@Before	public void setUp() throws Exception {		applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");	}	@Test	public void testFindUserById() throws Exception {		// 创建UserDao的对象		UserDao userDao = (UserDao)applicationContext.getBean("userDao");		// 调用UserDao的方法		User user = userDao.findUserById(1);				System.out.println(user);	}}

mapper代理开发

  • mapper.java
public interface UserMapper {    //根据id查询用户信息    User findUserById(int id) throws Exception;}
  • mapper.xml
  • 通过MapperFactoryBean创建代理对象
//mapperInterface指定mapper接口

此方法问题:需要针对每个mapper进行配置,麻烦。

  • 通过MapperScannerConfigurer进行mapper扫描(建议使用)
  • 测试代码
package com.iot.mybatis.mapper;import com.iot.ssm.mapper.UserMapper;import com.iot.ssm.po.User;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserMapperTest {	private ApplicationContext applicationContext;	//在setUp这个方法得到spring容器	@Before	public void setUp() throws Exception {		applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");	}	@Test	public void testFindUserById() throws Exception {		UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");		//调用userMapper的方法		User user = userMapper.findUserById(1);		System.out.println(user);	}}

遇到的问题

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\intellij\workspace\spring-mybatis\out\production\spring-mybatis\com\iot\ssm\mapper\UserMapper.class]; nested exception is java.lang.IllegalArgumentException	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:281)	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)	at org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:155)	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:220)	at org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry(MapperScannerConfigurer.java:315)	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)	at org.springframework.context.support.ClassPathXmlApplicationContext.
(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.
(ClassPathXmlApplicationContext.java:83) at com.iot.mybatis.mapper.UserMapperTest.setUp(UserMapperTest.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:292) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)Caused by: java.lang.IllegalArgumentException at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.asm.ClassReader.
(Unknown Source) at org.springframework.core.type.classreading.SimpleMetadataReader.
(SimpleMetadataReader.java:52) at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80) at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101) at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:257) ... 35 more
  • 搜到的答案

总结起来就是java 8 和spring 3 不能一起用,我在IDEA的project settings里把project language level换成7就好了。具体原因还不清楚


作者更多文章: | |

转载于:https://my.oschina.net/brianway/blog/630719

你可能感兴趣的文章
MATLAB模糊逻辑(2)
查看>>
linux 内核模块管理
查看>>
【每日一摩斯】-【序列】-续-RAC and Sequences (853652.1)
查看>>
把一个select查询结果插入到一个表(可选指定字段和值实例)
查看>>
使用windbg抓取崩溃文件和分析的过程
查看>>
ViewHolder模式超简洁写法
查看>>
项目管理学习笔记之三.绩效分析
查看>>
php十行代码将xml转成数组
查看>>
centos 7 执行 groupinstall报错
查看>>
Web开发入门
查看>>
Flex开发小结(1)如何使用AdvancedDataGrid
查看>>
AFNetworking 下载文件断点续传操作
查看>>
Jar mismatch! Fix your dependencies
查看>>
哀悼日, 网页变灰的实现
查看>>
php:检测用户当前浏览器是否为IE浏览器
查看>>
linux命令备份
查看>>
10个你可能不知道的JavaScript小技巧
查看>>
【ASP】文件上传
查看>>
集合类(数据结构图、集合图、集合之间的比较)
查看>>
hibernate _关联级别策略介绍
查看>>