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

纯注解Spring+Mybatis编程

01 基础配置

  • 连接池
  • SqlSessionFactoryBean
  • MapperScannerConfigure
@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<User> getAllUser();
}

03 Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.frame.mybatis.UserMapper">
    <select id="getAllUser" resultType="user">
        select * from user
    </select>

</mapper>

04 使用

ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
List<User> 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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

佩奇inging

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

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

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

打赏作者

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

抵扣说明:

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

余额充值