From dab9ee4b2fc93c8f2dfdc7b96c1e558ad91fdf48 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Wed, 14 Nov 2018 21:41:00 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-orm-beetlsql=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-orm-beetlsql/README.md | 387 +++++++++++++++++- spring-boot-demo-orm-beetlsql/pom.xml | 5 + .../orm/beetlsql/config/BeetlConfig.java | 3 + .../xkcoding/orm/beetlsql/entity/User.java | 2 + .../orm/beetlsql/service/UserService.java | 14 +- .../service/impl/UserServiceImpl.java | 29 +- .../src/main/resources/application.yml | 41 +- .../service/impl/UserServiceImplTest.java | 45 +- 8 files changed, 499 insertions(+), 27 deletions(-) diff --git a/spring-boot-demo-orm-beetlsql/README.md b/spring-boot-demo-orm-beetlsql/README.md index 06fd0e2..4de6300 100644 --- a/spring-boot-demo-orm-beetlsql/README.md +++ b/spring-boot-demo-orm-beetlsql/README.md @@ -1,3 +1,388 @@ # spring-boot-demo-orm-beetlsql -> 此 demo 仍然存在问题 \ No newline at end of file +> 此 demo 主要演示了 Spring Boot 如何整合 beetl sql 快捷操作数据库,使用的是beetl官方提供的beetl-framework-starter集成。集成过程不是十分顺利,没有其他的orm框架集成的便捷。 + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-orm-beetlsql + 1.0.0-SNAPSHOT + jar + + spring-boot-demo-orm-beetlsql + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + 1.1.68.RELEASE + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + com.ibeetl + beetl-framework-starter + ${ibeetl.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + + mysql + mysql-connector-java + + + + cn.hutool + hutool-all + + + + com.google.guava + guava + + + + org.projectlombok + lombok + true + + + + + spring-boot-demo-orm-beetlsql + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## application.yml + +> 注意下方注释的地方,**不能解开注释,并且需要通过JavaConfig的方式手动配置数据源**,否则,会导致beetl启动失败,因此,初始化数据库的数据,只能手动在数据库使用 resources/db 下的建表语句和数据库初始化数据。 + +```yaml +spring: + datasource: + url: jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 + username: root + password: root + driver-class-name: com.mysql.cj.jdbc.Driver +#### beetlsql starter不能开启下面选项 +# type: com.zaxxer.hikari.HikariDataSource +# initialization-mode: always +# continue-on-error: true +# schema: +# - "classpath:db/schema.sql" +# data: +# - "classpath:db/data.sql" +# hikari: +# minimum-idle: 5 +# connection-test-query: SELECT 1 FROM DUAL +# maximum-pool-size: 20 +# auto-commit: true +# idle-timeout: 30000 +# pool-name: SpringBootDemoHikariCP +# max-lifetime: 60000 +# connection-timeout: 30000 +logging: + level: + com.xkcoding: debug + com.xkcoding.orm.beetlsql: trace +beetl: + enabled: false +beetlsql: + enabled: true + sqlPath: /sql + daoSuffix: Dao + basePackage: com.xkcoding.orm.beetlsql.dao + dbStyle: org.beetl.sql.core.db.MySqlStyle + nameConversion: org.beetl.sql.core.UnderlinedNameConversion +beet-beetlsql: + dev: true +``` + +## BeetlConfig.java + +```java +/** + *

+ * Beetl数据源配置 + *

