springboot+clickhouse+mybatisplus

1.引入jar包

        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.projectlombok
            lombok
        
 
        
            ru.yandex.clickhouse
            clickhouse-jdbc
            0.1.53
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.2.0
        
        
            com.baomidou
            mybatis-plus-generator
            3.2.0
        

2.添加配置

在application.yml文件中添加如下配置信息

server:
  port: 8080
spring:
 datasource:
  type: com.alibaba.druid.pool.DruidDataSource
  click:
   driverClassName: ru.yandex.clickhouse.ClickHouseDriver
   url: jdbc:clickhouse://172.16.100.17:8123/esthihiate
   initialSize: 10
   maxActive: 100
   minIdle: 10
   maxWait: 6000

mybatis-plus:
  configuration:
    call-setters-on-nulls: true
    auto-mapping-behavior: full
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: com.springbootdemo.dao.mapper.*Mapper.xml
logging:
  file:
    name: springbootdemo.log

 添加数据源配置

package com.springbootdemo.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * JdbcParamConfig
 *
 * @Description
 */
@Configuration
@Data
@ConfigurationProperties(prefix = "spring.datasource.click")
public class JdbcParamConfig {

    private String driverClassName;

    private String url;

    private Integer initialSize;

    private Integer maxActive;

    private Integer minIdle;

    private Integer maxWait;


}
package com.springbootdemo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * DataSourceClickHouseConfig
 *
 * @Description
 */
@Configuration
public class DruidDataSourceConfig {

    @Resource
    private JdbcParamConfig jdbcParamConfig;
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcParamConfig.getUrl());
        dataSource.setDriverClassName(jdbcParamConfig.getDriverClassName());
        dataSource.setInitialSize(jdbcParamConfig.getInitialSize());
        dataSource.setMinIdle(jdbcParamConfig.getMinIdle());
        dataSource.setMaxActive(jdbcParamConfig.getMaxActive());
        dataSource.setMaxWait(jdbcParamConfig.getMaxWait());
        return dataSource;
    }


}

添加mybatisplus插件

package com.springbootdemo.config;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MybatisPlusConfig
 *
 * @Description
 */
@Configuration
@EnableTransactionManagement
@MapperScan("com.springbootdemo.dao.mapper")
public class MybatisPlusConfig {

    /**
     * 配置乐观锁插件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }



}

3.entity类

package com.springbootdemo.entity;

import com.alibaba.druid.support.monitor.annotation.MTable;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
 * UserInfo
 *
 * @Description
 */
@Data
@TableName("cs_user_info")
public class UserInfo {

    @TableId("id")
    private Long id;

    private String userName;

    private String passWord;

    private String phone;

    private String email;

    private String createDay;

}

4.dao层

package com.springbootdemo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springbootdemo.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * UserInfoMapper
 *
 * @Description
 */
@Repository
public interface UserInfoMapper extends BaseMapper {

    Long selectMaxId();
}

5.mapper层 







6.service层

package com.springbootdemo.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.springbootdemo.dao.UserInfoMapper;
import com.springbootdemo.entity.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserInfoService
 *
 * @Description
 */
@Service
public class UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    public void saveData(UserInfo userInfo) {
        //userInfo.setId(userInfoMapper.selectMaxId()+1);
        userInfoMapper.insert(userInfo);
    }

    public UserInfo selectById(@Param("id") Integer id) {
        return userInfoMapper.selectById(id);
    }

    public List selectList() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        return userInfoMapper.selectList(queryWrapper);
    }

}

7.controller层

package com.springbootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.springbootdemo.entity.UserInfo;
import com.springbootdemo.service.UserInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserInfoController
 *
 * @Description
 */
@RestController
@RequestMapping("/api/userInfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;


    @RequestMapping("/saveData")
    public void saveData(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1L);
        userInfo.setCreateDay("2022-04-15");
        userInfo.setEmail("[email protected]");
        userInfo.setPhone("13243545464");
        userInfo.setUserName("admin");
        userInfo.setPassWord("admin");
        userInfoService.saveData(userInfo);
    }

    @RequestMapping("/selectById")
    public String selectById(@Param("id") Integer id){
        return JSON.toJSONString(userInfoService.selectById(id));
    }

    @RequestMapping("/selectList")
    public String selectList(){
        return JSON.toJSONString(userInfoService.selectList());
    }


}

你可能感兴趣的:(springboot,springboot)