springboot整合mybatis,使用注解实现增删改查

数据库默认是mysql,切已经引入好依赖了,这里就不对mysql咋配置做介绍了

首先肯定还是先引入依赖了

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.2.0</version>
</dependency>

然后定义增删改查的mapper类

定义的核心在与字段和表字典要一一对应好

package com.chan.wechatshop.dataobject.mapper;

import com.chan.wechatshop.dataobject.ProductCategory;
import org.apache.ibatis.annotations.Insert;

import java.util.Map;

public interface ProductCategoryMapper {
    
    

    //后面value中的categoryName和categoryType是变量名,要和传入的map中的key值保持一致
    @Insert("insert into product_category(category_name,category_type) values(#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
    int insertByMap(Map<String,Object> map);

    //同样后面values中的的categoryName和categoryType变量要和对象中对应的属性名一致
    @Insert("insert into product_category(category_name,category_type) values(#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
    //useGeneratedKeys=true:获取数据库生成的主键 keyProperty="id":把主键值存入ProductCategory对象的id属性
    @Options(useGeneratedKeys=true, keyProperty="id") 
    int insertByObject(ProductCategory productCategory);

}

最后要在application启动类中将mapper类扫描进去

在这里插入图片描述

select

select one and select list

package com.chan.wechatshop.dataobject.mapper;

import com.chan.wechatshop.dataobject.ProductCategory;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

public interface ProductCategoryMapper {
    
    

    //后面value中的categoryName和categoryType是变量名,要和传入的map中的key值保持一致
    @Insert("insert into product_category(category_name,category_type) values(#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
    int insertByMap(Map<String,Object> map);

    //同样后面values中的的categoryName和categoryType变量要和对象中对应的属性名一致
    @Insert("insert into product_category(category_name,category_type) values(#{categoryName,jdbcType=VARCHAR},#{categoryType,jdbcType=INTEGER})")
    int insertByObject(ProductCategory productCategory);


	//Results中的Result中将表中字段映射到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);



	@Select("select * from product_category where category_name = #{categoryName}")
    @Results({
    
    
            @Result(column = "category_id",property = "categoryId"),
            @Result(column = "category_name",property = "categoryName"),
            @Result(column = "category_type",property = "categoryType")
    })
    List<ProductCategory> findByCategoryName(String categoryName);


@Select({
    
    
            "<script>",
            "select",
            "c.cust_id, plat_cust_id, plat_cust_name, cust_map_id, cust_type, is_merchant, ",
            "create_time, create_opt_id, p.cust_name, p.tel, p.id_type, p.id_no ",
            "from personal_customer p",
            "left join customer c",
            "on c.cust_id = p.cust_id",
            "where p.cust_id in ",
                "<foreach item='item' index='index' collection='ids'",
                "open='(' separator=',' close=')'>",
                "#{item}",
                "</foreach>",
            "</script>"
    })
    @ResultMap("com.cloud.customer.dao.PersonalCustomerMapper.PersonDOResultMap")
    List<PersonInfoDO> selectRecordsByIds(@Param("ids") String[] ids);
}


}

update

在上面的ProductCategoryMapper中增加一个update方法

在一个一个传参数的时候要加上@Param注解
也可以传一个对象进去,字段名和实体类的属性名要对应好

@Update("update product_category set category_name = #{name} where category_type = #{type}")
int updateNameByType(@Param("name") String name,@Param("type") Integer type);



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

delete

@Delete("delete from product_category where category_type = #{type}")
int deleteByType(@Param("type") Integer type);

如果要查看mybatis运行时的具体sql可以利用打印输出的日志等级来实现

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43944305/article/details/105754975