项目新增mybatis-plus

26 篇文章 0 订阅
4 篇文章 0 订阅

依赖

<!--		mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.22</version>
		</dependency>

数据源

@Data
@Component
@ConfigurationProperties(prefix = "datasource")
public class BaseDataSourceProperties {
    protected  String username;
    protected  String password;
    protected  String url;
    protected  String driverClassName;
    protected  String mapperLoacations;
}
package com.zhang.oauther2.mysql.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Copyright 深圳金雅福控股集团有限公司
 * @Author: zhangfanjun
 * @Date 2021/11/17
 * @Version: 1.0
 */
@ConditionalOnBean(BaseDataSourceProperties.class)
@Configuration
public class MybatisDataSourceConfig {
    @Autowired
    BaseDataSourceProperties baseDataSourceProperties;

//    @Value("${mybatis-plus.mapper-locations}")
//    private String mapperLoacations;

    @Bean(name = "baseDataSource")
    @Primary
    public DataSource baseDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(baseDataSourceProperties.getDriverClassName());
        dataSource.setUrl(baseDataSourceProperties.getUrl());
        dataSource.setUsername(baseDataSourceProperties.getUsername());
        dataSource.setPassword(baseDataSourceProperties.getPassword());
        return dataSource;
    }


    @Bean(name = "baseSqlSessionFactory")
    @Primary
    public SqlSessionFactory baseSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        //数据源地址
        bean.setDataSource(dataSource);
        // 设置mybatis的主配置文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] mybatisConfigXml = resolver.getResources(baseDataSourceProperties.getMapperLoacations());
//        Resource[] mybatisConfigXml = resolver.getResources(mapperLoacations);
        bean.setMapperLocations(mybatisConfigXml);
        return bean.getObject();
    }

    @Bean(name = "baseTransactionManager")
    @Primary
    public DataSourceTransactionManager baseTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "baseSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate baseSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

entity

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SsoPermission extends Model<SsoPermission> {

    private static final long serialVersionUID = 1L;

    @TableId(type= IdType.AUTO)
    private Integer id;

    @TableField("name")
    private String name;

    @TableField("uri")
    private String uri;

    @TableField("create_time")
    private Date createTime;

    @TableField("update_time")
    private Date updateTime;


    @Override
    protected Serializable pkVal() {
        return null;
    }

}

mapper接口

public interface SsoPermissionMapper extends BaseMapper<SsoPermission> {

}

service

public interface ISsoPermissionRoleService extends IService<SsoPermissionRole> {

}
@Service
public class SsoPermissionRoleServiceImpl extends ServiceImpl<SsoPermissionRoleMapper, SsoPermissionRole> implements ISsoPermissionRoleService {

}

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.zhang.oauther2.mysql.mapper.SsoPermissionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zhang.oauther2.mysql.entity.SsoPermission">
    <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="uri" property="uri" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

</mapper>

扫描mapper

@MapperScan("com.zhang.oauther2.mysql.mapper")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值