<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.15version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.23version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.2.0version>
dependency>
dependencies>
dependencyManagement>
dependencyManagement标签的作用是声明版本,但是不实际引入依赖,在子工程中我们引入的依赖就无需声明版本了,起到统一版本管理的作用.
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
dependency>
dependencies>
<dependencies>
<dependency>
<groupId>com.ccmgroupId>
<artifactId>assembly-mysqlartifactId>
<version>1.0.0version>
dependency>
dependencies>
server:
port: 1000 #服务端口
spring:
application:
name: server-user #服务名称
datasource:
username: root
password: Test2016@
url: jdbc:mysql://ip:3306/user?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
typeAliasesPackage: com.ccm.server.user.dao.mysql.domain #数据库实体类包
mapper-locations: classpath:mappering/*.xml #xml文件扫描
package com.ccm.server.user;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
/**
* @Description server-user服务启动类
* @Author zhouzhiwu
* @CreateTime 2020/07/04 0:50
*/
@MapperScan(basePackages = "com.ccm.server.user.dao.mysql.mapper")
@SpringBootApplication //声明为一个启动类
@Import(value = PaginationInterceptor.class)
public class ServerUserApplication {
public static void main(String[] args) {
SpringApplication.run(ServerUserApplication.class,args);
}
}
@MapperScab注解声明要扫描的mapper对象所在包
@Import注解的功能是往spring的ioc容器中注入bean,显然我们注入了一个分页拦截器.
// An highlighted block
package com.ccm.server.user.controller;
import com.ccm.server.user.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description 测试控制层
* @Author zhouzhiwu
* @CreateTime 2020/07/06 14:37
*/
@RestController
@RequestMapping(value = "test")
public class TestController {
@Autowired
private TestService testService;
/**
* @Description springboot整合mybatis-plus和druid测试
* @Author zhouzhiwu
* @CreateTime 2020/7/6 14:38
* @Params []
* @Return void
*/
@GetMapping(value = "test01")
public Object test01() {
return testService.test01();
}
/**
* @Description springboot整合mybatis-plus分页插件测试
* @Author zhouzhiwu
* @CreateTime 2020/7/7 14:55
* @Params []
* @Return java.lang.Object
*/
@GetMapping(value = "test02")
public Object test02() {
return testService.test02();
}
}
package com.ccm.server.user.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccm.server.user.dao.mysql.domain.TestTable;
import com.ccm.server.user.dao.mysql.mapper.TestTableMapper;
import com.ccm.server.user.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description 测试业务层实现
* @Author zhouzhiwu
* @CreateTime 2020/07/06 14:43
*/
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestTableMapper testTableMapper;
@Override
public List<TestTable> test01() {
return testTableMapper.list();
}
@Override
public Page<TestTable> test02() {
Page<TestTable> page = new Page<>(1, 10);
List<TestTable> records = testTableMapper.page(page);
page.setRecords(records);
return page;
}
}
package com.ccm.server.user.dao.mysql.mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccm.server.user.dao.mysql.domain.TestTable;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @Description testtable业务层
* @Author zhouzhiwu
* @CreateTime 2020/07/06 14:40
*/
@Mapper
public interface TestTableMapper {
List<TestTable> list();
List<TestTable> page(Page<TestTable> page);
}
<?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.ccm.server.user.dao.mysql.mapper.TestTableMapper" >
<select id="list" resultType="com.ccm.server.user.dao.mysql.domain.TestTable">
select * from test_table
</select>
<select id="page" resultType="com.ccm.server.user.dao.mysql.domain.TestTable">
select * from test_table
</select>
</mapper>
package com.ccm.server.user.dao.mysql.domain;
import lombok.Data;
/**
* @Description test_table表实体类
* @Author zhouzhiwu
* @CreateTime 2020/07/06 14:42
*/
@Data
public class TestTable {
private Long id;
private String name;
}