记录自己搭建Spring boot 2 + mybatis plus + druid的过程

原创
2018/08/23 00:49
阅读数 3.2K

记录自己搭建Spring boot 2 + mybatis plus + druid的过程,比较简略,详细还是看官方文档吧

  • 当然是添加依赖啦,我的是2.0.4.RELEASE版本的Spring boot
      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
  • 在配置文件中添加Druid的配置
spring.datasource.druid.url= jdbc:mysql://127.0.0.1:3306//myDB?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.druid.username=root
spring.datasource.druid.password=123456789
spring.datasource.druid.driver-class-name= com.mysql.cj.jdbc.Driver
spring.datasource.druid.initial-size=10
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=8
@Configuration
public class DruidConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);
    private static final String DB_PREFIX = "spring.datasource";
// @Autowired
// IDataSourceProperties dataSourceProperties;
    @Bean
    public ServletRegistrationBean druidServlet() {
        System.out.println("init Druid Servlet Configuration ");
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // IP白名单
        servletRegistrationBean.addInitParameter("allow", "*");
        // IP黑名单(共同存在时,deny优先于allow)
        servletRegistrationBean.addInitParameter("deny", "192.168.1.100");
        //控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        servletRegistrationBean.addInitParameter("resetEnable", "false");
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
  • 恩 druid添加好了,写个单元测试看看是否可用
   @Autowired
    JdbcTemplate jdbcTemplate;

 @Test
    public void getJdbc(){
        List<Map<String, Object>> res= jdbcTemplate.queryForList("SELECT * FROM user where id=1 ");
        System.out.println(res);
    }
  • 接下来在pom中引用mybatis-plus
 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0-RC1</version>
      </dependency>
# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.mapper
# 如果是放在resource目录 classpath:/mapper/*Mapper.mapper
mybatis-plus.mapper-locations= classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
mybatis-plus.typeAliasesPackage= org.xh.miaosha.code.entity
#主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.global-config.db-config.id-type= AUTO
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus.global-config.db-config.field-strategy= NOT_NULL
#mp2.3+ 全局表前缀 mp_
mybatis-plus.global-config.db-config.table-prefix= xxx_
#刷新mapper 调试神器
mybatis-plus.global-config.refresh=true

  • 添加Mybatis-plus配置类
@Configuration
@MapperScan("org.xh.sb.code.mapper*")
public class MybatisPlusConfig {
    @Autowired
    DataSource dataSource;
    /**
     * 分页插件,自动识别数据库类型
     * 多租户,请参考官网【插件扩展】
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
  • 最后 ,ok 搞定,其实中间自动生成这块按照官网的demo配置就行,不难,最后补下我的结构图
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户表id',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `password` varchar(50) NOT NULL COMMENT '用户密码,MD5加密',
  `email` varchar(50) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `question` varchar(100) DEFAULT NULL COMMENT '找回密码问题',
  `answer` varchar(100) DEFAULT NULL COMMENT '找回密码答案',
  `role` int(4) NOT NULL COMMENT '角色0-管理员,1-普通用户',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '最后一次更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_name_unique` (`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;

INSERT INTO `myDB`.`user`(`id`, `username`, `password`, `email`, `phone`, `question`, `answer`, `role`, `create_time`, `update_time`) VALUES (1, 'admin', '432D38237BD939443EC5D48E24FD3B1A', 'admin@admin.com', '13800138000', '问题', '答案', 1, '2016-11-06 16:56:45', '2017-04-04 19:27:36');
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部