Magician-JDBCMagician 的官方 JDBC 組件
Magician-JDBC 是Magician的官方JDBC組件,支持多數(shù)據(jù)源,無sql單表操作,復雜操作可以寫sql,事務管理等
文檔
示例
導入依賴
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician-JDBC</artifactId>
<version>last version</version>
</dependency>
<!-- mysql driver package -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- druid connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.5</version>
</dependency>
<!-- This is the log package, which supports any package that can be bridged with slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency>
創(chuàng)建數(shù)據(jù)源
// 這里用druid做示例,實際上可以支持任意實現(xiàn)了DataSource接口的連接池
DruidDataSource dataSource = new DruidDataSource();
Properties properties = new Properties();
properties.put("druid.name", "local");
properties.put("druid.url", "jdbc:mysql://127.0.0.1:3306/martian-test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true&useSSL=false");
properties.put("druid.username", "root");
properties.put("druid.password", "123456");
properties.put("druid.driverClassName", Driver.class.getName());
dataSource.setConnectProperties(properties);
將數(shù)據(jù)源添加到JDBC
// Create JDBC, it is recommended to execute it only once when the project starts
MagicianJDBC.createJDBC()
.addDataSource("a", dataSource)// Add data source, this method can be called multiple times to add multiple data sources
.defaultDataSourceName("a");// Set the name of the default data source
單表操作
按條件查詢
List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id > ?", 10));
conditionList.add(Condition.get("and name = ?", 100));
conditionList.add(Condition.get("order by create_time", Condition.NOT_WHERE));
List<Map> result = JDBCTemplate.get().select("表名", conditionList, Map.class);
按條件刪除
List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id > ?", 10));
conditionList.add(Condition.get("and name = ?", 100));
conditionList.add(Condition.get("order by create_time", Condition.NOT_WHERE));
JDBCTemplate.get().delete("表名", conditionList);
插入一條數(shù)據(jù)
DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);
JDBCTemplate.get().insert("表名", demoPO);
修改數(shù)據(jù)
DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);
List<Condition> conditionList = new ArrayList<>();
conditionList.add(Condition.get("id = ?", 10));
conditionList.add(Condition.get("and name = ?", 100));
JDBCTemplate.get().update("表名", demoPO, conditionList);
自己寫sql
查詢
DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);
List<Map> result = JDBCTemplate.get().selectList("select * from xxx where name={name} and age={age}", demoPO, Map.class);
增刪改
DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);
JDBCTemplate.get().exec("update xxx set xxx = {xxx}, ccc = {ccc} where name={name} and age={age}", demoPO);
除此之外,還支持事務管理 和分頁查詢,詳情可以查看文檔
評論
圖片
表情
