一、引入依赖
<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目录