spring与mybatis的使用
2021-05-05 22:31
标签:lap 定义 ann instance run tor att glib value
1、spring与mybatis整合配置文件版
spring与mybaits整合需要一个中间包mybatis-spring,将mybatis交给spring管理,中间包pom依赖如下
org.mybatis mybatis-spring2.0.3
完整pom依赖如下
junit junit4.11 test com.alibaba druid1.1.10 org.springframework spring-core5.1.5.RELEASE org.springframework spring-context5.1.5.RELEASE org.springframework spring-jdbc5.1.5.RELEASE org.springframework spring-tx5.1.5.RELEASE org.springframework spring-beans5.1.5.RELEASE org.mybatis mybatis3.5.3 org.aspectj aspectjweaver1.9.5 org.mybatis mybatis-spring2.0.3 mysql mysql-connector-java5.1.37 runtime
spring的application配置
classpath:jdbc.properties
mybatis config配置
之后需要对实体对应的mapper文件进行编写,有xml或者注解的方式进行定义,mapper文件如下。
package com.wonders.bigdata.mapper; import com.wonders.bigdata.domain.Websites; import com.wonders.bigdata.domain.WebsitesExample; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; public interface WebsitesMapper { int countByExample(WebsitesExample example); int deleteByExample(WebsitesExample example); int deleteByPrimaryKey(Long id); int insert(Websites record); int insertSelective(Websites record); ListselectByExample(WebsitesExample example); Websites selectByPrimaryKey(Long id); int updateByExampleSelective(@Param("record") Websites record, @Param("example") WebsitesExample example); int updateByExample(@Param("record") Websites record, @Param("example") WebsitesExample example); int updateByPrimaryKeySelective(Websites record); int updateByPrimaryKey(Websites record); @Select("select * from bd_websites where id = #{id}") Websites findById(Long id); }
对应service编写
package com.wonders.bigdata.spring.service; import com.wonders.bigdata.domain.Websites; public interface WebsitesService { Websites findById(Long id); }
package com.wonders.bigdata.spring.service.impl; import com.wonders.bigdata.domain.Websites; import com.wonders.bigdata.mapper.WebsitesMapper; import com.wonders.bigdata.spring.service.WebsitesService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class WebsitesServiceImpl implements WebsitesService { @Autowired private WebsitesMapper websitesMapper; @Override public Websites findById(Long id) { return websitesMapper.findById(id); } }
测试
@Test public void springContext() { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-context.xml"); MessageService messageService = context.getBean(MessageService.class); WebsitesService websitesService = context.getBean(WebsitesService.class); System.out.println(messageService.getMessage()); System.out.println(websitesService.findById(1L)); }
2、mybatis-spring整合原理
mybatis主要是通过使用sping的ImportBeanDefinitionRegistrar接口来将其交给spring管理,其中实现了ImportBeanDefinitionRegistrar接口的类将会拥有注册bean的能力,而其中对应bean对象的创建是通过实现FactoryBean接口来管理
MapperScannerRegistrar源码
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationAttributes mapperScanAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName())); if (mapperScanAttrs != null) { this.registerBeanDefinitions(mapperScanAttrs, registry, generateBaseBeanName(importingClassMetadata, 0)); } }
void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegistry registry, String beanName) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class); builder.addPropertyValue("processPropertyPlaceHolders", true); Class extends Annotation> annotationClass = annoAttrs.getClass("annotationClass"); if (!Annotation.class.equals(annotationClass)) { builder.addPropertyValue("annotationClass", annotationClass); } Class> markerInterface = annoAttrs.getClass("markerInterface"); if (!Class.class.equals(markerInterface)) { builder.addPropertyValue("markerInterface", markerInterface); } Class extends BeanNameGenerator> generatorClass = annoAttrs.getClass("nameGenerator"); if (!BeanNameGenerator.class.equals(generatorClass)) { builder.addPropertyValue("nameGenerator", BeanUtils.instantiateClass(generatorClass)); } Class extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean"); if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) { builder.addPropertyValue("mapperFactoryBeanClass", mapperFactoryBeanClass); } String sqlSessionTemplateRef = annoAttrs.getString("sqlSessionTemplateRef"); if (StringUtils.hasText(sqlSessionTemplateRef)) { builder.addPropertyValue("sqlSessionTemplateBeanName", annoAttrs.getString("sqlSessionTemplateRef")); } String sqlSessionFactoryRef = annoAttrs.getString("sqlSessionFactoryRef"); if (StringUtils.hasText(sqlSessionFactoryRef)) { builder.addPropertyValue("sqlSessionFactoryBeanName", annoAttrs.getString("sqlSessionFactoryRef")); } ListbasePackages = new ArrayList(); basePackages.addAll((Collection)Arrays.stream(annoAttrs.getStringArray("value")).filter(StringUtils::hasText).collect(Collectors.toList())); basePackages.addAll((Collection)Arrays.stream(annoAttrs.getStringArray("basePackages")).filter(StringUtils::hasText).collect(Collectors.toList())); basePackages.addAll((Collection)Arrays.stream(annoAttrs.getClassArray("basePackageClasses")).map(ClassUtils::getPackageName).collect(Collectors.toList())); String lazyInitialization = annoAttrs.getString("lazyInitialization"); if (StringUtils.hasText(lazyInitialization)) { builder.addPropertyValue("lazyInitialization", lazyInitialization); } builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages)); registry.registerBeanDefinition(beanName, builder.getBeanDefinition()); }
根据扫描的mapper名称对bean进行注册
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
spring与mybatis的使用
标签:lap 定义 ann instance run tor att glib value
原文地址:https://www.cnblogs.com/snow-wolf-1/p/13191524.html
下一篇:Spring注解总结