ソースを参照

增加Dao和SQL管理的使用范例

huangbo 5 年 前
コミット
836af082ce

+ 15 - 0
ipu-cache-example/pom.xml

@ -8,6 +8,21 @@
8 8
	<artifactId>ipu-cache-example</artifactId>
9 9
	<version>1.0</version>
10 10
11
	<repositories>
12
		<repository>
13
			<id>ipu</id>
14
			<name>ipu repository</name>
15
			<url>http://114.215.100.48:9090/nexus/content/groups/public/</url>
16
			<releases>
17
				<enabled>true</enabled>
18
			</releases>
19
			<snapshots>
20
				<enabled>true</enabled>
21
				<updatePolicy>always</updatePolicy>
22
			</snapshots>
23
		</repository>
24
	</repositories>
25
11 26
	<properties>
12 27
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13 28
        <ipu>3.1-SNAPSHOT</ipu>

+ 3 - 0
ipu-db-example/.gitignore

@ -0,0 +1,3 @@
1
/target/
2
/.classpath
3
/.settings/

+ 24 - 0
ipu-db-example/pom.xml

@ -7,24 +7,48 @@
7 7
    <groupId>com.ai.ipu</groupId>
8 8
    <artifactId>ipu-db-example</artifactId>
9 9
    <version>1.0-SNAPSHOT</version>
10
    
11
    <repositories>
12
		<repository>
13
			<id>ipu</id>
14
			<name>ipu repository</name>
15
			<url>http://114.215.100.48:9090/nexus/content/groups/public/</url>
16
			<releases>
17
				<enabled>true</enabled>
18
			</releases>
19
			<snapshots>
20
				<enabled>true</enabled>
21
				<updatePolicy>always</updatePolicy>
22
			</snapshots>
23
		</repository>
24
	</repositories>
10 25
11 26
    <properties>
12 27
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13 28
        <ipu>3.1-SNAPSHOT</ipu>
14 29
        <jdk>1.8</jdk>
30
        <junit>4.12</junit>
15 31
    </properties>
16 32
17 33
    <dependencies>
34
    	<!-- 数据库持久层引入依赖 -->
18 35
        <dependency>
19 36
            <groupId>com.ai.ipu</groupId>
20 37
            <artifactId>ipu-database</artifactId>
21 38
            <version>${ipu}</version>
22 39
        </dependency>
40
        <!-- 数据库SQL管理引入依赖 -->
23 41
        <dependency>
24 42
            <groupId>com.ai.ipu</groupId>
25 43
            <artifactId>ipu-sql-mgmt</artifactId>
26 44
            <version>${ipu}</version>
27 45
        </dependency>
46
        <dependency>
47
			<groupId>junit</groupId>
48
			<artifactId>junit</artifactId>
49
			<version>${junit}</version>
50
			<scope>test</scope>
51
		</dependency>
28 52
    </dependencies>
29 53
30 54
</project>

+ 48 - 0
ipu-db-example/src/test/java/com/ai/ipu/example/db/dao/SqlDaoExample.java

@ -0,0 +1,48 @@
1
package com.ai.ipu.example.db.dao;
2

3
import java.util.List;
4
import java.util.Map;
5

6
import org.junit.Test;
7

8
import com.ai.ipu.database.conn.SqlSessionManager;
9
import com.ai.ipu.database.dao.IpuDaoManager;
10
import com.ai.ipu.database.dao.impl.SqlDao;
11

12
/**
13
 * @author huangbo@asiainfo.com
14
 * @team IPU
15
 * @date 2019年10月12日下午5:40:57
16
 * @desc 基于SQL的数据库操作
17
 */
18
public class SqlDaoExample {
19
    private String connName = "db_druid";
20
    private String tableName = "ipu_db_demo";
21
    
22
    /**
23
     * 测试无参数的DQL语句
24
     */
25
    @Test
26
    public void testExecuteSelect() throws Exception{
27
        try{
28
            SqlDao dao = IpuDaoManager.takeDao(SqlDao.class, connName);
29
            String sql = "select * from " + tableName;
30
            List<Map<String, Object>>  result = dao.executeSelect(sql);
31
            System.out.println("查询结果:" + result);
32
        }catch(Exception e){
33
            System.err.println("查询失败:" + e.getMessage());
34
            e.printStackTrace();
35
        }finally{
36
            SqlSessionManager.closeAll();
37
        }
38
    }
39
    
40
    @Test
41
    public void testExecuteInsert() throws Exception{}
42
    
43
    @Test
44
    public void testExecuteUpdate() throws Exception{}
45
    
46
    @Test
47
    public void testExecuteDelete() throws Exception{}
48
}

+ 69 - 0
ipu-db-example/src/test/java/com/ai/ipu/example/db/dao/TableDaoExample.java

@ -0,0 +1,69 @@
1
package com.ai.ipu.example.db.dao;
2

3
import java.util.Map;
4

5
import org.junit.Test;
6

7
import com.ai.ipu.common.date.TimeUtil;
8
import com.ai.ipu.data.JMap;
9
import com.ai.ipu.data.impl.JsonMap;
10
import com.ai.ipu.database.conn.SqlSessionManager;
11
import com.ai.ipu.database.dao.IpuDaoManager;
12
import com.ai.ipu.database.dao.impl.SqlDao;
13
import com.ai.ipu.database.dao.impl.TableDao;
14

15
/**
16
 * @author huangbo@asiainfo.com
17
 * @team IPU
18
 * @date 2019年10月12日下午6:05:21
19
 * @desc 基于单表的数据库操作
20
 */
