Skip to content

Commit

Permalink
Merge pull request #1 from pengweizhong/dev
Browse files Browse the repository at this point in the history
更新版本
  • Loading branch information
pengweizhong committed Feb 22, 2021
2 parents ddb0c5d + b5e2e07 commit 384478c
Show file tree
Hide file tree
Showing 16 changed files with 446 additions and 460 deletions.
185 changes: 129 additions & 56 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,89 @@
# 单表动态查询 Dynamic-sql

## 版本号 1.0.3
## 版本号 1.1.0
```xml
<dependency>
<groupId>com.pengwz</groupId>
<artifactId>dynamic-sql</artifactId>
<version>1.0.3</version>
<version>1.1.0</version>
</dependency>
```
### 本次版本变更内容
1、修复groupBy()函数类字段不能正确匹配表字段的问题
2、修复groupBy()函数代码编译警告问题
3、inertMany()方法更名为batchInsert()
4、新增insertOrUpdate()、batchInsertOrUpdate()函数
5、新增updateActive()函数
6、优化了一些其他问题
1、优化了log日志
2、修复了一些bug
3、支持数据库连接池

### 项目简述
1、基于JDBC单表动态增删改查,可单独启动,不依赖环境
2、目前仅支持Mysql
3、支持多数据源(目前不支持默认数据源,需要明确指定)
4、支持事务(目前不提供主动事务,考虑后期优化,可以使用其他框架加入事务,如Spring事务)
5、目前不支持数据库连接池(等下期优化;或自己写连接池,或接入优秀的框架)
3、支持多数据源
4、支持事务
5、支持数据库连接池

### 本次发布的功能
### 主要功能描述
查询、增加、删除、修改
#### 下面举一个查询的示例
#### 快速开始

##### 准备工作
1、新建自定义的数据源,实现DataSourceConfig接口,并重写getProperties()方法,此处命名为MyDBConfig,如:
```java
public class MyDBConfig implements DataSourceConfig {
@Override
public Properties getProperties() {
//必须的配置
Properties properties = new Properties();
properties.setConfig(DRIVER, "com.mysql.jdbc.Driver");
properties.setConfig(USERNAME, "root");
properties.setConfig(PASSWORD, "pengwz");
properties.setConfig(PORT, "3306");
properties.setConfig(HOST, "127.0.0.1");
properties.setConfig(DATABASE, "dynamic");
//其他参数,比如设置时区,字符集等
Map<String, String> otherConfigMap = new HashMap<>();
otherConfigMap.put("serverTimezone", "GMT%2B8");
otherConfigMap.put("useUnicode", "true");
otherConfigMap.put("characterEncoding", "utf-8");
otherConfigMap.put("rewriteBatchedStatements", "true");
properties.setOtherConfigMap(otherConfigMap);
return properties;
public DataSource getDataSource() {
MysqlDataSource ds = new MysqlDataSource();
ds.setUrl("jdbc:mysql://127.0.0.1:3306/dynamic?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT%2B8&characterEncoding=utf-8");
ds.setUser("root");
ds.setPassword("pengwz");
return ds;
}
}
```
2、在指定的数据源中,新建一个表,此处命名为t_user,然后给这个表初始化两条记录,用于测试数据。
或者使用第三方的数据库连接池,如:阿里巴巴的Druid
引入Druid的依赖包
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
```
配置Druid数据源
```java
public class MyDBConfig implements DataSourceConfig {
@Override
public DataSource getDataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setUrl("jdbc:mysql://127.0.0.1:3306/dynamic?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT%2B8&characterEncoding=utf-8");
ds.setUsername("root");
ds.setPassword("pengwz");
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setInitialSize(3);
ds.setMaxActive(10);
ds.setMinIdle(5);
ds.setValidationQuery("select 1");
ds.setTestOnBorrow(true);
ds.setTestOnReturn(false);
ds.setUseUnfairLock(true);
ds.setTestWhileIdle(true);
ds.setMinEvictableIdleTimeMillis(10 * 60 * 1000L);
ds.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000L);
return ds;
}
}
```
2、在指定的数据源中,新建一个表,此处命名为t_user,此处为phone列创建了一个索引。
```mysql
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(100) DEFAULT NULL COMMENT '名称',
`sex` char(2) DEFAULT NULL COMMENT '性别',
`phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`birthday` date DEFAULT NULL COMMENT '生日',
`create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO t_user (id, name, sex, birthday, create_date, update_date) VALUES (1, 'tom', '', '1993-07-14', null, null);
INSERT INTO t_user (id, name, sex, birthday, create_date, update_date) VALUES (2, 'jerry', '', '1994-01-22', null, null);
PRIMARY KEY (`id`),
UNIQUE KEY `t_user_un` (`phone`)
) ENGINE=InnoDB AUTO_INCREMENT=328 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

