【sail】第三篇MybatisPlus的配置以及FreeMarker的配置

-前面我们利用MybatisPlus的代码生成器很方便的的生成了项目中用到的通用类,这一篇我们把MybatisPlus配置一下,另外页面这一块集成freemarker模版引擎,我们也需要配置一下。

MybatisPlus的配置

首先还是在pom文件中引入j依赖

然后我们在项目的application.properties配置

# jdbc_config
spring.datasource.url=jdbc:mysql://localhost:3306/bubble?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#druid_config
spring.datasource.max-active: 20
spring.datasource.initial-size: 1
spring.datasource.min-idle: 3
spring.datasource.max-wait: 60000
spring.datasource.time-between-eviction-runs-millis: 60000
spring.datasource.min-evictable-idle-time-millis: 300000
spring.datasource.test-while-idle: true
spring.datasource.test-on-borrow: false
spring.datasource.test-on-return: false
spring.datasource.poolPreparedStatements: true

# mybatis_config
mybatis.mapper-locations=classpath:com/bubble/mapper/*Mapper.xml 
mybatis.typeAliasesPackage=com.bubble.entity

这里只是基本的配置项,我们还需创建一个mybatisplus的配置类

@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
public class MybatisPlusConfig {

    @Autowired
    private Environment environment;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private MybatisProperties properties;

    private RelaxedPropertyResolver propertyResolver;

    @Autowired
    private ResourceLoader resourceLoader = new DefaultResourceLoader();

    @Autowired(required = false)
    private Interceptor[] interceptors;

    @Autowired(required = false)
    private DatabaseIdProvider databaseIdProvider;

    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }

    /**
     * 配置DataSource
     * @return
     * @throws SQLException
     */
    @Bean
    public DataSource druidDataSource() throws SQLException {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");

        System.out.println("====================druid数据库连接池注入====================");
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(propertyResolver.getProperty("url"));
        datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
        datasource.setUsername(propertyResolver.getProperty("username"));
        datasource.setPassword(propertyResolver.getProperty("password"));
        datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initial-size")));
        datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("min-idle")));
        datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("max-wait")));
        datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("max-active")));
        datasource.setMinEvictableIdleTimeMillis(Long.valueOf(propertyResolver.getProperty("min-evictable-idle-time-millis")));
        try {
            datasource.setFilters(propertyResolver.getProperty("filters"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return datasource;
    }

    /**
     * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定
     * 配置文件和mybatis-boot的配置文件同步
     * @return
     */
    @Bean
    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
        mybatisPlus.setDataSource(dataSource);
        mybatisPlus.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
        mybatisPlus.setConfiguration(properties.getConfiguration());
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            mybatisPlus.setPlugins(this.interceptors);
        }
        // MP 全局配置,更多内容进入类看注释
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        globalConfig.setDbType(DBType.MYSQL.name());
        // ID 策略 AUTO->`0`("数据库ID自增") INPUT->`1`(用户输入ID") ID_WORKER->`2`("全局唯一ID") UUID->`3`("全局唯一ID")
        globalConfig.setIdType(3);
        mybatisPlus.setGlobalConfig(globalConfig);
        MybatisConfiguration mc = new MybatisConfiguration();
        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        mybatisPlus.setConfiguration(mc);
        if (this.databaseIdProvider != null) {
            mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
        }
        return mybatisPlus;
    }

}

这是mybatisplus的全局配置类,接下来我们测试一下
在user表中新增一条数据

我们直接在controller里面写一个测试方法,直接注入我们利用代码生成器生成的servise以及通用方法,如下

我们到浏览器输入http://localhost:8080/user/info看看效果

FreeMarker的配置

前面我们取出数据都是直接json格式直接展示的,后边我们要用到后台的,以前都是使用JSP模版引擎的,久闻freemarker,今天尝试一下它的使用,freemarker支持jsp的标签,但是是使用自己的语法,在一些日期,金钱上的格式化处理更到位,特别的是对于宏定义的定义方便,简洁。之前使用过jsp的能很快的上手。

首先还是导入springboot中freemarker的依赖

    <!-- Spring Boot Freemarker 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

然后我们也要对其进行配置一番在applicaton.properties

#FreeMarker FREEMARKER (FreeMarkerAutoConfiguration)
#设置是否允许HttpServletRequest属性重写(隐藏)控制器生成同名的模型属性
spring.freemarker.allow-request-override=false
#设置是否允许HttpSession属性重写(隐藏)控制器生成同名的模型属性。
spring.freemarker.allow-session-override=false
#启用模板缓存
spring.freemarker.cache=true
# 检查模板位置是否存在
spring.freemarker.check-template-location=true
#设置freemarker的contentType
spring.freemarker.content-type=text/html;charset=UTF-8
# 启用mvc视图解决方案
spring.freemarker.enabled=true
# 是否将所有请求属性添加到与模板合 并之前的模型中
spring.freemarker.expose-request-attributes=true
# 设置是否所有HttpSession属性应该与模板融合之前添加到模型
spring.freemarker.expose-session-attributes=true
# 设置是否公开一个由Spring的macro库使用RequestContext,在名为“springMacroRequestContext”。
spring.freemarker.expose-spring-macro-helpers=true
# 设置RequestContext对象(便于在模版中调用)
spring.freemarker.request-context-attribute=rc
# 是否开启模板文件的热部署
spring.freemarker.prefer-file-system-access=true
# 视图前缀
spring.freemarker.suffix=.ftl
# 模板路径配置
spring.freemarker.template-loader-path=classpath:/templates/
# 设置模版更新时间
spring.freemarker.settings.template_update_delay=0
# 设置模版默认编码
spring.freemarker.settings.default_encoding=UTF-8
# 设置数字格式
spring.freemarker.settings.number_format=0.##########
# 设置模版时间格式
spring.freemarker.settings.datetime_format=yyyy-MM-dd HH:mm:ss
spring.freemarker.settings.classic_compatible=true
# 设置模版异常处理器
spring.freemarker.settings.template_exception_handler=ignore
spring.freemarker.order=1

基本的注释已经标了,想更深入的了解 可以到看看官方文档。

现在我们写一个基本的页面

我们写一个controller

在浏览器上输入http://localhost:8080/user/hello

最后说明一下,在${userName !} 后面有一个感叹号,这感叹号判断userName值为空,则用空格代替,这也是freemarker比较方便的一点。
更多用法我们可以网上看看详细的介绍

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值