Spring boot 配置 mybatis xml和动态SQL 分页配置

时间:2023-03-08 20:40:27

更新时间 2018年4月30日23:27:07

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.imooc</groupId>
<artifactId>sell</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>sell</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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.3.2</version>
</dependency> <!--数据库链接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--热启动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
<exclusions>
<exclusion>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 热部署 -->
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.配置 application.yml

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://127.0.0.1/tpshop2.5.0?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
mybatis:
config-locations: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mapper/*.xml
configuration:
call-setters-on-nulls: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql

3.配置Application.java 启动

@SpringBootApplication
@MapperScan("com.smartom.dao") //扫描dao层
public class MyApp {
public static void main(String[] args) throws Exception{
SpringApplication.run(MyApp.class,args);
}
}

4. Controller层

    @GetMapping("/list")
public PageInfo<ProductListVO> getList(
@RequestParam(value="current",defaultValue = "1") Integer current,
@RequestParam(value="pageSize",defaultValue = "10")Integer pageSize
){
PageInfo<ProductListVO> productList = iProductService.getProductList(current,pageSize);
return productList;
}

5.service层

    @Override
public PageInfo<ProductListVO> getProductList(Integer current, Integer pageSize) {
PageHelper.startPage(current,pageSize);
Page<ProductListVO> ProductListVO = productsMapper.getProductList();
Integer total = productsMapper.productsTotal();
PageInfo<ProductListVO> pageInfo = new PageInfo(current,pageSize,total,ProductListVO);
return pageInfo;
}

6.dao层

public interface ProductsMapper {
@Select("select product_name from product_info where goods_id = #{goods_id}")
public ProductVO getProductInfo(@Param("goods_id") int goods_id); public Page<ProductListVO> getProductList(); @Select("select count(*) total from tp_goods")
Integer productsTotal();
}

7.mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.smartom.dao.ProductsMapper">
<select id="getProductList" resultType="com.smartom.VO.ProductListVO">
select goods_id,goods_name,goods_sn,cat_id,price_ladder,is_recommend,is_new,is_hot,is_on_sale,store_count,sort from
tp_goods
</select>
</mapper>
···