+ * + * @package: com.xkcoding.orm.beetlsql.config + * @description: Beetl数据源配置 + * @author: yangkai.shen + * @date: Created in 2018/11/14 17:15 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +public class BeetlConfig { + + /** + * Beetl需要显示的配置数据源,方可启动项目,大坑,切记! + */ + @Bean(name = "datasource") + public DataSource getDataSource(Environment env){ + HikariDataSource dataSource = new HikariDataSource(); + dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); + dataSource.setJdbcUrl(env.getProperty("spring.datasource.url")); + dataSource.setUsername(env.getProperty("spring.datasource.username")); + dataSource.setPassword(env.getProperty("spring.datasource.password")); + return dataSource; + } +} +``` + +## UserDao.java + +```java +/** + *

+ * UserDao + *

+ * + * @package: com.xkcoding.orm.beetlsql.dao + * @description: UserDao + * @author: yangkai.shen + * @date: Created in 2018/11/14 16:18 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Component +public interface UserDao extends BaseMapper { + +} +``` + +## UserServiceImpl.java + +```java +/** + *

+ * User Service + *

+ * + * @package: com.xkcoding.orm.beetlsql.service.impl + * @description: User Service + * @author: yangkai.shen + * @date: Created in 2018/11/14 16:28 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Service +@Slf4j +public class UserServiceImpl implements UserService { + + private final UserDao userDao; + + @Autowired + public UserServiceImpl(UserDao userDao) { + this.userDao = userDao; + } + + /** + * 新增用户 + * + * @param user 用户 + */ + @Override + public User saveUser(User user) { + userDao.insert(user, true); + return user; + } + + /** + * 批量插入用户 + * + * @param users 用户列表 + */ + @Override + public void saveUserList(List users) { + userDao.insertBatch(users); + } + + /** + * 根据主键删除用户 + * + * @param id 主键 + */ + @Override + public void deleteUser(Long id) { + userDao.deleteById(id); + } + + /** + * 更新用户 + * + * @param user 用户 + * @return 更新后的用户 + */ + @Override + public User updateUser(User user) { + if (ObjectUtil.isNull(user)) { + throw new RuntimeException("用户id不能为null"); + } + userDao.updateTemplateById(user); + return userDao.single(user.getId()); + } + + /** + * 查询单个用户 + * + * @param id 主键id + * @return 用户信息 + */ + @Override + public User getUser(Long id) { + return userDao.single(id); + } + + /** + * 查询用户列表 + * + * @return 用户列表 + */ + @Override + public List getUserList() { + return userDao.all(); + } + + /** + * 分页查询 + * + * @param currentPage 当前页 + * @param pageSize 每页条数 + * @return 分页用户列表 + */ + @Override + public PageQuery getUserByPage(Integer currentPage, Integer pageSize) { + return userDao.createLambdaQuery().page(currentPage, pageSize); + } +} +``` + +## UserServiceImplTest.java + +```java +/** + *

+ * User Service测试 + *

+ * + * @package: com.xkcoding.orm.beetlsql.service.impl + * @description: User Service测试 + * @author: yangkai.shen + * @date: Created in 2018/11/14 16:30 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Slf4j +public class UserServiceImplTest extends SpringBootDemoOrmBeetlsqlApplicationTests { + @Autowired + private UserService userService; + + @Test + public void saveUser() { + String salt = IdUtil.fastSimpleUUID(); + User user = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave3@xkcoding.com").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + + user = userService.saveUser(user); + Assert.assertTrue(ObjectUtil.isNotNull(user.getId())); + log.debug("【user】= {}", user); + } + + @Test + public void saveUserList() { + List users = Lists.newArrayList(); + for (int i = 5; i < 15; i++) { + String salt = IdUtil.fastSimpleUUID(); + User user = User.builder().name("testSave" + i).password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave" + i + "@xkcoding.com").phoneNumber("1730000000" + i).status(1).lastLoginTime(new DateTime()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + users.add(user); + } + userService.saveUserList(users); + Assert.assertTrue(userService.getUserList().size() > 2); + } + + @Test + public void deleteUser() { + userService.deleteUser(1L); + User user = userService.getUser(1L); + Assert.assertTrue(ObjectUtil.isNull(user)); + } + + @Test + public void updateUser() { + User user = userService.getUser(2L); + user.setName("beetlSql 修改后的名字"); + User update = userService.updateUser(user); + Assert.assertEquals("beetlSql 修改后的名字", update.getName()); + log.debug("【update】= {}", update); + } + + @Test + public void getUser() { + User user = userService.getUser(1L); + Assert.assertNotNull(user); + log.debug("【user】= {}", user); + } + + @Test + public void getUserList() { + List userList = userService.getUserList(); + Assert.assertTrue(CollUtil.isNotEmpty(userList)); + log.debug("【userList】= {}", userList); + } + + @Test + public void getUserByPage() { + List userList = userService.getUserList(); + PageQuery userByPage = userService.getUserByPage(1, 5); + Assert.assertEquals(5, userByPage.getList().size()); + Assert.assertEquals(userList.size(), userByPage.getTotalRow()); + log.debug("【userByPage】= {}", JSONUtil.toJsonStr(userByPage)); + } +} +``` + +## 参考 + +- BeetlSQL官方文档:http://ibeetl.com/guide/#beetlsql +- 开源项目:https://gitee.com/yangkb/springboot-beetl-beetlsql +- 博客:https://blog.csdn.net/flystarfly/article/details/82752597 \ No newline at end of file diff --git a/spring-boot-demo-orm-beetlsql/pom.xml b/spring-boot-demo-orm-beetlsql/pom.xml index 5ee1974..0d2c42c 100644 --- a/spring-boot-demo-orm-beetlsql/pom.xml +++ b/spring-boot-demo-orm-beetlsql/pom.xml @@ -56,6 +56,11 @@ hutool-all + + com.google.guava + guava + + org.projectlombok lombok diff --git a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/config/BeetlConfig.java b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/config/BeetlConfig.java index f87c129..2727160 100644 --- a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/config/BeetlConfig.java +++ b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/config/BeetlConfig.java @@ -23,6 +23,9 @@ import javax.sql.DataSource; @Configuration public class BeetlConfig { + /** + * Beetl需要显示的配置数据源,方可启动项目,大坑,切记! + */ @Bean(name = "datasource") public DataSource getDataSource(Environment env){ HikariDataSource dataSource = new HikariDataSource(); diff --git a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/entity/User.java b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/entity/User.java index 04cea60..71e2fd3 100644 --- a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/entity/User.java +++ b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/entity/User.java @@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.beetl.sql.core.annotatoin.Table; import java.io.Serializable; import java.util.Date; @@ -25,6 +26,7 @@ import java.util.Date; @NoArgsConstructor @AllArgsConstructor @Builder +@Table(name = "orm_user") public class User implements Serializable { private static final long serialVersionUID = -1840831686851699943L; diff --git a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/UserService.java b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/UserService.java index 76d8c9a..a0e7af1 100644 --- a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/UserService.java +++ b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/UserService.java @@ -1,6 +1,7 @@ package com.xkcoding.orm.beetlsql.service; import com.xkcoding.orm.beetlsql.entity.User; +import org.beetl.sql.core.engine.PageQuery; import java.util.List; @@ -22,8 +23,17 @@ public interface UserService { * 新增用户 * * @param user 用户 + * @return 保存的用户 */ - void saveUser(User user); + User saveUser(User user); + + + /** + * 批量插入用户 + * + * @param users 用户列表 + */ + void saveUserList(List users); /** * 根据主键删除用户 @@ -62,5 +72,5 @@ public interface UserService { * @param pageSize 每页条数 * @return 分页用户列表 */ - List getUserByPage(Integer currentPage, Integer pageSize); + PageQuery getUserByPage(Integer currentPage, Integer pageSize); } diff --git a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java index e794ea8..f4bad10 100644 --- a/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java +++ b/spring-boot-demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java @@ -1,9 +1,11 @@ package com.xkcoding.orm.beetlsql.service.impl; +import cn.hutool.core.util.ObjectUtil; import com.xkcoding.orm.beetlsql.dao.UserDao; import com.xkcoding.orm.beetlsql.entity.User; import com.xkcoding.orm.beetlsql.service.UserService; import lombok.extern.slf4j.Slf4j; +import org.beetl.sql.core.engine.PageQuery; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -39,8 +41,19 @@ public class UserServiceImpl implements UserService { * @param user 用户 */ @Override - public void saveUser(User user) { + public User saveUser(User user) { + userDao.insert(user, true); + return user; + } + /** + * 批量插入用户 + * + * @param users 用户列表 + */ + @Override + public void saveUserList(List users) { + userDao.insertBatch(users); } /** @@ -50,7 +63,7 @@ public class UserServiceImpl implements UserService { */ @Override public void deleteUser(Long id) { - + userDao.deleteById(id); } /** @@ -61,7 +74,11 @@ public class UserServiceImpl implements UserService { */ @Override public User updateUser(User user) { - return null; + if (ObjectUtil.isNull(user)) { + throw new RuntimeException("用户id不能为null"); + } + userDao.updateTemplateById(user); + return userDao.single(user.getId()); } /** @@ -82,7 +99,7 @@ public class UserServiceImpl implements UserService { */ @Override public List getUserList() { - return null; + return userDao.all(); } /** @@ -93,7 +110,7 @@ public class UserServiceImpl implements UserService { * @return 分页用户列表 */ @Override - public List getUserByPage(Integer currentPage, Integer pageSize) { - return null; + public PageQuery getUserByPage(Integer currentPage, Integer pageSize) { + return userDao.createLambdaQuery().page(currentPage, pageSize); } } diff --git a/spring-boot-demo-orm-beetlsql/src/main/resources/application.yml b/spring-boot-demo-orm-beetlsql/src/main/resources/application.yml index 29d5418..65520e5 100644 --- a/spring-boot-demo-orm-beetlsql/src/main/resources/application.yml +++ b/spring-boot-demo-orm-beetlsql/src/main/resources/application.yml @@ -4,22 +4,23 @@ spring: username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver - type: com.zaxxer.hikari.HikariDataSource - initialization-mode: always - continue-on-error: true - schema: - - "classpath:db/schema.sql" - data: - - "classpath:db/data.sql" - hikari: - minimum-idle: 5 - connection-test-query: SELECT 1 FROM DUAL - maximum-pool-size: 20 - auto-commit: true - idle-timeout: 30000 - pool-name: SpringBootDemoHikariCP - max-lifetime: 60000 - connection-timeout: 30000 +#### beetlsql starter不能开启下面选项 +# type: com.zaxxer.hikari.HikariDataSource +# initialization-mode: always +# continue-on-error: true +# schema: +# - "classpath:db/schema.sql" +# data: +# - "classpath:db/data.sql" +# hikari: +# minimum-idle: 5 +# connection-test-query: SELECT 1 FROM DUAL +# maximum-pool-size: 20 +# auto-commit: true +# idle-timeout: 30000 +# pool-name: SpringBootDemoHikariCP +# max-lifetime: 60000 +# connection-timeout: 30000 logging: level: com.xkcoding: debug @@ -27,5 +28,11 @@ logging: beetl: enabled: false beetlsql: + enabled: true + sqlPath: /sql daoSuffix: Dao - basePackage: com.xkcoding.orm.beetlsql.dao \ No newline at end of file + basePackage: com.xkcoding.orm.beetlsql.dao + dbStyle: org.beetl.sql.core.db.MySqlStyle + nameConversion: org.beetl.sql.core.UnderlinedNameConversion +beet-beetlsql: + dev: true \ No newline at end of file diff --git a/spring-boot-demo-orm-beetlsql/src/test/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImplTest.java b/spring-boot-demo-orm-beetlsql/src/test/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImplTest.java index b798bdf..31529b7 100644 --- a/spring-boot-demo-orm-beetlsql/src/test/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImplTest.java +++ b/spring-boot-demo-orm-beetlsql/src/test/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImplTest.java @@ -1,14 +1,23 @@ package com.xkcoding.orm.beetlsql.service.impl; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.crypto.SecureUtil; +import cn.hutool.json.JSONUtil; import com.xkcoding.orm.beetlsql.SpringBootDemoOrmBeetlsqlApplicationTests; import com.xkcoding.orm.beetlsql.entity.User; import com.xkcoding.orm.beetlsql.service.UserService; import lombok.extern.slf4j.Slf4j; -import org.beetl.sql.core.SQLManager; +import org.assertj.core.util.Lists; +import org.beetl.sql.core.engine.PageQuery; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + /** *

* User Service测试 @@ -29,14 +38,40 @@ public class UserServiceImplTest extends SpringBootDemoOrmBeetlsqlApplicationTes @Test public void saveUser() { + String salt = IdUtil.fastSimpleUUID(); + User user = User.builder().name("testSave3").password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave3@xkcoding.com").phoneNumber("17300000003").status(1).lastLoginTime(new DateTime()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + + user = userService.saveUser(user); + Assert.assertTrue(ObjectUtil.isNotNull(user.getId())); + log.debug("【user】= {}", user); + } + + @Test + public void saveUserList() { + List users = Lists.newArrayList(); + for (int i = 5; i < 15; i++) { + String salt = IdUtil.fastSimpleUUID(); + User user = User.builder().name("testSave" + i).password(SecureUtil.md5("123456" + salt)).salt(salt).email("testSave" + i + "@xkcoding.com").phoneNumber("1730000000" + i).status(1).lastLoginTime(new DateTime()).createTime(new DateTime()).lastUpdateTime(new DateTime()).build(); + users.add(user); + } + userService.saveUserList(users); + Assert.assertTrue(userService.getUserList().size() > 2); } @Test public void deleteUser() { + userService.deleteUser(1L); + User user = userService.getUser(1L); + Assert.assertTrue(ObjectUtil.isNull(user)); } @Test public void updateUser() { + User user = userService.getUser(2L); + user.setName("beetlSql 修改后的名字"); + User update = userService.updateUser(user); + Assert.assertEquals("beetlSql 修改后的名字", update.getName()); + log.debug("【update】= {}", update); } @Test @@ -48,9 +83,17 @@ public class UserServiceImplTest extends SpringBootDemoOrmBeetlsqlApplicationTes @Test public void getUserList() { + List userList = userService.getUserList(); + Assert.assertTrue(CollUtil.isNotEmpty(userList)); + log.debug("【userList】= {}", userList); } @Test public void getUserByPage() { + List userList = userService.getUserList(); + PageQuery userByPage = userService.getUserByPage(1, 5); + Assert.assertEquals(5, userByPage.getList().size()); + Assert.assertEquals(userList.size(), userByPage.getTotalRow()); + log.debug("【userByPage】= {}", JSONUtil.toJsonStr(userByPage)); } } \ No newline at end of file