一、第一步想引入mybatis包
二、寫mapper文件
注:
@Results({
@Result(column = "category_id", property = "categoryId"),
@Result(column = "category_name", property = "categoryName"),
@Result(column = "category_type", property = "categoryType")
})
中@Results注解是設置希望返回的值,沒有設置的不會返回。
傳多參數時,要用到@Param("*")
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);
}
在測試類中檢驗
package com.imooc.mapper;
import com.imooc.dataobject.ProductCategory;
import com.imooc.dataobject.mapper.ProductCategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.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.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryMapperTest {
@Autowired
private ProductCategoryMapper mapper;
@Test
public void insertByMap() throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("categoryName", "師兄最不愛");
map.put("category_type", 101);
int result = mapper.insertByMap(map);
Assert.assertEquals(1, result);
}
@Test
public void insertByObject() {
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("師兄最不愛");
productCategory.setCategoryType(102);
int result = mapper.insertByObject(productCategory);
Assert.assertEquals(1, result);
}
@Test
public void findByCategoryType() {
ProductCategory result = mapper.findByCategoryType(102);
Assert.assertNotNull(result);
}
@Test
public void findByCategoryName() {
List<ProductCategory> result = mapper.findByCategoryName("師兄最不愛");
Assert.assertNotEquals(0, result.size());
}
@Test
public void updateByCategoryType() {
int result = mapper.updateByCategoryType("師兄最不愛的分類", 102);
Assert.assertEquals(1, result);
}
@Test
public void updateByObject() {
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("師兄最不愛");
productCategory.setCategoryType(102);
int result = mapper.updateByObject(productCategory);
Assert.assertEquals(1, result);
}
@Test
public void deleteByCategoryType() {
int result = mapper.deleteByCategoryType(102);
Assert.assertEquals(1, result);
}
@Test
public void selectByCategoryType() {
ProductCategory productCategory = mapper.selectByCategoryType(101);
Assert.assertNotNull(productCategory);
}
}
三、在啟動的主類SellApplication上配置mapper文件的掃描
路徑,即增加注解@MapperScan(basePackages=*[mapper文件夾的路徑])
四、如果你要在控制台上查到mysql的語句,可以在application.yml上加入以下配置: