mybatis接入

1.引入配置:zzzz.jdbc.url=jdbc:mysql://192.168.101.31:3306/zzzz?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true

zzzz.jdbc.username=xxx

zzzz.jdbc.password=xxx

zzzz.jdbc.driverclass=com.mysql.jdbc.Driver

Pom.xml依赖

<!-- mzzatis -->

<dependenzz>

<groupId>com.zz.zzwb</groupId>

<artifactId>data-service-core</artifactId>

<version>${data-service-core-version}</version>

<exclusions>

<exclusion>

<groupId>com.zz.redis</groupId>

<artifactId>zz-redis-core</artifactId>

</exclusion>

</exclusions>

</dependenzz>

<dependenzz>

<groupId>org.mzzatis</groupId>

<artifactId>mzzatis-spring</artifactId>

<version>1.3.0</version>

</dependenzz>

2.初始化mzzatis配置bean:

package com.zz.zz.bean;

 

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

 

import com.alibaba.druid.pool.DruidDataSource;

 

import org.mzzatis.spring.SqlSessionFactorzzean;

import org.mzzatis.spring.annotation.MapperScan;

 

@Configuration

@EnableTransactionManagement

@MapperScan("com.zz.data.service.core.dal.mapper")

public class MzzatisBean {

 

    @Value("${zzzz.jdbc.driverclass}")

    private String driverClass;

    @Value("${zzzz.jdbc.url}")

    private String zzzzJdbcUrl;

    @Value("${zzzz.jdbc.username}")

    private String zzzzJdbcUsername;

    @Value("${zzzz.jdbc.password}")

    private String zzzzJdbcPassword;

    /**

     * MAPPER 文件路径

     */

    private static final String MAPPER_LOCATIONS = "classpath*:com/zz/data/service/mapper/**/*Mapper.xml";

    

    @Bean

    public SqlSessionFactory initzzSessionFactory() throws Exception{

        SqlSessionFactorzzean sqlSessionFactorzzean = new SqlSessionFactorzzean();

        sqlSessionFactorzzean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactorzzean.setMapperLocations(resolver.getResources(MAPPER_LOCATIONS));

        return sqlSessionFactorzzean.getObject();

    }

    

    @Bean

    public DataSource dataSource() {

        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setUrl(zzzzJdbcUrl);

        dataSource.setDriverClassName(driverClass);

        dataSource.setUsername(zzzzJdbcUsername);

        dataSource.setPassword(zzzzJdbcPassword);

        return dataSource;

    }

    

    @Bean

    public PlatformTransactionManager transactionManager() {

        return new DataSourceTransactionManager(dataSource());

    }   

}

3.应用:

@Autowired

    private InfozzMapper infozzMapper;

 

    @Override

    public int selectCount() {

        InfozzQuery query = new InfozzQuery();

        query.setPageSize(1);

        query.setPageNo(1);

        return infozzMapper.countByQuery(query);

    }

 

    @Override

    public List<InfozzDO> selectInfozzByPage(int page, int size) {

        InfozzQuery query = new InfozzQuery();

        query.setPageNo(page);

        query.setPageSize(size);

        return infozzMapper.selectByQuery(query);

    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值