美文网首页
一、spring-boot-druid(single datab

一、spring-boot-druid(single datab

作者: 喝咖啡的蚂蚁 | 来源:发表于2018-08-01 17:49 被阅读0次

Spring Boot 单个数据源的配置 && 分页(PageHelper)

项目地址:https://gitee.com/eaco/yi-spring-boot-study

  1. 添加pom.xml依赖
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
  1. 配置application.properties
    配置druid数据源,更多的数据源配置参考alibaba的GitHub说明,地址为:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf8&serverTimezone=UTC
spring.datasource.druid.username=root
spring.datasource.druid.password=root
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
#注意:一定要对应mapper映射xml文件的所在路径
mybatis.mapper-locations: classpath*:cn/yi/springboot/druid/mapper/*.xml
# 注意:对应实体类的路径
mybatis.type-aliases-package:cn.yi.springboot.druid.dto
  1. 配置数据源以及mybatis的映射关系包路径
/**
 * @author: yiz
 * @date: 2018/8/1 14:35
 **/
@Configuration
@MapperScan(basePackages = "cn.yi.springboot.druid.mapper")
public class DruidConfig {
    @Primary
    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource dataSourceOne(){
        return DruidDataSourceBuilder.create().build();
    }
}
  1. 分页代码
    BaseCurdService通用分页代码
/**
 * @author: yiz
 * @date: 2018/8/1 16:33
 **/
public class BaseCurdService<T> {
    public PageInfo<T> findbyPage(Page pageRequest, PageAction action) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize(), pageRequest.getOrderBy());
        //执行DB操作,PageHelper自动对分页进行查询
        List<?> list =  action.execute();
        PageInfo<T> pageInfo = new PageInfo(list);
        return pageInfo;
    }
}

PageAction 使用FunctionInterface,使用JDK1.8新特性

/**
 * @author: yiz
 * @date: 2018/7/27 16:16
 **/
@FunctionalInterface
public interface PageAction {
    List<?> execute();
}

UserServiceImpl 具体实现

/**
 * @author: yiz
 * @date: 2018/8/1 16:36
 **/
@Service
public class UserServiceImpl extends BaseCurdService<UserDTO> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<UserDTO> findByPage(Page page) {
        return super.findbyPage(page, () -> userMapper.selectAllUser());
    }
}

好了,已经写好代码,开始测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes= Application.class)
public class TestMapper {
    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;
    @Test
    public void testFind() {
        List<UserDTO> users = userMapper.selectAllUser();
        System.out.println(users);
        System.out.println(userMapper.selectByPrimaryKey(1));
    }

    @Test
    public void testPage(){
        Page page = new Page(1,1);
        PageInfo<UserDTO> pageInfo = userService.findByPage(page);
        System.out.println(pageInfo);
    }
}

相关文章

网友评论

      本文标题:一、spring-boot-druid(single datab

      本文链接:https://www.haomeiwen.com/subject/xfbrvftx.html