springboot------整合mybatis

1.注解版

0.配置文件

spring:
  datasource:
    username: root
    password: 123456
#    ?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8&useUnicode=true&useSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver
#    指定数据源类型
    type: com.alibaba.druid.pool.DruidDataSource
    #   数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#    filters: stat,wall,log4j
    filters:
        commons-log.connection-logger-name: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#自动生成表结构
#    schema:
#            - classpath:sql/department.sql
#            - classpath:sql/employee.sql
#    initialization-mode: ALWAYS





1.pom文件

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

2.实体类

package com.atguigu.springbootmybaties.bean;

/**
 * 作者:
 * 创建时间:2019/7/15
 * 项目名字:com.atguigu.springbootmybaties.bean
 * 版本:1.0
 **/


public class Employee {

    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;
    private Integer d_id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Integer getD_id() {
        return d_id;
    }

    public void setD_id(Integer d_id) {
        this.d_id = d_id;
    }
}

3.mapper文件

@Mapper//(或者在启动器上面写个注册---@MapperScan(basePackages = {"com.atguigu.springbootmybaties.mapper"}))
public interface EmployeeMapper {

    @Select("select * from employee where id=#{id}")
    public Employee getEmployeeById(Integer id);

}

4.Controller

package com.atguigu.springbootmybaties.controller;

import com.atguigu.springbootmybaties.bean.Employee;
import com.atguigu.springbootmybaties.mapper.DepMapper;
import com.atguigu.springbootmybaties.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者:
 * 创建时间:2019/7/16
 * 项目名字:com.atguigu.springbootmyServicebaties.controller
 * 版本:1.0
 **/

@RestController
public class DeptController {

    @Autowired
    EmployeeMapper employeeMapper;
    @GetMapping(value = "/demp/{id}")
    public Employee getEmployee(@PathVariable(value = "id") Integer id){
        return this.employeeMapper.getEmployeeById(id);
    }

}

二.配置版本(只写注释版的区别)

1.全局配置文件

mybatis:
##全局配置文件的位置
    config-location: classpath:mybatis/mybatis-config.xml
###  mapper文件
    mapper-locations: classpath:mybatis/mapper/*.xml

2.mapper文件

public interface DepMapper {
    public Employee getEmployeeById(Integer id);
}

3.Controller文件

package com.atguigu.springbootmybaties.controller;

import com.atguigu.springbootmybaties.bean.Employee;
import com.atguigu.springbootmybaties.mapper.DepMapper;
import com.atguigu.springbootmybaties.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者:
 * 创建时间:2019/7/16
 * 项目名字:com.atguigu.springbootmyServicebaties.controller
 * 版本:1.0
 **/

@RestController
public class DeptController {

    @Autowired
    DepMapper depMapper;


    @GetMapping(value = "/demp1/{id}")
    public Employee getEmployee1(@PathVariable(value = "id") Integer id){
        System.out.println("我是配置文件的");
        return this.depMapper.getEmployeeById(id);
    }


}

最重要的在启动器上加注解

@MapperScan(basePackages = {"com.atguigu.springbootmybaties.mapper"})【mapper接口的包的位置】

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值