MyBatis注解版使用
1.导入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2.创建mapper层
package com.sell.dataobject.mapper;
import com.sell.dataobject.ProductCategory;
import org.apache.ibatis.annotations.*;
import java.util.Map;
public interface ProductCategoryMapper {
//以map为参数#{}里面的第一个参数为map中value对应的key,第二个参数jdbcType=VARCHAR为字段在数据库的格式
@Insert("insert into product_category(category_name,category_type) values(#{category_name,jdbcType=VARCHAR},#{category_type,jdbcType=INTEGER})")
int insertByMap(Map<String,Object> map);
//以对象为参数#{}里面的第一个参数为对象的属性,第二个参数jdbcType=VARCHAR为字段在数据库的格式
@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);
@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 category);
@Delete("delete from product_category where category_type = #{categoryType}")
int deleteByCategoryType(Integer categoryType);
}
(1)insertByMap使用 @INSERT注解的方式在后面填写sql语句,这里以map的形式入参,所以"#“号后面的”{}“第一个参数是map里面的key;入参方式用”#{}"填写参数,并且需要使用"jdbcType"指定字段在数据库的格式;
3.启动类添加@MapperScan注解扫描mapper层
package com.sell;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.sell.dataobject.mapper")
public class SellApplication {
public static void main(String[] args) {
SpringApplication.run(SellApplication.class, args);
}
}
使用@MapperScan注解扫描mapper所在的包;
4.新增测试类
package com.sell.dataobject.mapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
class ProductCategoryMapperTest {
@Autowired
private ProductCategoryMapper mapper;
@Test
void insertByMap() {
Map<String,Object> map = new HashMap<>();
map.put("category_name","少女最爱");
map.put("category_type",10);
int result = mapper.insertByMap(map);
if (result > 0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
}
}
创建hashMap,以map形式入参,运行;