栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录

纯注解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



    
        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.Driver
mybatis.url=jdbc:mysql://localhost:3306/chat?serverTimezone=UTC
mybatis.username=root
mybatis.password=123456
mybatis.mapperLocations=com.frame.mybatis/*Mapper.xml
mybatis.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")
@Data
public 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;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736671.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号