MyBatis注解版使用

这篇博客介绍了如何使用MyBatis注解版进行开发,包括导入相关依赖、创建mapper层并使用@INSERT注解编写SQL,启动类中添加@MapperScan注解扫描mapper,以及创建测试类进行功能验证。

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

MyBatis注解版使用

1.导入依赖

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

2.创建mapper层

package com.sell.dataobject.mapper;

import com.sell.dataobject.ProductCategory;
import org.apache.ibatis.annotations.*;

import java.util.Map;

public interface ProductCategoryMapper {

    //以map为参数#{}里面的第一个参数为map中value对应的key,第二个参数jdbcType=VARCHAR为字段在数据库的格式
    @Insert("insert into product_category(category_name,category_type) values(#{category_name,jdbcType=VARCHAR},#{category_type,jdbcType=INTEGER})")
    int insertByMap(Map<String,Object> map);

    //以对象为参数#{}里面的第一个参数为对象的属性,第二个参数jdbcType=VARCHAR为字段在数据库的格式
    @Insert("insert into product_category(category_name,category_type) values(#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
    int insertByObject(ProductCategory productCategory);

    //以单个变量入参,#{}里面就是方法的参数
    @Select("select * from product_category where category_type = #{categoryType}")
    @Results({  //需要指定返回的结果数据库字段对应的实体属性
            @Result(column = "category_id",property = "categoryId"),
            @Result(column = "category_name",property = "categoryName"),
            @Result(column = "category_type",property = "categoryType")
    })
    ProductCategory findByCategoryType(Integer categoryType);

    @Update("update product_category set category_name = #{categoryName} where category_type = #{categoryType}")
    int updateByCategoryType(@Param("categoryName") String categoryName,
                             @Param("categoryType") Integer categoryType);

    @Update("update product_category set category_name = #{categoryName} where category_type = #{categoryType}")
    int updateByObject(ProductCategory category);

    @Delete("delete from product_category where category_type = #{categoryType}")
    int deleteByCategoryType(Integer categoryType);
}


(1)insertByMap使用 @INSERT注解的方式在后面填写sql语句,这里以map的形式入参,所以"#“号后面的”{}“第一个参数是map里面的key;入参方式用”#{}"填写参数,并且需要使用"jdbcType"指定字段在数据库的格式;

3.启动类添加@MapperScan注解扫描mapper层

package com.sell;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "com.sell.dataobject.mapper")
public class SellApplication {

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

}

使用@MapperScan注解扫描mapper所在的包;

4.新增测试类

package com.sell.dataobject.mapper;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
class ProductCategoryMapperTest {

    @Autowired
    private ProductCategoryMapper mapper;

    @Test
    void insertByMap() {
        Map<String,Object> map = new HashMap<>();
        map.put("category_name","少女最爱");
        map.put("category_type",10);
       int result = mapper.insertByMap(map);
       if (result > 0){
           System.out.println("添加成功!");
       }else{
           System.out.println("添加失败!");
       }
    }
}

创建hashMap,以map形式入参,运行;
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值