springboot+mybatis

抽象磷火
• 阅读 1082

springboot整合mybatis

一.创建springboot项目

二.配置文件: application.yml


#公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources(不修改相关配置默认必须再resources下,否则扫包扫不到)
mybatis:
  typeAliasesPackage: com.rainbow.quartzdemo
  mapperLocations: classpath:com/rainbow/quartzdemo/mapper/*.xml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource

server:
  port: 8083

三.pom文件

 <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <!-- web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- MYSQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- Jdbc 模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>


        <!-- druid 线程池模块 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.1.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.7.RELEASE</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

四.启动类加注解

@MapperScan("com.rainbow.quartzdemo.mapper")

五.创建Controller,启动测试

package com.rainbow.test.controller;

import com.rainbow.test.mapper.PersonMapper;
import com.rainbow.test.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("test")
public class TestController {
    @Autowired
    private PersonMapper personMapper;

    @RequestMapping("person")
    public @ResponseBody Person getPerson(Integer id){
        Person person = personMapper.selectByPrimaryKey(id);
        return person;
    }
}
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
springboot读取外部配置文件
springboot项目打成jar包后不好进行配置文件修改,可设置为读取外部配置文件,方便进行配置修改.步骤:1.将jar包中的application.properties配置文件复制到自定义路径下;2.运行jar包命令指定外部配置文件路径:nohupjavajar.jarspring.config.location
Element 'configuration' cannot have character [children], because the type's content type is element-only.
整合springbootmybatis时copy网上的配置,然后报错了。<configuration       <!mybatis用于生成代码的配置文件  <configurationFilesrc/main/resources/gener
Easter79 Easter79
3年前
springboot整合mybatis+oracle
第一步认识springboot:springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合。创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包。第二步_打开项目的结构,
Stella981 Stella981
3年前
NutzBoot 开发时配置各种热更新及注意事项
静态文件配置下面这句必须是绝对路径,这样才能保证开发模式下修改静态文件后进行热更新jetty.staticPathLocalD:\\Work\\src\\main\\resources\\static\\beetl模版文件配置下面这句必须是绝对路径,这样才能保证开发模式下修改模版内容后模版
Stella981 Stella981
3年前
MybatisPlus之逻辑删除的坑
Springboot的配置方式_mybatis配置文件映射_mybatisplus:mapperlocations:classpath:/mapper/\Mapper.xmltypeAliasesPackage:com.siyuan.card.model_\以下配置均有默认值,可以不设置_
Stella981 Stella981
3年前
Spring Boot 访问静态资源
在SpringBoot项目中没有我们之前常规web开发的WebContent(WebApp),它只有src目录。在src/main/resources下面有两个文件夹,static和templates。SpringBoot默认在static目录中存放静态页面,而templates中放动态页面。1static目录
Stella981 Stella981
3年前
SpringBoot项目优化和Jvm调优
阅读文本大概需要3分钟。项目调优作为一名工程师,项目调优这事,是必须得熟练掌握的事情。在SpringBoot项目中,调优主要通过配置文件和配置JVM的参数的方式进行。一、修改配置文件关于修改配置文件 application.properties。SpringBoot项目详细的配置文件修改文档https:
Easter79 Easter79
3年前
SpringBoot项目优化和Jvm调优
阅读文本大概需要3分钟。项目调优作为一名工程师,项目调优这事,是必须得熟练掌握的事情。在SpringBoot项目中,调优主要通过配置文件和配置JVM的参数的方式进行。一、修改配置文件关于修改配置文件 application.properties。SpringBoot项目详细的配置文件修改文档https:
Easter79 Easter79
3年前
SpringBoot集成mybatis以及自动化测试代码实现
Mybatis和logback的应用配置1、在module的pom.xml文件中,加载springboot和swagger、lombok、fastjson、mysql、mybatis包2、在resources中添加配置:配置文件有两种,一种是properties,另一种是yaml,这里使用yamlyaml配
Stella981 Stella981
3年前
Spring Boot配置文件详解
一、主配置文件SpringBoot默认主配置文件名为application.yml或者application.properties1.yml和properties1.1yml语法:key:空格value同一个层级的缩进tab或者空格必须相同
Stella981 Stella981
3年前
Maven入门(六) profile标签
项目开发经常碰到配置文件中测试的jdbc.url和线上的是不一样的。为此每次打包都要修改配置文件,很烦,而且也容易出错。 !(http://static.oschina.net/uploads/space/2016/0830/154134_7hjx_1398304.png)在src/main/profiles下面设置不同环境的配置文件例如db