Mybatis分页助手和通用Mapper的使用

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 单表的增删改查,支持单表操作,不支持通用的多表联合查询

优点:

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、源码下载


  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MyBatis Plus提供了通用Mapper和Service,可以减少重复的CRUD操作代码,提高开发效率。 通用Mapper是指通过MyBatis Plus提供的基础Mapper接口和默认实现类,可以通过简单的方法调用完成常见的单表CRUD操作,无需手写SQL语句。这些方法包括insert、insertBatch、deleteById、deleteBatchIds、updateById、update、selectById、selectBatchIds、selectList、selectOne等等。使用通用Mapper可以大大简化DAO层的代码,提高开发效率。以下是一个使用通用Mapper的示例代码: ```java public interface UserMapper extends BaseMapper<User> { } ``` ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 通用Service是指通过MyBatis Plus提供的基础Service接口和默认实现类,可以通过简单的方法调用完成常见的单表CRUD操作,无需手写SQL语句。通用Service继承自通用Mapper,并且提供了一些额外的方法,如分页查询、批量插入、批量更新、批量删除等等。使用通用Service可以进一步简化Service层的代码,提高开发效率。以下是一个使用通用Service的示例代码: ```java public interface UserService extends IService<User> { } ``` ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 使用通用Mapper和Service需要注意的是,在实体类中需要使用注解@TableId来指定表的主键字段,以及注解@TableField来指定实体类属性和数据库表字段的映射关系。另外,如果需要使用分页查询,则需要在配置文件中配置分页插件。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值