Spring-boot整合Mybaties

1.引入需要的启动器(添加pom.xml依赖

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

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

 

2.配置数据源 (application.yaml) 3.配置mybatis

spring:
  datasource:
    username: root
    password: 000000
    url: jdbc:mysql://localhost:3306/***?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    platform: mysql
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  type-aliases-package: com/example/demo/model
  configuration:
    map-underscore-to-camel-case: true

4.编写实体类

package com.example.demo.model;

import java.util.Date;

public class Admin {
    private int Id;
    private  String Name;
    private String Passworld;
    private String Realname;
    private String sex;
    private int age;
    private String address;
    private String tel;
    private String addtime;
    private String sf;//身份

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getPassworld() {
        return Passworld;
    }

    public void setPassworld(String passworld) {
        Passworld = passworld;
    }

    public String getRealname() {
        return Realname;
    }

    public void setRealname(String realname) {
        Realname = realname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getAddtime() {
        return addtime;
    }

    public void setAddtime(String addtime) {
        this.addtime = addtime;
    }

    public String getSf() {
        return sf;
    }

    public void setSf(String sf) {
        this.sf = sf;
    }

    @Override
    public String toString() {
        return "Admin{" +
                "Id=" + Id +
                ", Name='" + Name + '\'' +
                ", Passworld='" + Passworld + '\'' +
                ", Realname='" + Realname + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", tel='" + tel + '\'' +
                ", addtime='" + addtime + '\'' +
                ", sf='" + sf + '\'' +
                '}';
    }
}

  

5.编写mapper映射接口

import com.example.demo.model.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface adminDao {
    @Select("select * from admin where id=#{id}")
    Admin getAdmin(int id);
}

  

6.编写代码测试

package com.example.demo;

import com.example.demo.dao.adminDao;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {
    @Resource
    adminDao adminDao;
    @Test
    void contextLoads() {
        System.out.println(adminDao.getAdmin(1));

    }

}

  

junit  contextLoads() 

posted @ 2020-03-05 16:07  小帅Tianjs  阅读(60)  评论(0编辑  收藏  举报