springboot集成beetl,beetlsql ,解决No supported DataSource type found这样的问题

  • Post author:
  • Post category:其他


文件目录结构:

pom文件添加下面引用(最后三个引用可以任选一个,当然全写上也不会错,三个都没有,就可能会出现标题中的问题):

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>

<dependency>
   <groupId>com.ibeetl</groupId>
   <artifactId>beetl</artifactId>
   <version>2.8.5</version>
</dependency>
<dependency>
   <groupId>com.ibeetl</groupId>
   <artifactId>beetlsql</artifactId>
   <version>2.10.30</version>
</dependency>
<dependency>
   <groupId>com.ibeetl</groupId>
   <artifactId>beetl-framework-starter</artifactId>
   <version>1.1.35.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>4.3.13.RELEASE</version>
</dependency>
<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP</artifactId>
   <version>3.2.0</version>
</dependency>
<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-dbcp2</artifactId>
   <version>2.4.0</version>
</dependency>
<dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jdbc</artifactId>
   <version>9.0.10</version>
</dependency>

applicatiom.yml配置文件

server:
  port: 9090
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/ebs?useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: haolei
    password: 123456

beetl.properties配置文件(从网上抄别人的)

restart.include.beetl= /beetl-2.3.2.jar
restart.include.beetlsql= /beetlsql-2.3.1.jar
ENGINE=org.beetl.core.engine.DefaultTemplateEngine
DELIMITER_PLACEHOLDER_START=${
DELIMITER_PLACEHOLDER_END=}
DELIMITER_STATEMENT_START=<%
DELIMITER_STATEMENT_END=%>
DIRECT_BYTE_OUTPUT = FALSE
HTML_TAG_SUPPORT = true
HTML_TAG_FLAG = #
HTML_TAG_BINDING_ATTRIBUTE = var
NATIVE_CALL = TRUE
TEMPLATE_CHARSET = UTF-8
ERROR_HANDLER = org.beetl.core.ConsoleErrorHandler
NATIVE_SECUARTY_MANAGER= org.beetl.core.DefaultNativeSecurityManager
MVC_STRICT = FALSE

#资源配置,resource后的属性只限于特定ResourceLoader
RESOURCE_LOADER=org.beetl.core.resource.ClasspathResourceLoader
#classpath 根路径
#RESOURCE.root= /templates/
#是否检测文件变化,开发用true合适,但线上要改为false
RESOURCE.autoCheck= true

beetlconf.java配置类文件

@Configuration
public class BeetlConf {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
        beetlGroupUtilConfiguration.setResourceLoader(classpathResourceLoader);
        beetlGroupUtilConfiguration.init();
        return beetlGroupUtilConfiguration;
    }
    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setPrefix("/templates/");
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }

    //=============  以下是beetsql配置  =============
    @Bean(name = "beetlSqlScannerConfigurer")
    public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
        BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
        conf.setBasePackage("com.example.demobeetl2.dao");
        conf.setDaoSuffix("Dao");
        conf.setSqlManagerFactoryBeanName("sqlManagerFactoryBean");
        return conf;
    }

    @Bean(name = "sqlManagerFactoryBean")
    @Primary
    public SqlManagerFactoryBean getSqlManagerFactoryBean(@Qualifier("datasource") DataSource datasource) {
        SqlManagerFactoryBean factory = new SqlManagerFactoryBean();
        BeetlSqlDataSource source = new BeetlSqlDataSource();
        source.setMasterSource(datasource);

        factory.setCs(source);
        factory.setDbStyle(new MySqlStyle());
        factory.setInterceptors(new Interceptor[]{new DebugInterceptor()});
        factory.setNc(new UnderlinedNameConversion());//开启驼峰
        factory.setSqlLoader(new ClasspathLoader("/sql"));//sql文件路径
        return factory;
    }

    //配置数据库
    @Bean(name = "datasource")
    public DataSource getDataSource(Environment env) {
        String url = env.getProperty("spring.datasource.url");
        String userName = env.getProperty("spring.datasource.username");
        String password = env.getProperty("spring.datasource.password");
        return DataSourceBuilder.create().url(url).username(userName).password(password).build();
    }
    //开启事务
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("datasource") DataSource datasource) {
        DataSourceTransactionManager dsm = new DataSourceTransactionManager();
        dsm.setDataSource(datasource);
        return dsm;
    }
}

pojo类:Account.java  (因为添加了lombok依赖,且使用了Data注解,所以可以不用写get、set、toString 等方法)

@Data
public class Account {
    private int id ;
    private String name ;
    private double money;
}

controller文件(里面直接调用了dao,跳过了service,因为懒):

@Controller
@RequestMapping(value = "/home")
public class Beetl2Controller {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    AccountDao dao ;

    @RequestMapping("/ad")
    public String add(){
        logger.info("---------this is first beetl----------");
        Account account = dao.selectAccountByName("aaa");
        System.out.println("===================" + account.getName()+account.getMoney());
        return "add";
    }

}

dao文件:

@SqlResource("account")
public interface AccountDao extends BaseMapper<Account> {

    Account selectAccountByName(@Param("name") String name);

}

account.md文件(sql文件,文件名称和上面的@SqlResource(“account”)有点联系,想想他们之间有什么样的联系):

selectAccountByName
===
*根据name获account
 
    select * from account where name= #name#

add.html文件(比较简单):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hhhhhhhhhhhhhhhhhhh
</body>
</html>



版权声明:本文为jiandao230原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。