8.通用Mapper常见接口方法
1.select方法
@Test /** * 根据属性值进行查询,以等号方式查询 * <p>Title: testSelect</p> * <p>Description: select * from user where id=110</p> * @throws Exception */ public void testSelect() throws Exception { /* 查询id=100的数据 User u = new User(); u.setId(110); userMapper.select(u); */ // user表中所有的数据信息 List<User> list = userMapper.select(null); System.out.println(list); }
2.按主键查询
@Test /** * 按表中主键查询数据 * <p>Title: testselectByPrimaryKey</p> * <p>Description: </p> * @throws Exception */ public void testSelectByPrimaryKey() throws Exception { User u = userMapper.selectByPrimaryKey(6); System.out.println(u); }
3.查询表中所有的数据
/** * 查询user表中所有数据 * <p>Title: testSelectAll</p> * <p>Description: </p> * @throws Exception */ @Test public void testSelectAll() throws Exception { List<User> list = userMapper.selectAll(); System.out.println(list); }
4.查询单条记录
/** * 根据条件查询单条数据 * <p>Title: testSelectOne</p> * <p>Description: </p> * @throws Exception */ @Test public void testSelectOne() throws Exception { User u=new User(); u.setId(2); User user = userMapper.selectOne(u); System.out.println(user); }
5.根据属性值查询总记录数
@Test /** * 根据属性值查询数据总数 * <p>Title: testSelectCount</p> * <p>Description: </p> * @throws Exception */ public void testSelectCount() throws Exception { User u=new User(); u.setFlag(1);//查询正常用户信息 1=正常 0=软删除 int count = userMapper.selectCount(u); System.out.println("共有用户:"+count); }
6.保存数据
@Test /** * 保存用户 * <p>Title: testInsert</p> * <p>Description: </p> */ public void testInsert() { User u=new User(); u.setName("kkkkk"); u.setAge(30); u.setFlag(1); userMapper.insert(u); // insert()如果属性null为空 也会保存 System.out.println(u); userMapper.insertSelective(u);//如果属性null则不会保存 }
修改
updateByPrimaryKey
updateByPrimaryKeySelective
删除
delete
deleteByPrimaryKey

浙公网安备 33010602011771号