```
4、新建一个实体类,用于和数据库交互,此处命名为UserEntity
Expand All @@ -80,6 +97,7 @@ public class UserEntity {
private String username;
private String sex;
private LocalDate birthday;
private String phone;
private LocalDateTime createDate;
private LocalDateTime updateDate;
/* getter and setter */
Expand All @@ -93,40 +111,95 @@ public class UserEntity {
@Column是可选项,表示该属性所对应表的字段,若不声明@Column,则默认使用驼峰命名规则匹配表字段,若声明则以注解内值为准。
此外,Dynamic-sql自带类型转换功能,比如当您的表字段类型为date,在实体类希望使用LocalDate接收,程序会自动转换类型。

##### 查询示例
1、查询全部
##### 主要功能示例
1、批量新增数据
新增10条测试数据
```java
List<UserEntity> select = BraveSql.build(UserEntity.class).select();
select.forEach(System.out::println);
@Test
public void batchInsert() {
DynamicSql<UserEntity> dynamicSql = DynamicSql.createDynamicSql();
List<UserEntity> userEntities = new ArrayList<>();
for (int i = 0; i < 10; i++) {
UserEntity data = new UserEntity();
data.setSex("");
data.setUsername("海绵宝宝" + i);
data.setBirthday(LocalDate.now());
data.setCreateDate(LocalDateTime.now());
data.setUpdateDate(LocalDateTime.now());
userEntities.add(data);
}
BraveSql.build(dynamicSql, UserEntity.class).batchInsert(userEntities);
userEntities.forEach(System.out::println);
}

