企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 环境搭建 为了方便快速演示,使用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的配置文件(参考`BeetlSqlConfig`,内容如下 ```properties 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.sqlManagers=sqlManager1 # 新版配置 beetlsql.sqlManager1.ds=ds1 beetlsql.sqlManager1.basePackage=org.beetl.sql.springboot.simple ``` ```yml beetlsql: sqlManagers: sqlManager1,sqlManager2 sqlManager1: ds: dataSource basePackage: org.beetl.sql.springboot.simple daoSuffix: Dao ``` 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> { } ```