1. 程式人生 > 其它 >千峰商城-springboot專案搭建-13-tkMapper常用方法之增刪改查

千峰商城-springboot專案搭建-13-tkMapper常用方法之增刪改查

1.bean中新建一個實體類Category.java:

 Category.java:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Category{
    private int categoryId;
    private String categoryName;
    private Integer categoryLevel;
    private Integer parentId;
    private String categoryIcon;
    private String categorySlogan;
    
private String categoryPic; private String categoryBgColor; }

 

  2.建立DAO:   GeneralDAO.java:  (一定不能放到mapperScan能掃描到的包中,要建立一個common包或general包放進去)
public interface GeneraDAO<T> extends Mapper<T>, MySqlMapper<T>{

}

UserDAO.java:

public interface UserDAO extends GeneralDAO<User>{
}

CategoryDAO.java:

public interface CategoryDAO extends GeneralDAO<Category>{
}

 

3.測試(增刪改):

CategoryDAOTest.java:

@RunWith(SpringRunner.class)
@SpringBootTest(classes=TkmapperDemoApplication.class)
public class CategoryDAOTest{
    //新增
    @Autowired
    private CategoryDAO categoryDAO;

    @Test
    
public void testInsert(){ Category category = new Category(0,"測試類別1",1,0,"hehe","01.png","hehe","aaa.jpg","black"); int i = categoryDAO.insert(category); assertEquals(1,i); } //修改 @Test public void testUpdate(){ Category category = new Category(46,"測試類別2",1,0,"heihei","02.png","hehe","aaa.jpg","black"); int i = categoryDAO.updateByPrimaryKey(category); assertEquals(1,i); } //刪除 @Test public void testDelete(){ int i = categoryDAO.deleteByPrimaryKey(46); assertEquals(1,i); } }

 

4.測試(查詢):

CategoryDAOTest.java:

    @Test
    //查詢所有
    public void testSelect1(){
    List<Category> category = categoryDAO.selectAll();
    for(Category category:categories){
        System.out.println(category);
    }

    //根據主鍵查詢
    @Test
    public void testSelect2(){
        Category category = categoryDAO.selectByPrimaryKey(45);
        System.out.println(category);
    }

    //條件查詢
    @Test
    public void testSelect3(){
        Example example = new Example(Category.class);//建立一個example物件封裝類別Category查詢條件
        Example.Criteria carteria = example.createCriteria();
        criteria.andEqualTo("categoryLevel",1);//查詢類別等於1的資料
        criteria.orEqualTo("categoryLevel",2);//查詢類別等於1或者2的資料
        //criteria.andNotEqualTo("categoryLevel",1);//查詢類別不等於1的資料

        criteria.andLike("categoryName","%茶%");//查詢商品名中帶有”茶“字的資料

        List<Category> categories = categoryDAO.selectByExample(example);
        for(Category category:categories){
            System.out.println(category);
            }
    }

    //分頁查詢
    @Test
    public void testSelect4(){
        int pageNum=2;
        int pageSize=10;
        int start = (pageNum-1) * pageSize;

        RowBounds rowBounds = new RowBounds(start,pageSize);
        List<Category> category = categoryDAO.selectByRowBounds(new Category() ,rowBounds);
        for(Category category:categories){
            System.out.println(category);
            }

        //查詢總記錄數
        int i = categoryDAO.selectCount(new Category() );
        System.out.println(i);
    }

    //帶條件的分頁查詢
    @Test
    public void testSelect5(){
        Example example = new Example(Category.class);//建立一個example物件封裝類別Category查詢條件
        Example.Criteria carteria = example.createCriteria();
        criteria.andEqualTo("categoryLevel",1);//查詢類別等於1的資料

        int pageNum=1;
        int pageSize=3;
        int start = (pageNum-1) * pageSize;
        RowBounds rowBounds = new RowBounds(start,pageSize);

        List<Category> categories = categoryDAO.selectByExampleAndRowBounds(exemple,rowBounds);
        for(Category category:categories){
            System.out.println(category);
            }
        //查詢滿足條件的總記錄數
        int i = categoryDAO.selectCountByExample(example);
        System.out.println(i);
    }