# Magician-JDBC **Repository Path**: jmagician/Magician-JDBC ## Basic Information - **Project Name**: Magician-JDBC - **Description**: Magician-JDBC 是Magician的官方JDBC组件,可以很快捷的实现数据库操作 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: http://magician-io.com - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2021-04-13 - **Last Updated**: 2024-11-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

Magician-JDBC ·

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. ## Documentation [https://magician-io.com/db](https://magician-io.com/db) ## Example ### Importing dependencies ```xml com.github.yuyenews Magician-JDBC 2.0.5 mysql mysql-connector-java 8.0.20 com.alibaba druid 1.2.5 org.slf4j slf4j-jdk14 1.7.12 ``` ### Creating a data source ```java // 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); ``` ### Adding a data source to JDBC ```java // 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 ``` ### Single Table Operations Search by condition ```java List conditionList = ConditionBuilder.createCondition() .add("id > ?", 10) .add("and (name = ? or age > ?)", "bee", 10)) .add("order by create_time", Condition.NOT_WHERE)) .build(); List result = JDBCTemplate.get().select("table name", conditionList, Map.class); ``` Delete by condition ```java List conditionList = ConditionBuilder.createCondition() .add("id = ?", 10) .build(); JDBCTemplate.get().delete("table name", conditionList); ``` Insert a piece of data ```java DemoPO demoPO = new DemoPo(); demoPO.setName("bee"); demoPo.setAge(10); JDBCTemplate.get().insert("table name", demoPO); ``` Modify data ```java DemoPO demoPO = new DemoPo(); demoPO.setName("bee"); demoPo.setAge(10); List conditionList = ConditionBuilder.createCondition() .add("id > ?", 10) .add("and name = ?", "bee")) .build(); JDBCTemplate.get().update("table name", demoPO, conditionList); ``` ## Write your own sql Select ```java DemoPO demoPO = new DemoPo(); demoPO.setName("bee"); demoPo.setAge(10); List result = JDBCTemplate.get().selectList("select * from xxx where name={name} and age={age}", demoPO, Map.class); ``` insert, delete, update ```java 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