014mybatis注解方式使用

博客介绍了MyBatis的配置步骤,包括引入依赖、添加mapper、添加mapper扫描、输出sql执行语句(在application.yml添加参数)以及配置添加xml等内容,为使用MyBatis进行开发提供了清晰的操作指引。
一、引入依赖
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.2.0</version>
</dependency>
二、添加mapper
public interface ProductCategoryMapper {

    //map方式插入
    @Insert("insert into product_category(category_name,category_type)
    values(#{category_name,jdbcType=VARCHAR},#{category_type,jdbcType=INTEGER})")
    int insertByMap(Map<String,Object> map);

    //实体类方式插入
    @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);

    @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")
    })
    ProductCategory findByCategoryName(String categoryName);

    @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 productCategory);

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

}

三、添加mapper扫描
@SpringBootApplication
@MapperScan(basePackages ="com.sell.dataobject.mapper" )
public class SellApplication {

	public static void main(String[] args) {
		SpringApplication.run(SellApplication.class, args);
	}
}
四、其他 输出sql执行语句

application.yml添加参数

logging:
  level:
    com.sell.dataobject.mapper : trace # mybatis输出SQL语句

五、配置添加xml
mybatis:
  mapper-locations: classpath:mapper/*.xml  # 配置xml引入:classpath就是resource目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值