springboot整合mybatis

1.导入依赖和数据库驱动(新建项目的时候若是有勾选可以跳过这一步)
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>
2.配置数据源和mybatis
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test1
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  #config-location: classpath:mybatis/mybatis-config.xml #全局配置文件
  mapper-locations: classpath:mapper/*.xml #全局映射文件
  configuration: #和全局配置文件冲突,可以不写全局配置文件,所有配置写在configuration下面
    map-underscore-to-camel-case: true #驼峰命名策略
3.编写Mapper接口
package com.szg.boot.mapper;
import com.szg.boot.bean.Pet;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

//若是在主程序入口配置@MapperScan("com.szg.boot.mapper"),这个注解可以不用配置
@Mapper
public interface PetMapper {

    //注解版
    @Select("select * from  pet where  id=#{id}")
    public Pet getPet(Long id);

    //配置文件版
    public void insertPet(Pet pet);
}
4.编写Mapper映射配置文件
<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.szg.boot.mapper.PetMapper">
    <!--配置自增属性也返回-->
    <insert id="insertPet" useGeneratedKeys="true" keyProperty="id">
        insert into pet(`name`,`weight`) values(#{name}, #{weight})
    </insert>

</mapper>
5.编写service类
package com.szg.boot.service;
import com.szg.boot.bean.Pet;
import com.szg.boot.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PetService {

    @Autowired
    PetMapper petMapper;

    public Pet getPetById(Long id) {
        return petMapper.getPet(id);
    }

    public void savePet(Pet pet) {
        petMapper.insertPet(pet);
    }
}
6.编写Controller接口
package com.szg.boot.controller;
import com.szg.boot.bean.Pet;
import com.szg.boot.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PetController {

    @Autowired
    PetService petService;

    @GetMapping("pet")
    public Pet getById(@RequestParam("id") Long id) {
        return petService.getPetById(id);
    }

    @PostMapping("savePet")
    public Pet savePet(Pet pet) {
        petService.savePet(pet);
        return pet;
    }
}
注:controller调用service,service调用mapper
目录结构.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容