springboot+mybatis

springboot整合mybatis

一.建立springboot項目java

二.配置文件: application.ymlmysql

#公共配置與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文件web

<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>

四.啓動類加註解spring

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

五.建立Controller,啓動測試sql

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;
    }
}
相關文章
相關標籤/搜索