资讯专栏INFORMATION COLUMN

springboot (一)集成tkmapper

Shihira / 3435人阅读

摘要:整合想着每次搭建新项目时框架都要从新搭建,基本常用的也就哪几种,现在就来搭建一种常用的后台框架,以后新开小项目可以直接拿来使用项目整体结构图新建空白项目,选中依赖略,也可以完全根据本人代码操作文件依赖项展示

springboot整合tkMapper

想着每次搭建新项目时框架都要从新搭建,基本常用的也就哪几种,现在就来搭建一种常用的springboot后台框架,以后新开小项目可以直接拿来使用
项目整体结构图

1.新建springboot空白项目,选中web依赖(略,也可以完全根据本人代码操作)
2.pom文件依赖项展示



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.cxt
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
            mysql
            mysql-connector-java
            8.0.16
            runtime
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            RELEASE
        
        
            tk.mybatis
            mapper
            3.4.5
        
        
        
            com.alibaba
            druid
            1.1.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        8.0.16
                    
                    
                        tk.mybatis
                        mapper-generator
                        1.1.3
                    
                
            
        
    

application.yml文件内容

server:
  port: 8081

#数据源配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #Druid连接池
    url: jdbc:mysql://localhost:3306/springboot-demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=true&nullCatalogMeansCurrent=true
    username: root #数据库用户名
    password: root #数据库密码
    driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动
    initialSize: 10 #初始化连接池大小
    minIdle: 10 #初始化最小连接池数量
    maxActive: 100 #初始化最大连接池数量
    maxWait: 6000 #配置获取连接等待超时的时间
    timeBetweenEvictionRunsMills: 6000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    minEvictableIdleTimeMillis: 30000 #配置一个连接在池中最小生存的时间,单位是毫秒
    validationQuery: SELECT "x" #测试连接
mybatis:
  mapper-locations: classpath:/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
    #打印sql
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper:
  identity: MYSQL # 配置主键自动增长(使用MYSQL原生方式)
logging:
  level:
    com.cxt.demo: debug
# 分页插件
pagehelper:
  reasonable: true
  page-size-zero: true
  params: pageNum=start;pageSize=limit
  support-methods-arguments: true

