BeetlSQL是一个全功能DAO工具, 同时具有Hibernate 优点 & Mybatis优点功能,适用于承认以SQL为中心,同时又需求工具能自动能生成大量常用的SQL的应用。BeetlSQL官网:http://ibeetl.com
beatlsql 优点
开发效率:
无需注解,自动使用大量内置SQL,轻易完成增删改查功能,节省50%的开发工作量
数据模型支持Pojo,也支持Map/List这种快速模型,也支持混合模型
SQL 模板基于Beetl实现,更容易写和调试,以及扩展
维护性:
SQL 以更简洁的方式,Markdown方式集中管理,同时方便程序开发和数据库SQL调试。
可以自动将sql文件映射为dao接口类。
灵活直观的支持支持一对一,一对多,多对多关系映射而不引入复杂的OR Mapping概念和技术。
具备Interceptor功能,可以调试,性能诊断SQL,以及扩展其他功能。
其他:
内置支持主从数据库支持的开源工具。
支持跨数据库平台,开发者所需工作减少到最小,目前跨数据库支持mysql,postgres,oracle,sqlserver,h2,sqllite,DB2。spring boot、beetlsql整合过程
resouces/META-INF/spring-devtools.properties
spring-devtools.properties文件内容:
restart.include.beetl=/beetl-2.3.2.jar
restart.include.beetlsql=/beetlsql-2.3.1.jar
resouces/sql/ account.md
account.md文件内容:
selectAccountByName // 对应接口名
===
*根据name获account // *注释
select * from account where name= #name# // 自定义SQL
// 放弃XML,使用Markdown文件格式原因:
XML格式过于复杂,书写不方便。
XML 格式有保留符号,写SQL的时候也不方便,如常用的< 符号 必须转义。
MD 格式本身就是一个文档格式,也容易通过浏览器阅读和维护。
resouces/templates/index.btl
index.btl模板文件内容:
total = ${total}
resouces/application.properties
开发按需配置,此处不做任何配置
// 引入必要依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetlsql</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
springboot没有对beatlsql进行快速启动装配,所以需要自己导入相关的bean,包括数据源,包扫描,事物管理器等!
@SpringBootApplication
public class SpringbootBeatlsqlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootBeatlsqlApplication.class, args);
}
//配置文件
@Bean(initMethod = "init", name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
try {
// WebAppResourceLoader 配置root路径是关键
WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/templates").getFile().getPath());
beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
} catch (IOException e) {
e.printStackTrace();
}
//读取配置文件信息
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;
}
//配置包扫描
@Bean(name = "beetlSqlScannerConfigurer")
public BeetlSqlScannerConfigurer getBeetlSqlScannerConfigurer() {
BeetlSqlScannerConfigurer conf = new BeetlSqlScannerConfigurer();
conf.setBasePackage("com.forezp.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() {
return DataSourceBuilder.create().url("jdbc:mysql://127.0.0.1:3306/test").username("root").password("123456").build();
}
//开启事务
@Bean(name = "txManager")
public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("datasource") DataSource datasource) {
DataSourceTransactionManager dsm = new DataSourceTransactionManager();
dsm.setDataSource(datasource);
return dsm;
}
}
加入jar和配置beatlsql的这些bean,以及resources这些配置之后,springboot就能够访问到数据库类。
初始化数据库表
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`money` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');
// bean
@Data
public class Account {
private int id ;
private String name ;
private double money;
}
// DAO层
public interface AccountDao extends BaseMapper<Account> {
// params可以不用在接口参数上使用@Param,而直接使用params属性
@SqlStatement(params = "name");
Account selectAccountByName(String name);
// 下述两个等价,_st,_sz同@RowStart和@RowSize
@SqlStatement(params="name,age,_st,_sz")
public List<User> queryUser( String name, Integer age,int start, int size);
public List<User> queryUser( @Param(name) String name, @Param(age) Integer age, @RowStart int start, @RowSize int size);
}
// 简略service层
// WEB层
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
AccountDao accountDao;
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List<Account> getAccounts(){
return accountDao.all();
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Account getAccountById(@PathVariable("id") int id){
return accountDao.unique(id);
}
@RequestMapping(value = "",method = RequestMethod.GET)
public Account getAccountById(@RequestParam("name") String name){
return accountDao.selectAccountByName(name);
}
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public String updateAccount(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money" ,required = true)double money){
Account account=new Account();
account.setMoney(money);
account.setName(name);
account.setId(id);
int t=accountDao.updateById(account);
if(t==1){
return account.toString();
}else {
return "fail";
}
}
@RequestMapping(value = "",method = RequestMethod.POST)
public String postAccount( @RequestParam(value = "name")String name,
@RequestParam(value = "money" )double money) {
Account account = new Account();
account.setMoney(money);
account.setName(name);
KeyHolder t = accountDao.insertReturnKey(account);
if (t.getInt() > 0) {
return account.toString();
} else {
return "fail";
}
}
}