springboot整合mybatis

文章目录

  • 整合Mybatis框架
    • 引入依赖
    • 配置配置文件
    • 加入mybatis配置(指定mybatis的文件的目录)
    • 建立数据表
    • 开发实体类
    • 开发mapper层接口以及Mapper
    • 开发Service层接口
    • 开发Controller
    • 启动项目访问测试

整合Mybatis框架

引入依赖

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.2.4</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>

<!--整合mybatis-->
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.1.4</version>
</dependency>

配置配置文件

server:
  port:8080
servlet:
    context-path: /springboot_test3
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #制定连接池
    driver-class-name: com.mysql.jdbc.Driver        #指定驱动
    url: jdbc:mysql://localhost:3306/?characterEncoding=UTF-8           #指定url
    username: root									#指定用户名
    password: roo

加入mybatis配置(指定mybatis的文件的目录)

#配置文件中加入如下配置:
mybatis:
  mapper-locations: classpath:com/demo/mapper/*.xml  #指定mapper配置文件位置
  type-aliases-package: com.demo.entity              #指定起别名所在包
//入口类中加入如下配置:
@SpringBootApplication
@MapperScan("com.demo.mapper")   //必须在入口类中加入这个配置 扫描指定的目录。
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

建立数据表

CREATE TABLE `student` (
  `id` varchar(40) NOT NULL,
  `name` varchar(80) DEFAULT NULL,
  `no` varchar(90) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

开发实体类

package com.demo.entity;

import java.util.Date;

public class Student {
    private Integer id;
    private String name;
    private Date birthday;
    private Double salary;

    public Student(Integer id, String name, Date birthday, Double salary) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }
}

开发mapper层接口以及Mapper

public interface StudentMapper {
    public List<Student> findAll();
}


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.mapper.StudentMapper">

    <select id="findAll" resultType="com.demo.entity.Student">
        select *from student
    select>
mapper>

开发Service层接口

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    public List<Student> findAll() {
        return studentMapper.findAll();
    }
}

开发Controller

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;
    @RequestMapping("findAll")
    public List<Student> findAll(){

        return studentService.findAll();
    }
}

启动项目访问测试

http://localhost:8989/项目名/findAll

@SpringBootApplication
@MapperScan("com.demo.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

springboot整合mybatis_第1张图片

你可能感兴趣的:(spring,boot,mybatis,后端)