欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Spring学习笔记【十六】纯注解Spring+Mybatis编程

时间:2023-06-12
文章目录

纯注解Spring+Mybatis编程

01 基础配置02 Mapper03 Mapper.xml04 使用05 MapperLocations 编码时通配的写法06 耦合问题

6.1 创建配置文件6.2 新建一个类6.3 配置类 纯注解Spring+Mybatis编程 01 基础配置

连接池SqlSessionFactoryBeanMapperScannerConfigure

@Configuration@ComponentScan("com.frame.mybatis")@MapperScan(basePackages = ("com.frame.mybatis"))public class AppConfig { @Bean public DruidDataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/chat?serverTimezone=UTC"); dataSource.setUsername("root"); dataSource.setPassword("123456"); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(){ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); sqlSessionFactoryBean.setTypeAliasesPackage("com.frame.mybatis"); sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserMapper.xml")); return sqlSessionFactoryBean; }}

02 Mapper

public interface UserMapper { public List getAllUser();}

03 Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?> select * from user

04 使用

ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);UserMapper userMapper = (UserMapper) ac.getBean("userMapper");List allUser = userMapper.getAllUser();

05 MapperLocations 编码时通配的写法

sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserMapper.xml"));// 参数实际上就是Resource的可变长参数 public void setMapperLocations(Resource..、mapperLocations) { this.mapperLocations = mapperLocations; }// 通配的写法ResourcePatternResolver pattern = new PathMatchingResourcePatternResolver();Resource[] resources = pattern.getResources("com.frame.mybatis/*Mapper.xml");sqlSessionFactoryBean.setMapperLocations(resources);

06 耦合问题 6.1 创建配置文件

mybatis.driverClassName=com.mysql.cj.jdbc.Drivermybatis.url=jdbc:mysql://localhost:3306/chat?serverTimezone=UTCmybatis.username=rootmybatis.password=123456mybatis.mapperLocations=com.frame.mybatis/*Mapper.xmlmybatis.typeAliasesPackages=com.frame.mybatis

6.2 新建一个类

package com.frame.mybatis;import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component@PropertySource("mybatis.properties")@Datapublic class MybatisProperties { @Value("${mybatis.driverClassName}") private String driverClassName; @Value("${mybatis.url}") private String url; @Value("${mybatis.username}") private String username; @Value("${mybatis.password}") private String password; @Value("${mybatis.typeAliasesPackages}") private String typeAliasesPackages; @Value("${mybatis.mapperLocations}") private String mapperLocations;}

6.3 配置类

@Configuration@ComponentScan("com.frame.mybatis")@MapperScan(basePackages = ("com.frame.mybatis"))@PropertySource("mybatis.properties")public class AppConfig { @Autowired private MybatisProperties mybatisProperties; @Bean public DruidDataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(mybatisProperties.getDriverClassName()); dataSource.setUrl(mybatisProperties.getUrl()); dataSource.setUsername(mybatisProperties.getUsername()); dataSource.setPassword(mybatisProperties.getPassword()); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); sqlSessionFactoryBean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackages()); ResourcePatternResolver pattern = new PathMatchingResourcePatternResolver(); Resource[] resources = pattern.getResources(mybatisProperties.getMapperLocations()); sqlSessionFactoryBean.setMapperLocations(resources); return sqlSessionFactoryBean; }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。