Spring Boot 中使用 tkMapper( 二 )

2、创建 dao 接口,继承 GeneralDao 即可!
public interface UserDao extends GeneralDao<User> {}4. 测试添加 Junit 和 springboot test 两个测试依赖:
<!-- junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!-- springboot test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>写测试类进行测试:
@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringbootTkMapperDemoApplication.class) //启动类.classpublic class UserDaoTest {@Autowiredprivate UserDao userDao; //如果爆红线不用管(或Dao接口上添加@Repository注解)@Testpublic void test() {User user = new User();user.setUserName("mike");user.setUserPwd("123");user.setUserRealname("zhangsan");user.setUserImg("user/default.jpg");int i = userDao.insert(user);System.out.println("========> i = " + i);}}tkMapper 常用方法之增删改

  • insert:普通添加
  • insertUseGeneratedKeys:可返回自增 id 的添加
  • updateByPrimaryKey:根据主键修改
  • deleteByPrimaryKey:根据主键删除
@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringbootTkMapperDemoApplication.class) //启动类.classpublic class UserDaoTest {@Autowiredprivate UserDao userDao; //如果爆红线不用管(或Dao接口上添加@Repository注解)@Testpublic void testInsert() {User user = new User();user.setUserName("juno4");user.setUserPwd("321");user.setUserRealname("lin");user.setUserImg("user/default.jpg");/*** insert: 添加(自增的id不会返回)*/int i = userDao.insert(user);System.out.println("========> i = " + i);System.out.println(user.getUserId()); //null}@Testpublic void testInsertUseGeneratedKeys() {User user = new User();user.setUserName("juno3");user.setUserPwd("321");user.setUserRealname("lin");user.setUserImg("user/default.jpg");/*** insertUseGeneratedKeys: 添加(自增的id可以返回)* 注意:*1. 数据库中主键字段需要设置为自增*2. 实体类中主键属性需要使用@Id注解指定;并且需要使用包装类型Integer,不要使用int*/int i = userDao.insertUseGeneratedKeys(user);System.out.println("========> i = " + i);System.out.println(user.getUserId()); //10}@Testpublic void testUpdateByPrimaryKey() {User user = new User();user.setUserId(10); //必须指定要修改的iduser.setUserName("juno new");user.setUserPwd("000");user.setUserRealname("lin new");user.setUserImg("new.jpg");/*** updateByPrimaryKey:根据主键修改*/int i = userDao.updateByPrimaryKey(user);System.out.println("========> i = " + i);System.out.println(user);}@Testpublic void testDeleteByPrimaryKey() {/*** deleteByPrimaryKey:根据主键删除*/int i = userDao.deleteByPrimaryKey(9);System.out.println("========> i = " + i);}}PS:其实还有根据自定义条件修改或删除的方法(使用方法参考带条件的查询示例)
tkMapper 常用方法之查询
  • selectAll:查所有
  • selectByPrimaryKey:根据主键查所有
  • selectByExample:根据条件查所有
  • selectByRowBounds:分页查询
  • selectByExampleAndRowBounds:带条件的分页查询
  • selectCount:查总记录数
  • selectCountByExample:根据条件查总记录数
@RunWith(SpringRunner.class)@SpringBootTest(classes = SpringbootTkMapperDemoApplication.class) //启动类.classpublic class UserDaoTest {@Autowiredprivate UserDao userDao; //如果爆红线不用管(或Dao接口上添加@Repository注解)@Testpublic void testSelectAll() {/*** selectAll:查询所有*/List<User> users = userDao.selectAll();for (User user : users) {System.out.println(user);}}@Testpublic void testSelectByPrimaryKey() {/*** selectByPrimaryKey:根据主键查询*/User user = userDao.selectByPrimaryKey(10);System.out.println(user);}@Testpublic void testSelectByExample() {//封装查询条件Example example = new Example(User.class);Example.Criteria criteria = example.createCriteria();//条件信息(根据Criteria对象的各种方法进行设置)criteria.andEqualTo("userRealname", "lin");// criteria.orEqualTo("userPwd", "123");// criteria.andLike("userName", "%i%");/*** selectByPrimaryKey:根据条件查询(PS:根据条件修改或删除与此类似)*注意:需要设置查询条件信息,并传入条件对象*/List<User> users = userDao.selectByExample(example);for (User user : users) {System.out.println("========> " + user);}}@Testpublic void testSelectByRowBounds() {//分页查询信息int pageNum = 2; //第几页int pageSize = 3; //每页显示多少行int start = (pageNum - 1) * pageSize; //起始显示的下标RowBounds rowBounds = new RowBounds(start, pageSize);/*** selectByRowBounds:查所有的分页查询*/List<User> users = userDao.selectByRowBounds(new User(), rowBounds);for (User user : users) {System.out.println("========> " + user);}/*** selectCount:查询总记录数*/int count = userDao.selectCount(new User());System.out.println("========> count = " + count);}@Testpublic void testSelectByExampleAndRowBounds() {//封装查询条件Example example = new Example(User.class);Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("userRealname", "lin");//分页查询信息int pageNum = 2; //第几页int pageSize = 2; //每页显示多少行int start = (pageNum - 1) * pageSize; //起始显示的下标RowBounds rowBounds = new RowBounds(start, pageSize);/*** selectByExampleAndRowBounds:带条件的分页查询*/List<User> users = userDao.selectByExampleAndRowBounds(example, rowBounds);for (User user : users) {System.out.println("========> " + user);}/*** selectCountByExample:根据条件查询总记录数*/int count = userDao.selectCountByExample(example);System.out.println("========> count = " + count);}}

推荐阅读