test(添加SQL的工具类)

master
031026 2024-05-11 18:45:00 +08:00
commit 6e4f6f1c31
8 changed files with 217 additions and 12 deletions

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.muyu</groupId>
<artifactId>muyu-common</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>muyu-common-method</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,23 @@
package com.muyu.method.SQL;
import java.util.Map;
/**
* SQL
*/
public interface SqlMethod<K, V, C> {
// 插入SQL接口
String insertData(K key, Map<K, V> value);
// 查询SQL接口
String selectData(K key,Map<K, V> value ,C condition);
// 修改SQL接口
String updateData(K key, Map<K, V> value, C condition,Map<K, V> update);
// 删除SQL接口
String deleteData(K key, Map<K, V> value, C condition);
}

View File

@ -0,0 +1,120 @@
package com.muyu.method.SQL.implementation;
import com.muyu.method.SQL.SqlMethod;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* SQL
*
* @ClassName SqlMethodImplementation
* @Author AnNan.Wang
* @Date 2024/5/12 14:29
*/
@Component
public class SqlMethodImplementation implements SqlMethod<String, Object, String> {
// 插入SQL接口
@Override
public String insertData(String key, Map<String, Object> value) {
StringBuilder columns = new StringBuilder();
StringBuilder values = new StringBuilder();
for (String column : value.keySet()) {
columns.append(column).append(",");
values.append("?,");
}
//删除最后一个逗号
columns.deleteCharAt(columns.length() - 1);
values.deleteCharAt(values.length() - 1 );
String sql = "INSERT INTO " + key + " (" + columns + ") VALUES (" + values + ")";
return sql;
}
// 查询SQL接口
@Override
public String selectData(String key,Map<String, Object> value ,String condition) {
StringBuilder sql = new StringBuilder("SELECT * FROM ").append(key);
// 如果有条件,构建 WHERE 子句
if (condition != null && !condition.isEmpty()) {
StringBuilder whereClause = new StringBuilder(" WHERE ");
int index = 0;
for (String column : value.keySet()) {
whereClause.append(column).append(" = ?");
// 如果不是最后一个条件,则添加 AND 连接符
if (index < value.size() - 1) {
whereClause.append(" AND ");
}
index++;
}
sql.append(whereClause);
}
return sql.toString();
}
// 修改SQL接口
@Override
public String updateData(String key, Map<String, Object> value, String condition, Map<String, Object> updateData) {
StringBuilder setClause = new StringBuilder();
for (String column : value.keySet()) {
setClause.append(column).append("=?,");
}
// 删除最后一个逗号
setClause.deleteCharAt(setClause.length() - 1);
String sql = "UPDATE " + key + " SET " + setClause;
if (condition != null && !condition.isEmpty()) {
sql += " WHERE ";
}
int index = 0;
StringBuilder builder = new StringBuilder();
for (String column : updateData.keySet()) {
builder.append(column).append(" = ?");
if (index < value.size() - 1) {
builder.append(" AND ");
}
index++;
}
sql += builder;
return sql;
}
// 删除SQL接口
@Override
public String deleteData(String key, Map<String, Object> value, String condition) {
StringBuilder whereClause = new StringBuilder();
String sql = "DELETE FROM " + key;
// 添加自定义条件
if (condition != null && !condition.isEmpty()) {
whereClause.append(" WHERE ").append(condition);
}
// 添加条件参数
if (value != null && !value.isEmpty()) {
if (whereClause.length() == 0) {
whereClause.append(" WHERE ");
} else {
whereClause.append(" AND ");
}
int index = 0;
for (String column : value.keySet()) {
whereClause.append(column).append(" = ?");
if (index < value.size() - 1) {
whereClause.append(" AND ");
}
index++;
}
}
sql += whereClause.toString();
return sql;
}
}

View File

@ -0,0 +1 @@
com.muyu.method.SQL.implementation.SqlMethodImplementation

View File

@ -19,6 +19,7 @@
<module>muyu-common-datasource</module>
<module>muyu-common-system</module>
<module>muyu-common-cache</module>
<module>muyu-common-method</module>
</modules>
<artifactId>muyu-common</artifactId>

View File

@ -40,5 +40,16 @@
<groupId>com.muyu</groupId>
<artifactId>muyu-common-cache</artifactId>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-common-method</artifactId>
</dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-common-method</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
</project>

View File

@ -1,6 +1,7 @@
package com.muyu.source.clinet.pool;
import com.alibaba.druid.pool.DruidDataSource;
import com.muyu.method.SQL.implementation.SqlMethodImplementation;
import com.muyu.source.clinet.factory.DataSourceSingleton;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
@ -8,10 +9,9 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
/**
* druid()
@ -23,6 +23,10 @@ import java.sql.Statement;
@Log4j2
public class DruidDetails implements ApplicationRunner {
Connection connection=null;
@Autowired
private SqlMethodImplementation sqlMethodImplementation;
@Override
public void run(ApplicationArguments args) throws Exception {
@ -48,23 +52,47 @@ public class DruidDetails implements ApplicationRunner {
source.setMaxWait(3000);
//获取数据连接
Connection connection=null;
try {
connection=source.getConnection();
//执行查询操作
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM t_user");
//// Map<String, Object> value ,String condition
// String tableName = "t_user";
// Map<String, Object> map = new HashMap<>();
// map.put("age",13);
// String condition="1";
// //查询
// String SQL = sqlMethodImplementation.selectData(tableName, map, condition);
//插入
// String SQL = sqlMethodImplementation.insertData(tableName, map);
//删除
// String SQL = sqlMethodImplementation.deleteData(tableName, map, condition);
while (resultSet.next()) {
int userId = resultSet.getInt("user_id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
//修改
// Map<String, Object> hashMap = new HashMap<>();
// hashMap.put("name","李四");
// String SQL = sqlMethodImplementation.updateData(tableName, map, condition,hashMap);
// System.out.println(SQL);
// PreparedStatement prepared = connection.prepareStatement(SQL);
//
// prepared.setInt(1,19);
// prepared.setString(2,"李四");
//
// int i = prepared.executeUpdate();
// System.out.println(i + " 行已插入.");
log.info("user_id:{},name:{},age:{}",userId,name,age);
}
// while (resultSet.next()) {
// int userId = resultSet.getInt("user_id");
// String name = resultSet.getString("name");
// int age = resultSet.getInt("age");
//
// log.info("user_id:{},name:{},age:{}",userId,name,age);
//
// }
} catch (Exception e) {
e.printStackTrace();
} finally {

View File

@ -75,6 +75,7 @@
<artifactId>druid</artifactId>
<version>1.2.6</version> <!-- 使用最新的 Druid 版本 -->
</dependency>
</dependencies>
<build>