第一步pom.xml中引入mybatis的包
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency>
第二步
第二步写mapper文件 ,有两种方式,看下文
第三步在配置扫描路径
@SpringBootApplication @MapperScan(basePackages = "com.imooc.dataobject.mapper") @EnableCaching public class SellApplication { public static void main(String[] args) { SpringApplication.run(SellApplication.class, args); } }
第四步 分层思想所以在创建一个dao层
public class ProductCategoryDao { @Autowired ProductCategoryMapper mapper; public int insertByMap(Map<String, Object> map) { return mapper.insertByMap(map); } }
第五步在配置文件中配置日志级别,就可以看到sql语句了
logging: level: com.imooc.dataobject.mapper: trace
1.两种方式 一种注解
public interface ProductCategoryMapper { @Insert("insert into product_category(category_name, category_type) values (#{categoryName, 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") }) List<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 categoryType);
第二种是mybatis xml文件
public interface ProductCategoryMapper { ProductCategory selectByCategoryType(Integer categoryType);}
在resource下面新建文件,在新建*.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.imooc.dataobject.mapper.ProductCategoryMapper" > <resultMap id="BaseResultMap" type="com.imooc.dataobject.ProductCategory"> <id column="category_id" property="categoryId" jdbcType="INTEGER" /> <id column="category_name" property="categoryName" jdbcType="VARCHAR" /> <id column="category_type" property="categoryType" jdbcType="INTEGER" /> </resultMap> <select id="selectByCategoryType" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select category_id, category_name, category_type from product_category where category_type = #{category_type, jdbcType=INTEGER} </select> </mapper>
之后
还需要配置文件中配置
mybatis: mapper-locations: classpath:mapper/*.xml