Mybatis - 快速入门

简介: Mybatis - 快速入门
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.2.0</version>
</dependency>
package com.imooc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@MapperScan(basePackages = "com.imooc.dataobject.mapper")
@EnableCaching
public class SellApplication {
  public static void main(String[] args) {
    SpringApplication.run(SellApplication.class, args);
  }
}
  • @MapperScan 注解必须加上。    
package com.imooc.dataobject.mapper;
import com.imooc.dataobject.ProductCategory;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;
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);
    ProductCategory selectByCategoryType(Integer categoryType);
}
<?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>
目录
相关文章
|
6月前
|
SQL Java 数据库连接
MyBatis快速入门以及环境搭建和CRUD的实现
MyBatis快速入门以及环境搭建和CRUD的实现
28 0
|
6月前
|
SQL Java 关系型数据库
Mybatis知识【Mybatis快速入门】第二章
Mybatis知识【Mybatis快速入门】第二章
|
2月前
|
SQL Java 数据库连接
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门
48 0
|
8月前
|
Java 关系型数据库 MySQL
MyBatis(简化数据库操作的持久层框架)--快速入门[上]
MyBatis(简化数据库操作的持久层框架)--快速入门[上]
64 0
|
9月前
|
Java 关系型数据库 MySQL
SpringBoot使用Mybatis 快速入门
SpringBoot使用Mybatis 快速入门
75 0
SpringBoot使用Mybatis 快速入门
|
4月前
|
XML 算法 Java
MyBatis-Plus 实战教程一 快速入门(二)
MyBatis-Plus 实战教程一 快速入门
47 0
|
4月前
|
Java 关系型数据库 数据库连接
MyBatis-Plus 实战教程一 快速入门(一)
MyBatis-Plus 实战教程一 快速入门
48 0
|
6月前
|
XML SQL 算法
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门(下)
3.常见注解 在刚刚的入门案例中,我们仅仅引入了依赖,继承了BaseMapper就能使用MybatisPlus,非常简单。但是问题来了: MybatisPlus如何知道我们要查询的是哪张表?表中有哪些字段呢?
69 0
|
6月前
|
SQL Java 数据库连接
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门(上)
大家在日常开发中应该能发现,单表的CRUD功能代码重复度很高,也没有什么难度。而这部分代码量往往比较大,开发起来比较费时。
49 0
|
7月前
|
SQL Java 数据库连接
Mybatis快速入门
Mybatis快速入门
28 0