```
运行结果
UserEntity{id=1, username='tom', birthday=1993-07-14, createDate=2021-01-23T01:09:51, updateDate=2021-01-23T01:09:51}
UserEntity{id=2, username='jerry', birthday=1994-01-22, createDate=2021-01-23T01:10:23, updateDate=2021-01-23T01:10:23}
2、带条件的查询,如查询性别等于男的数据
UserEntity{id=1, username='海绵宝宝0', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=2, username='海绵宝宝1', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=3, username='海绵宝宝2', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=4, username='海绵宝宝3', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=5, username='海绵宝宝4', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=6, username='海绵宝宝5', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=7, username='海绵宝宝6', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=8, username='海绵宝宝7', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=9, username='海绵宝宝8', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}
UserEntity{id=10, username='海绵宝宝9', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10.045, updateDate=2021-02-22T15:29:10.045}ntity{id=2, username='jerry', birthday=1994-01-22, createDate=2021-01-23T01:10:23, updateDate=2021-01-23T01:10:23}
2、查询全部的数据
```java
DynamicSql<UserEntity> dynamicSql = DynamicSql.createDynamicSql();
dynamicSql.andEqualTo(UserEntity::getSex, "");
List<UserEntity> select = BraveSql.build(dynamicSql, UserEntity.class).select();
select.forEach(System.out::println);
@Test
public void selectAll(){
List<UserEntity> select = BraveSql.build(UserEntity.class).select();
select.forEach(System.out::println);
}
```
运行结果
UserEntity{id=1, username='tom', sex='男', birthday=1993-07-14, createDate=2021-01-23T01:09:51, updateDate=2021-01-23T01:09:51}
3、根据主键查询
UserEntity{id=1, username='海绵宝宝0', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=2, username='海绵宝宝1', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=3, username='海绵宝宝2', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=4, username='海绵宝宝3', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=5, username='海绵宝宝4', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=6, username='海绵宝宝5', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=7, username='海绵宝宝6', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=8, username='海绵宝宝7', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=9, username='海绵宝宝8', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
UserEntity{id=10, username='海绵宝宝9', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}3、根据主键查询
3、根据主键查询
查询主键等于1的数据
```java
UserEntity userEntity = BraveSql.build(UserEntity.class).selectByPrimaryKey(2);
System.out.println(userEntity);
@Test
public void selectByPrimaryKey(){
UserEntity userEntity = BraveSql.build(UserEntity.class).selectByPrimaryKey(1);
System.out.println(userEntity);
}
```
运行结果
UserEntity{id=2, username='jerry', sex='女', birthday=1994-01-22, createDate=2021-01-23T01:10:23, updateDate=2021-01-23T01:10:23}
4、分页查询
UserEntity{id=1, username='海绵宝宝0', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
4、根据条件查询
查询用户名字是海绵宝宝6的数据
```java
PageInfo<UserEntity> userEntityPageInfo = BraveSql.build(UserEntity.class).selectPageInfo(1, 1);
@Test
public void selectByCondition(){
DynamicSql<UserEntity> dynamicSql = DynamicSql.createDynamicSql();
dynamicSql.andEqualTo(UserEntity::getUsername,"海绵宝宝6");
UserEntity userEntity = BraveSql.build(dynamicSql, UserEntity.class).selectSingle();
System.out.println(userEntity);
}
```
运行结果
UserEntity{id=7, username='海绵宝宝6', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}
5、分页查询
查询第二页的两条数据
```java
@Test
public void selectPageInfo() {
PageInfo<UserEntity> userEntityPageInfo = BraveSql.build(UserEntity.class).selectPageInfo(2, 2);
System.out.println(userEntityPageInfo);
}
```
运行结果
PageInfo{pageIndex=1, pageSize=1, realPageSize=1, totalPages=2, totalSize=2, resultList=[UserEntity{id=1, username='tom', sex='男', birthday=1993-07-14, createDate=2021-01-23T01:09:51, updateDate=2021-01-23T01:09:51}]}
PageInfo{pageIndex=2, pageSize=2, realPageSize=2, totalPages=5, totalSize=10, resultList=[UserEntity{id=3, username='海绵宝宝2', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}, UserEntity{id=4, username='海绵宝宝3', sex='男', birthday=2021-02-22, phone='null', createDate=2021-02-22T15:29:10, updateDate=2021-02-22T15:29:10}]}

其中,DynamicSql主要用于创建where条件,BraveSql用于操作数据库。
(文档待更新... ... )


30 changes: 18 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.pengwz</groupId>
<artifactId>dynamic-sql</artifactId>
<version>1.0.3</version>
<version>1.1.0</version>
<name>pengwz-dynamic-sql</name>

<inceptionYear>2020</inceptionYear>
Expand Down Expand Up @@ -55,6 +55,18 @@


<dependencies>
<!-- 常用的工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!-- bean copy utils-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -65,18 +77,13 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>test</scope>
</dependency>
<!-- 常用的工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!-- bean copy utils-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down Expand Up @@ -131,7 +138,6 @@
</configuration>
</plugin>

<!-- 这个不知道咋说,不太理解-->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/pengwz/dynamic/anno/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@

Class<?> dataSourceClass() default Void.class;

//注解嵌套
// Reference reference() default @Reference(next=true);
}
5 changes: 0 additions & 5 deletions src/main/java/com/pengwz/dynamic/config/DBConfigEnum.java

This file was deleted.

40 changes: 6 additions & 34 deletions src/main/java/com/pengwz/dynamic/config/DataSourceConfig.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
package com.pengwz.dynamic.config;

import com.pengwz.dynamic.exception.BraveException;
import com.pengwz.dynamic.utils.StringUtils;
import javax.sql.DataSource;

import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;

public interface DataSourceConfig /*extends CommonDataSource*/ {

Properties getProperties();

class Properties {
private EnumMap<DBConfigEnum, String> configEnumMap = new EnumMap<>(DBConfigEnum.class);
private Map<String, String> otherConfigMap;

public EnumMap<DBConfigEnum, String> getConfigEnumMap() {
return configEnumMap;
}

public void setConfig(DBConfigEnum configEnum, String value) {
if (StringUtils.isEmpty(value)) {
throw new BraveException("value不可为空");
}
configEnumMap.put(configEnum, value);
}

public Map<String, String> getOtherConfigMap() {
return otherConfigMap;
}

public void setOtherConfigMap(Map<String, String> otherConfigMap) {
this.otherConfigMap = otherConfigMap;
}

}
public interface DataSourceConfig {
/**
* 若没有自定义数据源 ,请使用mysql链接驱动包自带的 MysqlDataSource
*/
DataSource getDataSource();

}

0 comments on commit 384478c

Please sign in to comment.