spring-boot 2.1.3整合Mybatis

本文详细介绍了如何在Spring Boot项目中整合MyBatis,包括添加依赖、配置数据源、编写Mapper接口、使用注解版和配置文件版进行操作。通过示例展示了自定义配置规则、批量扫描Mapper接口的方法,以及如何实现数据的增删改查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、注解版

1.添加依赖

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

2.配置数据源
3.写mapper接口

package com.edward.springbootmybaties.mapper;

import com.edward.springbootmybaties.bean.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

    @Select("select * from user where id=#{id}")
    public User getUserById(Integer id);

    @Delete("delete * from user where id=#{id}")
    public int deleteUserById(Integer id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user(username,password) values(#{username},#{password})")
    public int insertUser(User user);

    @Update("update user set username=#{username},password=#{password}")
    public int updateUser(User user);


}

4.使用


	//UserMapper使用的是@Mapper注解,IDEA会提示错误UserMapper找不到,但不影响运行
    @Autowired
    UserMapper userMapper;

    @ResponseBody
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        User user = userMapper.getUserById(id);
        return user;
    }

    @ResponseBody
    @GetMapping("/user")
    public User insertUser(User user){
        int i = userMapper.insertUser(user);
        return user;
    }

5.自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer

@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer(){
            @Override
            public void customize(Configuration configuration) {
            	//开启驼峰命名法
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

6.使用MapperScan批量扫描所有的Mapper接口,这样不用在每个Mapper文件上加@Mapper注解

@MapperScan(basePackages = "com.edward.sprignbootmybaties.mapper")
@SpringBootApplication
public class SpringBootMybatiesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatiesApplication.class, args);
    }

}

二、 配置文件版

创建mybatis-config.xml和xxxMapper.xml文件
在这里插入图片描述
mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    //开启驼峰命名
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

xxxMapper.xml文件

<?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.atguigu.springboot.mapper.EmployeeMapper">
    <!--    public Employee getEmpById(Integer id);

     public void insertEmp(Employee employee);-->
    <select id="getEmpById" resultType="com.atguigu.springboot.bean.Employee">
        SELECT * FROM employee WHERE id=#{id}
    </select>

    <insert id="insertEmp">
        INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{dId})
    </insert>
</mapper>

在application.yml文件中

mybatis:
  config‐location: classpath:mybatis/mybatis‐config.xml 指定全局配置文件的位置
  mapper‐locations: classpath:mybatis/mapper/*.xml  指定sql映射文件的位置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值