码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+mybatis集成分页功能

时间:2019-06-03 18:51:51      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:database   agg   extend   color   rtp   mamicode   ssl   项目   help   

1.搭建使用idea搭建srpingboot项目

技术图片

技术图片

技术图片

技术图片

在pom.xml文件中引入如下的依赖:

                <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- swagger文档相关依赖-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.1.2</version>
		</dependency>
		<!-- swagger文档相关依赖上下2个依赖的版本号必须一致-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- mybatis相关依赖-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- 使用阿里的druid数据库连接池-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!-- jdbc相关插件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<version>1.5.19.RELEASE</version>
		</dependency>
		<!-- mysql相关插件-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>

		<!-- 分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- 用来将对象转成json和将json转为对象 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.12</version>
		</dependency>

 spring项目配置文件:application.yml,如果是application.properties,那么建议改成yml格式,配置如下:

server:
  port: 2300
spring:
  application:
    name: run
  datasource:
    druid:
      url: jdbc:mysql://192.168.50.117:23306/shop?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
      username: shop
      password: shop
      filter:
        stat:
          db-type: mysql
          log-slow-sql: true
          slow-sql-millis: 3000
          enabled: true
        wall:
          config:
            multi-statement-allow: true
          enabled: true
      filters: stat,wall #防御SQL注入的filter:wall
      initial-size: 1 #初始化连接数量
      max-active: 10 #最大并发连接数
      min-idle: 1 #最小空闲连接数
      max-wait: 60000 #配置获取连接等待超时的时间
      time-between-eviction-runs-millis: 60000 #间隔多久才进行一次检测
      min-evictable-idle-time-millis: 300000 #一个连接在池中最小生存的时间
      test-on-borrow: false
      test-on-return: false
      test-while-idle: true
      validation-query: SELECT ‘lll‘
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 内置监控页面的首页是/druid/index.html
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: flex
        login-password: flex
        reset-enable: false

 //包结构如下创建:

技术图片

//其中domain,mapper接口和mapper.xml可以通过mybatis生产工具生成.一般可以在百度下载一个生产工具,然后生成对应的类后就拷贝到项目来:

此处可以参考别人提供的方法:https://www.cnblogs.com/smileberry/p/4145872.html

如下是我下载的一个项目:

技术图片

 

 技术图片

接着在主配置类上配上mapper的包扫描路径:

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 

 //创建service和实现类(具体分页逻辑后面分析)

public interface IOrderMastService {

    ShopOrderMast getOrderMastByOrderId(String orderId);

    PageInfo<ShopOrderMast> selectOrderList(OrderQueryObject   queryObject);

}

@Service
public class OrderMastServiceImpl implements IOrderMastService{
    private final static Logger logger =  LoggerFactory.getLogger(OrderMastServiceImpl.class);
    @Autowired
    private ShopOrderMastMapper orderMastMapper;
    @Override
    public ShopOrderMast getOrderMastByOrderId(String orderId) {
        return orderMastMapper.findOrderByOrderId(orderId);
    }

    @Override
    public PageInfo<ShopOrderMast> selectOrderList(OrderQueryObject queryObject) {
        logger.info("请求参数:{}", JSON.toJSONString(queryObject));
        PageHelper.startPage(queryObject.getCurrentNum(),queryObject.getPageSize());
        List<ShopOrderMast> list= orderMastMapper.selectOrderList(queryObject);
        logger.info("返回值:{}", JSON.toJSONString(list));
        return new PageInfo<>(list);
    }
}

//mapper接口
@Repository
public interface ShopOrderMastMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table shop_order_mast
*
* @mbggenerated
*/
int deleteByPrimaryKey(Long codId);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table shop_order_mast
*
* @mbggenerated
*/
int insert(ShopOrderMast record);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table shop_order_mast
*
* @mbggenerated
*/
int insertSelective(ShopOrderMast record);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table shop_order_mast
*
* @mbggenerated
*/
ShopOrderMast selectByPrimaryKey(Long codId);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table shop_order_mast
*
* @mbggenerated
*/
int updateByPrimaryKeySelective(ShopOrderMast record);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table shop_order_mast
*
* @mbggenerated
*/
int updateByPrimaryKey(ShopOrderMast record);

ShopOrderMast findOrderByOrderId(String orderId);

List<ShopOrderMast> selectOrderList(OrderQueryObject queryObject);
}
//controller

@Controller
@Api(value ="订单",description ="订单相关接口" ) //API注解是用来对类进行描述
public class ShopOrderController {

private final static Logger logger = LoggerFactory.getLogger(ShopOrderController.class);
@Autowired
private IOrderMastService orderMastService;

@RequestMapping(value = "shop/order/{orderId}",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value ="订单查询",notes ="通过订单号查询对应的订单",httpMethod ="POST")//对该方法进行说明
public ShopOrderMast findOrderMastByOrderId(@PathVariable("orderId") String orderId){
return orderMastService.getOrderMastByOrderId(orderId);
}

@RequestMapping(value = "shop/order/list",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value ="订单列表",notes ="订单列表分页查询",httpMethod ="POST")//对该方法进行说明
public PageInfo<ShopOrderMast> findOrderMastByOrderId(@RequestBody OrderQueryObject queryObject){
return orderMastService.selectOrderList(queryObject);
}
}

mapper.xml(提供部分信息)
<select id="findOrderByOrderId" resultMap="BaseResultMap">
select * from shop_order_mast WHERE cod_order_id=#{orderId};
</select>
<select id="selectOrderList" resultMap="BaseResultMap">
select * from shop_order_mast WHERE flg_status_order=#{orderStatu} ORDER by dat_order_submit DESC
</select>

//分页请求参数对象
public class QueryObject {
@ApiModelProperty("当前页")
private int currentNum=1;
@ApiModelProperty("总页数")
private int pageSize=10;

public int getCurrentNum() {
return currentNum;
}

public void setCurrentNum(int currentNum) {
this.currentNum = currentNum;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}

public class OrderQueryObject extends QueryObject {
@ApiModelProperty("订单状态")
private int orderStatu;

public int getOrderStatu() {
return orderStatu;
}

public void setOrderStatu(int orderStatu) {
this.orderStatu = orderStatu;
}
}

分页实现总结:
技术图片

 



 

 

springboot+mybatis集成分页功能

标签:database   agg   extend   color   rtp   mamicode   ssl   项目   help   

原文地址:https://www.cnblogs.com/yangxiaohui227/p/10969348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!