21
public class TableDaoExample {
22
    private String connName = "db_druid";
23
    private String tableName = "ipu_db_demo";
24
    private boolean isCommit = true;
25

26
    @Test
27
    public void testSelect(){}
28
    @Test
29
    public void testInsert(){
30
        int result = 0;
31
        try {
32
            int pk = this.takePK();
33
            TableDao dao = IpuDaoManager.takeDao(TableDao.class, connName);
34
            JMap data = new JsonMap();
35
            data.put("pk", pk);
36
            data.put("string_type", "字符串" + pk);
37
            data.put("int_type", pk);
38
            data.put("decimal_type", "10." + pk);
39
            data.put("date_type", TimeUtil.getSysDate());
40
            data.put("datetime_type", TimeUtil.getSysTime());
41
            result = dao.insert(tableName, data);
42

43
            System.out.println("插入成功:" + result + "条数据");
44
            if (isCommit)
45
                SqlSessionManager.commit(connName);
46
        }
47
        catch (Exception e) {
48
            System.err.println("插入失败:" + e.getMessage());
49
        }
50
    }
51
    @Test
52
    public void testUpdate(){}
53
    @Test
54
    public void testDelete(){}
55
    
56
    public int takePK() throws Exception {
57
        SqlDao sqlDao = IpuDaoManager.takeDao(SqlDao.class, connName);
58
        String sql = "SELECT * FROM ipu_db_demo ORDER BY pk DESC";
59
        Map<String, Object> dataResult = sqlDao.executeSelectFirst(sql);
60
        int pk;
61
        if(dataResult==null||dataResult.isEmpty()){
62
            pk = 0;
63
        }else{
64
            pk = Integer.parseInt(dataResult.get("pk").toString());
65
            pk++;
66
        }
67
        return pk;
68
    }
69
}

+ 28 - 0
ipu-db-example/src/test/java/com/ai/ipu/example/db/mgmt/SqlMgmtDaoExample.java

@ -0,0 +1,28 @@
1
package com.ai.ipu.example.db.mgmt;
2

3
import java.util.List;
4
import java.util.Map;
5

6
import org.junit.Test;
7

8
import com.ai.ipu.data.JMap;
9
import com.ai.ipu.data.impl.JsonMap;
10
import com.ai.ipu.sql.mgmt.ISqlMgmtDao;
11
import com.ai.ipu.sql.mgmt.SqlMgmtDaoFactory;
12

13
public class SqlMgmtDaoExample {
14
    private String connName = "db_druid";
15

16
    @Test
17
    public void testExecuteSelect() {
18
        try{
19
            JMap params = new JsonMap();
20
            ISqlMgmtDao dao = SqlMgmtDaoFactory.createFileSqlMgmtDao(connName);
21
            List<Map<String, Object>> dataResult = dao.executeSelect("com.ai.ipu.example.db.ipu_db_demo","select_all_ipu_db_demo", params);
22
            System.out.println(dataResult);
23
        }catch(Exception e){
24
            e.printStackTrace();
25
        }
26
        
27
    }
28
}

+ 24 - 0
ipu-db-example/src/test/resources/ipu-mybatis-config.xml

@ -0,0 +1,24 @@
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE configuration
3
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
5

6
<configuration>
7
    <settings>
8
		<setting name="defaultFetchSize" value="1000" /> <!-- 结果集获取数量提示值,分批传输 -->
9
	</settings>
10
    <plugins>
11
	    <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> <!-- 分页插件,可根据参数定制化 -->
12
	    <plugin interceptor="com.ai.ipu.sql.mgmt.mybatis.SqlMgmtPlugin"></plugin>
13
	</plugins>
14
	<environments default="db_druid">
15
		<environment id="db_druid">
16
			<transactionManager type="jdbc" />
17
			<dataSource type="com.ai.ipu.database.datasource.DruidDataSourceFactory">
18
				<property name="jdbcUrl" value="jdbc:log4jdbc:mysql://47.105.160.21:3307/test" />
19
				<property name="username" value="ipu" />
20
				<property name="password" value="ipu@321" />
21
			</dataSource>
22
		</environment>
23
	</environments>
24
</configuration>

+ 10 - 0
ipu-db-example/src/test/resources/sql/com/ai/ipu/example/db/ipu_db_demo.xml

@ -0,0 +1,10 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<sqls>
3
	<sql name="select_all_ipu_db_demo">
4
		<![cdata[
5
		<select id="select" resultType="java.util.Map" useCache="true">
6
	        select * from ipu_db_demo
7
		</select>
8
		]]>
9
	</sql>
10
<sqls>

+ 28 - 0
ipu-db-example/src/test/resources/sql_mgmt_mybatis.xml

@ -0,0 +1,28 @@
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<!DOCTYPE configuration
3
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
<!-- <!DOCTYPE configuration SYSTEM "mybatis-ipu-3-config.dtd"> -->
6

7
<configuration>
8
    <settings>
9
		<setting name="defaultFetchSize" value="1000" /> <!-- 结果集获取数量提示值,分批传输 -->
10
	</settings>
11
    <plugins>
12
        <!-- 分页插件,可根据参数定制化 -->
13
	    <plugin interceptor="com.github.pagehelper.PageInterceptor">
14
	        <!-- config params as the following -->
15
		</plugin>
16
	</plugins>
17
	<environments default="ipu_sql_mgmt">
18
		<environment id="ipu_sql_mgmt">
19
			<transactionManager type="JDBC" />
20
			<dataSource type="com.ai.ipu.database.datasource.DruidDataSourceFactory">
21
				<property name="driverClass" value="com.mysql.jdbc.Driver" />
22
				<property name="jdbcUrl" value="jdbc:mysql://121.42.183.206:3307/ipu_sql_mgmt" />
23
				<property name="username" value="ipu" />
24
				<property name="password" value="ipumysql" />
25
			</dataSource>
26
		</environment>
27
	</environments>
28
</configuration>