新建MyBaseMapper,( 注意此接口不能被扫描到 *

package com.cxt.demo.tk;

import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.ExampleMapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * @author liBai
 * @Classname MyBaseMapper
 * @Description TODO base mapper 不能被扫描
 * @Date 2019-06-02 10:41
 */
@Repository
public interface MyBaseMapper  extends Mapper, MySqlMapper , ExampleMapper {
}

启动类添加扫描mapper

注意此处@MapperScan要引tk包下面的,否则会报错
(import tk.mybatis.spring.annotation.MapperScan;)

package com.cxt.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @author liBai
 * mapperscan 要引tk包下面的
 */
@SpringBootApplication
@MapperScan(basePackages = "com.cxt.demo.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

添加MybatisConfigurer配置类

package com.cxt.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author liBai
 * @Classname MybatisConfigurer
 * @Description TODO
 * @Date 2019-06-02 10:42
 */
@SpringBootConfiguration
public class MybatisConfigurer {
    @Resource
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.cxt.demo.bean");
        return bean.getObject();
    }

}

数据库创建sql语句(测试tkmapper链接数据库)

CREATE TABLE `test_sys` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id`,`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

#插入数据
INSERT INTO `springboot-demo`.`test_sys` (`id`, `name`) VALUES ("1‘, "test1");
INSERT INTO `springboot-demo`.`test_sys` (`id`, `name`) VALUES ("2‘, "test2");
INSERT INTO `springboot-demo`.`test_sys` (`id`, `name`) VALUES ("3‘, "test3");

generator自动生成bean,dao,mapper,generator配置内容如下



    
        
        

        


            
            
            
            
            
            
        
        
        
        

        

        

        


        

点击maven命令执行生成相对应的bean,dao,mapper


或者使用maven命令

mybatis-generator:generate –e

创建HelloController

package com.cxt.demo.controller;

import com.cxt.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liBai
 * @Classname HelloController
 * @Description TODO
 * @Date 2019-06-02 10:49
 */
@RestController
@RequestMapping("/test")
public class HelloController {
    @Autowired
    private HelloService helloService;
    @RequestMapping("/hello")
    public String hello(){
        return helloService.sayHello();
    }
}

创建HelloService

package com.cxt.demo.service;

/**
 * @author liBai
 * @Classname HelloService
 * @Description TODO
 * @Date 2019-06-02 10:49
 */
public interface HelloService {
    String sayHello();

}

创建HelloServiceImpl实现

package com.cxt.demo.service.impl;

import com.cxt.demo.bean.TestSys;
import com.cxt.demo.dao.TestSysMapper;
import com.cxt.demo.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liBai
 * @Classname HelloServiceImpl
 * @Description TODO
 * @Date 2019-06-02 10:50
 */
@Service
@Slf4j
public class HelloServiceImpl implements HelloService {
    @Autowired
    private TestSysMapper testSysMapper;

    @Override
    public String sayHello() {
        TestSys testSys = testSysMapper.selectByPrimaryKey("1");
        log.debug("testSys  =  [{}]",testSys.toString());
        return testSys.getName();
    }
}

浏览器测试,url中输入 http://localhost:8081/test/hello 显示

到此为止,springboot整合通用mapper,使用mysql的demo就结束了,后续整合请关注后续博客

源码地址 https://github.com/TianPuJun/...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77789.html

相关文章

  • springboot(二)集成redis

    摘要:相隔很久,我又回来了,献上一篇整合的教程给新手看一下吧,技术点不怎么有,最简单的配置,入手即用,那下面开始本章在我的上一篇文章为基础继续整合的,不知道的可以见我的整合整合下面开始引入依赖整合文件根据自己配置添加信息代码,用户 相隔很久,我又回来了,献上一篇整合redis 的教程给新手看一下吧,技术点不怎么有,最简单的配置,入手即用,那下面开始 本章在我的上一篇文章为基础继续整合的,不知...

    zhangke3016 评论0 收藏0
  • SpringBoot非官方教程 | 第十二篇:springboot集成apidoc

    摘要:首先声明下,是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了系列的完整性,所以标了个题。二准备工作安装完安装它的项目源码。输命令输入目录输出目录是我的工程名。 首先声明下,apidoc是基于注释来生成文档的,它不基于任何框架,而且支持大多数编程语言,为了springboot系列的完整性,所以标了个题。 一、apidoc简介 apidoc通过在你代码的注释来生成ap...

    xiaoxiaozi 评论0 收藏0
  • SpringBoot非官方教程 | 第十篇:SpringBoot集成swagger2,构建优雅的R

    摘要:另外很容易构建风格的,简单优雅帅气,正如它的名字。配置一些基本的信息。三写生产文档的注解通过注解表明该接口会生成文档,包括接口名请求方法参数返回信息的等等。四参考资料中使用构建强大的文档 swagger,中文拽的意思。它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试。另外swagger很容易构建restful风格的api,简单优雅帅气...

    荆兆峰 评论0 收藏0
  • SpringBoot 1024行代码 - 集成Logback

    前言 SpringBoot是一个全家桶,可以方便的集成各种开发工具。日志框架是一个在线应用必需的,本文介绍了当前主流日志框架Logback与SpringBoot的集成方法 准备工作 完成SpringBoot 1024行代码 - Getting Started(一个简单的web应用) 具体步骤 1. 添加Logback的配置文件logback-springboot.xml 其中文件名需要为logba...

    dailybird 评论0 收藏0
  • SpringBoot 实战 () | 如何使用 IDEA 构建 Spring Boot 工程

    摘要:它使用约定大于配置的理念让你的项目快速运行起来。如何使用构建工程第一步,当然是安装傻瓜式教程,请自行百度。包名,填完和后自动生成,默认即可。确认无误,点完成创建即可。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 新年立了个 flag,好好运营这个公众号。具体来说,就是每周要写两篇文章在这个号发表。刚立的 flag 可不能这么快打脸。下面送上本周第...

    Ryan_Li 评论0 收藏0

发表评论

0条评论

Shihira

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<