千峰商城-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); }