MyBatis的好用大家就不用说了吧,本文使用ruid连接池以及Spring JDBC实现极简易的ORM,不多说,上代码
maven配置
<?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>
<groupId>org.example</groupId>
<artifactId>db</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.34.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</project>
Db类
package db;
import db.utils.JdbcUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
public class Db {
String table;
String field;
HashMap<String,Object> option;
String sql;
Boolean fetchSql = false;
/**
* 构造函数
*/
public Db(){
this.option = new HashMap<>();
}
/**
* 设置表
*/
public Db table(String table){
this.table = table;
return this;
}
/**
* 设置字段
*/
public Db filed(String field){
this.field = field;
return this;
}
/**
* 生成sql而不执行
*/
public Db fetchSql(){
this.fetchSql = true;
return this;
}
/**
* 获取结果
* @return
*/
public <T> Object result(String type){
if (this.sql.trim() == ""){
this.buildSql();
}
if (this.fetchSql){
return this.sql;
}
JdbcTemplate template = new JdbcTemplate(JdbcUtils.getDataSource());
if( type == "select"){
return template.queryForList(this.sql);
}else if (type == "find"){
return template.queryForMap(this.sql);
}
else{
return template.update(this.sql);
}
}
/**
* select查询
* @return
*/
public Object select(){
return this.result("select");
}
/**
* find查询
* @return
*/
public Object find(){
try{
return this.result("find");
}catch (Exception e){
return new HashMap<String,String>();
}
}
/**
* query查询
* @return
*/
public Object query(String sql){
this.sql = sql;
return this.result("select");
}
/**
* queryUpdate操作
* @return
*/
public Object queryUpdate(String sql){
this.sql = sql;
return this.result("update");
}
/**
* 获取最后生成的sql
* @return
*/
public String getLastSql(){
return this.sql;
}
/**
* 设置需要查询的字段
* @return
*/
public Db field(String field){
this.option.put("field"," " + field + " ");
return this;
}
/**
* 构造where条件
* @return
*/
public Db where(HashMap<String,String> map){
ArrayList<HashMap> arrayHash = new ArrayList<HashMap>();
if(option.containsKey("where")){
arrayHash = (ArrayList<HashMap>)option.get("where");
}
arrayHash.add(map);
option.put("where", arrayHash);
return this;
}
/**
* 构造order
* @return
*/
public Db order(String order){
option.put("order", " order by " + order + " ");
return this;
}
/**
* 构造limit
* @return
*/
public Db limit(String limit){
option.put("limit", limit);
return this;
}
/**
* 构造数据层
* @return
*/
public Db data(String data){
option.put("data", data);
return this;
}
/**
* 生成where部分
* @return
*/
public String buildWhere(){
String where = "";
ArrayList<HashMap<String,String>> mapList = (ArrayList<HashMap<String, String>>)option.get("where");
for (int i = 0; i < mapList.size(); i++) {
HashMap<String, String> map = mapList.get(i);
for (String key : map.keySet()) {
where += " `" + key + "`='" + map.get(key) + "' and";
}
}
return " where " + where.substring(0,where.lastIndexOf("and"));
}
/**
* 构造sql
* @return
*/
public void buildSql(){
String where = "";
String order = "";
String limit = "";
String field = "";
if(option.containsKey("field")){
field = (String) option.get("field");
}
if(option.containsKey("where")){
where = this.buildWhere();
}
if(option.containsKey("order")){
order = (String) option.get("order");
}
if(option.containsKey("limit")){
limit = (String) option.get("limit");
}
this.sql = "select " + field + "from " + this.table + where + order + limit;
}
/**
* 插入数据
* @return
*/
public Object insert(HashMap<String,String> map){
String key = "";
String value = "";
for (String k : map.keySet()) {
key += "`" + k + "`,";
value += "'" + map.get(k) + "',";
}
key = key.substring(0,key.lastIndexOf(","));
value = value.substring(0,value.lastIndexOf(","));
this.sql = "Insert into " + this.table + "(" + key + ") VALUES (" + value + ")";
return this.result("update");
}
/**
* 插入数据
* @return
*/
public Object insertAll(ArrayList<HashMap<String,String>> mapList){
String key = "";
String value = "";
String valueAll = "";
HashMap<String,String> map = mapList.get(0);
for (String k : map.keySet()) {
key += "`" + k + "`,";
}
for (int i = 0; i < mapList.size(); i++) {
HashMap<String, String> sonMap = mapList.get(i);
value = "";
for (String kk : sonMap.keySet()) {
value += "'" + sonMap.get(kk) + "',";
}
value = "(" + value.substring(0,value.lastIndexOf(",")) + "),";
valueAll += value;
}
key = key.substring(0,key.lastIndexOf(","));
valueAll = valueAll.substring(0,valueAll.lastIndexOf(","));
this.sql = "Insert into " + this.table + "(" + key + ") VALUES " + valueAll;
return this.result("update");
}
}
单元测试
import db.Db;
import db.utils.JdbcUtils;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JdbcTplTest {
@Test
public void test(){
// JdbcTemplate j = new JdbcTemplate(JdbcUtils.getDataSource());
// System.out.println(j);
// System.out.println(j.queryForList("select * from teacher"));
Db db = new Db();
//System.out.println(db.fetchSql().queryUpdate("update teacher set name='哈哈哈jjjj' where id =2"));;
HashMap<String,String> where1 = new HashMap<String,String>();
HashMap<String,String> where2 = new HashMap<String,String>();
HashMap<String,String> where3 = new HashMap<String,String>();
where1.put("id","40");
where2.put("name","哈哈哈");
where3.put("id","30");
HashMap<String,String> add = new HashMap<String,String>();
add.put("name","陈肥肥");
HashMap<String,String> add1 = new HashMap<String,String>();
add1.put("name","陈中中");
ArrayList< HashMap<String,String>> mapList = new ArrayList< HashMap<String,String>>();
mapList.add(add);
mapList.add(add1);
System.out.println(mapList);
//String sql = db.table("teacher").field("*").where(where1).order("id desc").find();
//System.out.println(db.table("teacher").field("*").where(where1).order("id desc").select());
//System.out.println(db.table("teacher").fetchSql().insert(add));
System.out.println(db.table("teacher").fetchSql().insertAll(mapList));
}
}
执行结果