Magician-JDBC is the official JDBC component of Magician, supporting multiple data sources, no sql single table operations, complex operations can write sql, transaction management, etc.
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician-JDBC</artifactId>
<version>2.0.3</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>
// Here is an example using druid, which can actually support any connection pool that implements the DataSource interface
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);
// 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
Search by condition
List<Condition> conditionList = ConditionBuilder.createCondition()
.add("id > ?", 10)
.add("and (name = ? or age > ?)", "bee", 10))
.add("order by create_time", Condition.NOT_WHERE))
.build();
List<Map> result = JDBCTemplate.get().select("table name", conditionList, Map.class);
Delete by condition
List<Condition> conditionList = ConditionBuilder.createCondition()
.add("id = ?", 10)
.build();
JDBCTemplate.get().delete("table name", conditionList);
Insert a piece of data
DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);
JDBCTemplate.get().insert("table name", demoPO);
Modify data
DemoPO demoPO = new DemoPo();
demoPO.setName("bee");
demoPo.setAge(10);
List<Condition> conditionList = ConditionBuilder.createCondition()
.add("id > ?", 10)
.add("and name = ?", "bee"))
.build();
JDBCTemplate.get().update("table name", demoPO, conditionList);
Select
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);
insert, delete, update
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);
In addition, transaction management and paging are also supported, see the documentation for details