一、工程目录结构
二、pom.xml文件配置
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope></dependency> <!--beetlsql--> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-framework-starter</artifactId> <version>1.1.35.RELEASE</version> </dependency> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetlsql</artifactId> <version>2.10.10</version> </dependency>
三、beetlsqlConfig文件
@Configurationpublic class BeetlSqlConfig { //=============以下是beetl配置========================= @Bean(initMethod = "init", name = "beetlConfig") public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() { BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration(); ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()); // WebAppResourceLoader 配置root路径是关键 //WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/sql").getFile().getPath()); WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(); beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader); //读取配置文件信息 return beetlGroupUtilConfiguration; } @Bean(name = "beetlViewResolver") public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) { BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver(); 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.demo.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 DefaultNameConversion()); factory.setSqlLoader(new ClasspathLoader("/sql")); //sql文件路径 return factory; } //配置数据库 @Bean(name = "datasource") public DataSource getDataSource(Environment env) { String url = env.getProperty("spring.datasource.mysql.url"); String userName = env.getProperty("spring.datasource.mysql.username"); String password = env.getProperty("spring.datasource.mysql.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; }}
四、dao层,继承一个BaseMapper
五、service层