欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot整合mybatis(配置文件)

程序员文章站 2022-07-12 22:42:57
...

结构目录
springboot整合mybatis(配置文件)
依赖`

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

数据库
springboot整合mybatis(配置文件)
写代码,1,配置文件application.yml

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456

mybatis:
  type-aliases-package: com.wan.pojo
  mapper-locations: classpath:mapper/*.xml

2,实体类

package com.wan.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private int age;
    private String sex;
}

dao层

package com.wan.dao;

import com.wan.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;


@Repository //加入ioc容器
@Mapper
public interface StudentMapper {
    public int addStudent(Student student);
}

controller层

package com.wan.controller;

import com.alibaba.fastjson.JSON;
import com.wan.dao.StudentMapper;
import com.wan.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class BmController {

    @Autowired()
    StudentMapper studentMapper;
    @RequestMapping("/a")
    public String allStudents(){
        Student student= new Student();
        student.setName("大哈");
        student.setAge(6);
        student.setSex("男");
       int result = studentMapper.addStudent(student);
        System.out.println(result);
        return result>0?"插入成功!":"插入失败";

    }

}

运行结果:
springboot整合mybatis(配置文件)
查看数据库是否插入(由于我多次操作,没改值)
springboot整合mybatis(配置文件)