# SpringBoot 快速开始 所有代码在https://gitee.com/xiandafu/beetlsql/tree/3.0/sql-integration/sql-springboot-starter,在SpringBoot快速开始前,如果你不熟悉BeetlSQL,可以参考《快速开始》 ## 环境准备 项目演示使用maven工程,新建一个Maven工程,直接引用如下库 ```xml <dependency> <groupId>com.ibeetl</groupId> <artifactId>sql-springboot-starter</artifactId> <version>${version}</version> </dependency> ``` 然后再引入数据库驱动,本章使用H2数据库作为例子,作为快速开始,避免安装数据库服务器,你也可以使用任意其他数据库 ```xml <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> ``` 引入数据库连接池HikariCP ```xml <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.3.1</version> </dependency> ``` 最后,需要准备一个sql脚本,以初始化H2数据库。保存如下sql到resources/db/schema.sql ```sql DROP TABLE IF EXISTS `sys_user`; CREATE TABLE `sys_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `department_id` int(11) DEFAULT NULL, `create_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ; BEGIN; INSERT INTO `sys_user` VALUES (1, 'lijz', 1, NULL); INSERT INTO `sys_user` VALUES (2, 'lucy', 1, NULL); INSERT INTO `sys_user` VALUES (3, 'bear', 2, NULL); INSERT INTO `sys_user` VALUES (4, 'mike', 1, NULL); INSERT INTO `sys_user` VALUES (5, 'lisan', 1, NULL); INSERT INTO `sys_user` VALUES (6, 'xb', 1, NULL); INSERT INTO `sys_user` VALUES (7, 'duanwu', 2, NULL); INSERT INTO `sys_user` VALUES (8, 'fenh', 1, NULL); INSERT INTO `sys_user` VALUES (9, 'lj', 2, NULL); INSERT INTO `sys_user` VALUES (10, 'gshen', 1, NULL); INSERT INTO `sys_user` VALUES (11, 'lihui', 1, NULL); COMMIT; ``` > 这个脚本也适合mysql执行 ## 环境搭建 为了方便快速演示,使用Spring单元测试。 在src/test下创建一个java类SimpleApplication,如下 ```java package org.beetl.sql.springboot.simple; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.PropertySource; @PropertySource(value = { "classpath:application-simple.properties" }, encoding = "utf-8") @SpringBootApplication public class SimpleApplication { public static void main(String[] args) { SpringApplication.run(SimpleApplication.class, args); } } ``` application-simple.properties 是springboot的配置文件,内容如下 ```xml spring.datasource.url=jdbc:h2:mem:dbtest;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=sa spring.datasource.password= spring.datasource.driver-class-name=org.h2.Driver beetlsql = sqlManager1 beetlsql.sqlManager1.ds=ds1 beetlsql.sqlManager1.basePackage=org.beetl.sql.springboot.simple ``` Spring配置需要配置如下Bean * datasource,这里使用H2库,这个是在`SimpleDataSourceConfig` 类里配置的数据源,命名为ds1 * beetlsql 配置一个名为sqlManager1的SQLManager实例,其后的配置`beetlsql.sqlManager1.ds` 配置了关联的数据源ds1 ​ 并配置了mapper的包名是org.beetl.sql.springboot.simple,BeetlSQL将会扫描其下的所有的Mapper接口,并为其自动生成代理类,注册为Spring的Bean SimpleDataSourceConfig定义如下,会配置一个叫ds1的数据源 ```java @Configuration public class SimpleDataSourceConfig { @Autowired ApplicationContext ctx; @Primary @Bean(name = "ds1") public DataSource datasource(Environment env) { HikariDataSource ds = new HikariDataSource(); ds.setJdbcUrl(env.getProperty("spring.datasource.url")); ds.setUsername(env.getProperty("spring.datasource.username")); ds.setPassword(env.getProperty("spring.datasource.password")); ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); return ds; } @Bean public SQLManagerCustomize mySQLManagerCustomize(){ return new SQLManagerCustomize(){ @Override public void customize(String sqlMangerName, SQLManager manager) { //初始化sql,这里也可以对sqlManager进行修改 DBInitHelper.executeSqlScript(manager,"db/schema.sql"); } }; } } ``` SQLManagerCustomize类可以在配置的基础上,定制SQLManager,这里不做任何修改,仅仅调用初始化数据库,以方便测试 有了以上准备,现在可以编写一个单元测试 ```java @RunWith(SpringRunner.class) @SpringBootTest(classes = SimpleApplication.class) @Transactional public class SimpleTest { @Autowired SQLManager sqlManager; @Autowired SimpleUserInfoMapper userInfoMapper; @Test public void test(){ sqlManager.single(UserInfo.class,1); userInfoMapper.single(1); } } ``` SimpleUserInfoMapper 是一个BeetlSQL3的Mapper,定义如下 ```java public interface SimpleUserInfoMapper extends BaseMapper<UserInfo> { } ``` UserInfo定义如下 ```java @Table(name="sys_user") @Data public class UserInfo { @AssignID private Long id; private String name; } ``` UserService是一个Spring的Service,简单的调用`userMapper.insert` ```java @Service @Transactional public class UserService { @Autowired UserMapper userMapper; public void addUser(UserInfo userInfo){ userMapper.insert(userInfo); } } ``` UserMapper是一个BeetlSQL3的Mapper,我们使用BaseMapper提供的内置insert方法操作 ```java public interface UserMapper extends BaseMapper<UserInfo> { } ``` ## 完整配置 完整配置通过`BeetlSqlConfig`解析,因此可以参考这个类了解如何配置 ``` beetlsql=xxx1,xxx2 ``` 多个sqlManager用逗号分开,然后可以一次配置`beetlsql.xxx1.配置项`的配置风格来配置 ```properties #2.x模式是Dao,3.x改成Mapper beetlsql.xxx1.daoSuffix=Dao #2.x默认是DefaultNameConversion,3.x改成UnderlinedNameConversion beetlsql.xxx1.nameConversion=org.beetl.sql.core.UnderlinedNameConversion beetlsql.xxx1.dbStyle=org.beetl.sql.core.db.MySqlStyle beetlsql.xxx1.basePackage=com.compamny.xxxx beetlsql.xxx1.dev=true beetlsql.xxx1.sqlPath=sql ``` 如果有多个sqlManager,你可以配置默认的,使用`_default` ```properties beetlsql._default.nameConversion=org.beetl.sql.core.UnderlinedNameConversion ```