上图是BeetlSQL 官网中对 BeetlSQL 的介绍,简单来说我们可以得到几个点
- 开发效率高
- 维护性好
- 性能数倍于 JPA MyBatis
关于 BeetlSQL 的更多介绍大家可以去到官网去看看,接下来我们来看看如何把这个 DAO 工具整合到项目中
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!-- 引入beetlsql --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetlsql</artifactId> <version>2.10.34</version> </dependency> <!-- 引入beetl --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl</artifactId> <version>2.9.3</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
sql 文件,我这里用的是 mysql
1 2 3 4 5 6 7 8 9
| CREATE TABLE `test`.`Untitled` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `nickname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT 18, `cdate` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0), `udate` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0), PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
|
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.priv.gabriel.entity;
/** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */ public class User {
private long id;
private String username;
private String nickname;
private int age;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getNickname() { return nickname; }
public void setNickname(String nickname) { this.nickname = nickname; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", nickname='" + nickname + '\'' + ", age=" + age + '}'; } }
|
在这里有两个分支,一种是通过 sqlManager 来操作,另一种是整合 mapper,在这里我们现看看第一种方式
SQLManager 方式
UserControllerForSQLManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| package com.priv.gabriel.controller;
import com.priv.gabriel.entity.User; import com.priv.gabriel.repository.UserRepository; import org.beetl.sql.core.SQLManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RequestMapping("/sqlManager/users") public class UserControllerForSQLManager {
@Autowired private SQLManager sqlManager;
@RequestMapping(value = "/{id}",method = RequestMethod.GET) public User selectUserById(@PathVariable("id")int id){ return sqlManager.single(User.class,id); }
@RequestMapping(value = {"","/"},method = RequestMethod.GET) public List<User> getUsers(){ return sqlManager.all(User.class,1,2); }
public void singletonTableQuery(){ sqlManager.query(User.class).andLike("username","admin").orderBy("age").select(); }
@RequestMapping(value = {"","/"},method = RequestMethod.POST) public void addUser(User user){ sqlManager.insertTemplate(user,true); System.out.println(user.getId()); System.out.println("新增成功"); }
@RequestMapping(value = {"","/"},method = RequestMethod.PUT) public String updateById(User user){ if(sqlManager.updateTemplateById(user)>0){ return "修改成功"; }else{ return "修改失败"; } }
@RequestMapping(value = "/id",method = RequestMethod.DELETE) public String deleteById(@PathVariable("id") int id){ if(sqlManager.deleteById(User.class,id) >0 ){ return "删除成功"; }else{ return "删除失败"; } } }
|
Mapper 方式
如果要使用 mapper 方式,则需要新建一个 mapper 接口,并继承 BaseMapper
UserRepository.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.priv.gabriel.repository;
import com.priv.gabriel.entity.User; import org.beetl.sql.core.mapper.BaseMapper;
public interface UserRepository extends BaseMapper<User>{
}
|
UserControllerForMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.priv.gabriel.controller;
import com.priv.gabriel.entity.User; import com.priv.gabriel.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RequestMapping("/mapper/users") public class UserControllerForMapper {
@Autowired private UserRepository repository;
@RequestMapping(value = {"","/"},method = RequestMethod.POST) public void addUser(User user){ repository.insert(user); }
@RequestMapping(value = {"","/"},method = RequestMethod.DELETE) public String deleteUserById(User user){ if(repository.deleteById(user) >0 ){ return "删除成功"; }else{ return "删除失败"; } }
@RequestMapping(value = {"","/"},method = RequestMethod.PUT) public String updateUserById(User user){ if(repository.deleteById(user) > 0){ return "修改成功"; }else{ return "修改失败"; } }
@RequestMapping(value = "/{id}",method = RequestMethod.GET) public User selectUserById(@PathVariable("id")int id){ return repository.single(id); }
@RequestMapping(value = {"","/"},method = RequestMethod.GET) public List<User> getUsers(){ return repository.all(); } }
|
两种方式都介绍完毕了,但是 BeetlSQL 的重点部分还不在这,BeetlSQL 的重点是可以创建一个 SQL 模板,到这大家可能会想,不就是个 xml 嘛,mybatis 就有呀,不一样的地方就在这了,BeetlSQL 的 SQL 模板是这样的
1 2 3
| # selectByTest
select \* from user where 1=1
|
怎么样,是不是眼前一亮,很明显 selectByTest
是这条 SQL 语句的 id , ===
的作用是代表 id 和内容的分割,而最后的部分当然就是 SQL 语句啦
然后简单介绍一下调用 SQL 模板的方式
SQLManager 方式
1 2 3 4
| @RequestMapping(value = "/test",method = RequestMethod.GET) public List<User> getUsersByTest(){ return sqlManager.select("user.selectByTest",User.class); }
|
在 SQLManager 的方式中,通过sqlManager.select("模板id",类型)
的方式直接调用
Mapper 的方式
1 2 3 4
| @SqlResource("user") public interface UserRepository extends BaseMapper<User>{ List<User> selectByTest(); }
|
在 Mapper 的方式,需要先建立一个xxx.md
的 SQL 模板文件,通过@SqlResource(模板文件名)
这个注解找到模板文件,再在 mapper 中写入与模板文件中同名的方法,即可在外部调用
注意,BeetlSQL 的模板文件位置默认在resource/sql/xxx.md
中,好啦,关于 BeetlSQL 的介绍就到这里。
BeetlSQL 的详细介绍
Beetl 官方文档
BeetlSQL 官方文档
项目点此下载