1、Mybatis分页助手的简介
1.1Mybatis分页助手简介
1.2 详细介绍
https://www.oschina.net/p/mybatis_pagehelper
2、Mybatis分页助手的使用
2.1 导入Maven的依赖
<!-- Mybatis的分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
2.2 Mybatis的全局配置文件
<configuration>
<!--Mybatis的拦截器 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql" />
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询,也就是是否查询数据总条数 -->
<property name="rowBoundsWithCount" value="true" />
</plugin>
</plugins>
</configuration>
3、在Java程序中,设置分页参数以及获取分页信息
注意,userMapper.queryUserList(),本身并没有实现分页查询。是在使用了Mybatis分页助手后,实现了分页查询。
4、Mybatis分页助手使用的注意事项
5、Mybatis 通用 Mapper 详细介绍
Mybatis 通用 Mapper
-
极其方便的使用 Mybatis 单表的增删改查,支持单表操作,不支持通用的多表联合查询
优点:
-
通用 Mapper 可以极大的方便开发人员。
-
Mybatis 通用 Mapper 详细介绍: http://www.oschina.net/p/mybatis-mapper
6、Mybatis通用Mapper的使用
6.1 加入Maven依赖
<!-- 通用Mapper -->
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>2.3.4</version>
</dependency>
6.2 配置通用Mapper拦截器
<configuration>
<!--Mybatis的拦截器 -->
<plugins>
<!--Mybatis分页助手 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql" />
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询,也就是是否查询数据总条数 -->
<property name="rowBoundsWithCount" value="true" />
</plugin>
<!--Mybatis通用Mapper -->
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!-- 主键自增回写方法,默认值MYSQL,详细说明请看文档 -->
<property name="IDENTITY" value="MYSQL" />
<!-- 通用Mapper接口,多个通用接口用逗号隔开 -->
<property name="mappers" value="com.github.abel533.mapper.Mapper" />
</plugin>
</plugins>
</configuration>
6.3 定义Mapper接口
6.4 实体添加JPA注解
7、通用Mapper的使用测试
7.1 创建Junit测试用例
7.2 完整的测试用例
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.github.abel533.entity.Example;
import cn.itcast.user.mapper.NewUserMapper;
import cn.itcast.user.pojo.User;
public class NewUserMapperTest {
private NewUserMapper newUserMapper;
@Before
public void setUp() throws Exception {
// 完成newUserMapper的初始化
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring/app*.xml");
newUserMapper = context.getBean("newUserMapper", NewUserMapper.class);
}
@Test
public void testSelectOne() {
User user = new User();
user.setId(1L);
user.setAge(30);
User selectOne = newUserMapper.selectOne(user);
System.out.println(selectOne);
}
@Test
public void testSelect() {
List<User> lists = newUserMapper.select(null);
for (User user : lists) {
System.out.println(user);
}
}
@Test
public void testSelectCount() {
System.out.println(newUserMapper.selectCount(null));
}
@Test
public void testSelectByPrimaryKey() {
System.out.println(newUserMapper.selectByPrimaryKey(1L));
}
@Test
public void testInsert() {
}
@Test
public void testInsertSelective() {
}
@Test
public void testDelete() {
}
@Test
public void testDeleteByPrimaryKey() {
}
@Test
public void testUpdateByPrimaryKey() {
}
@Test
public void testUpdateByPrimaryKeySelective() {
}
@Test
public void testSelectCountByExample() {
// 根据多个id查询用户信息
List<Object> ids = new ArrayList<Object>();
ids.add(1);
ids.add(2);
ids.add(3);
Example example = new Example(User.class);
example.createCriteria().andIn("id", ids);
List<User> list = this.newUserMapper.selectByExample(example);
for (User user : list) {
System.out.println(user);
}
}
@Test
public void testDeleteByExample() {
Example example = new Example(User.class);
example.createCriteria().andBetween("age", 80, 90);
newUserMapper.deleteByExample(example);
}
@Test
public void testSelectByExample() {
}
@Test
public void testUpdateByExampleSelective() {
}
@Test
public void testUpdateByExample() {
}
}
8、集成到项目中
9、源码下载