Browse Source

ipu-sql-mgmt的示例

liutong3 5 years ago
parent
commit
f8c391bdba

+ 51 - 0
ipu-db-example/src/main/java/com/ai/ipu/example/db/IpuSqlMgmtExample.java

@ -0,0 +1,51 @@
1
package com.ai.ipu.example.db;
2
3
import com.ai.ipu.data.JMap;
4
import com.ai.ipu.data.impl.JsonMap;
5
import com.ai.ipu.database.conn.SqlSessionManager;
6
import com.ai.ipu.sql.mgmt.ISqlMgmtDao;
7
import com.ai.ipu.sql.mgmt.SqlMgmtDaoFactory;
8
import org.junit.Test;
9
10
import java.util.List;
11
import java.util.Map;
12
13
/**
14
 * @author liutong3
15
 * @team IPU
16
 * @date 2019/10/12 14:44
17
 */
18
public class IpuSqlMgmtExample {
19
    //通过数据库获取sql并查询
20
    @Test
21
    public void testGetSqlByDb() {
22
        try {
23
            String connName = "test";
24
            ISqlMgmtDao dao = SqlMgmtDaoFactory.createDbSqlMgmtDao(connName);
25
            JMap params = new JsonMap();
26
            params.put("pk", 0);
27
            List<Map<String, Object>> result = dao.executeSelect("com.ai.ipu.ipu-db-demo.ipu-db-demo", "select", params);
28
            System.out.println("查询结果:" + result);
29
        }catch (Exception e) {
30
            e.printStackTrace();
31
        } finally {
32
            SqlSessionManager.closeAll();
33
        }
34
    }
35
36
    @Test
37
    public void testGetSqlByFile() {
38
        try {
39
            String connName = "test";
40
            ISqlMgmtDao dao = SqlMgmtDaoFactory.createFileSqlMgmtDao(connName);
41
            JMap params = new JsonMap();
42
            params.put("pk", 0);
43
            List<Map<String, Object>> result = dao.executeSelect("com.ai.ipu.ipu-db-demo.ipu-db-demo", "select", params);
44
            System.out.println("查询结果:" + result);
45
        }catch (Exception e) {
46
            e.printStackTrace();
47
        } finally {
48
            SqlSessionManager.closeAll();
49
        }
50
    }
51
}

+ 6 - 4
ipu-db-example/src/main/resources/ipu-mybatis-config.xml

@ -8,10 +8,12 @@
8 8
		<setting name="defaultFetchSize" value="1000" /> <!-- 结果集获取数量提示值,分批传输 -->
9 9
	</settings>
10 10
    <plugins>
11
        <!-- 分页插件,可根据参数定制化 -->
12
	    <plugin interceptor="com.github.pagehelper.PageInterceptor">
13
	        <!-- config params as the following -->
14
		</plugin>
11
        <!--&lt;!&ndash; 分页插件,可根据参数定制化 &ndash;&gt;-->
12
	    <!--<plugin interceptor="com.github.pagehelper.PageInterceptor">-->
13
	        <!--&lt;!&ndash; config params as the following &ndash;&gt;-->
14
		<!--</plugin>-->
15
		<plugin interceptor="com.ai.ipu.sql.mgmt.mybatis.SqlMgmtPlugin">
16
        </plugin>
15 17
	</plugins>
16 18
	<environments default="test">
17 19
        <environment id="test">

+ 219 - 0
ipu-db-example/src/main/resources/sql/com/ai/ipu/ipu-db-demo/ipu-db-demo.xml

@ -0,0 +1,219 @@
1
<?xml version="1.0" encoding="UTF-8"?>
2
<sqls>
3
	<sql name="select">
4
		<![CDATA[
5
		<select id="select" parameterType="ai.ipu.data.JMap" resultType="java.util.Map" useCache="true">
6
	        select pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type from ipu_db_demo
7
	        <where>
8
	            <if test="pk != null">
9
	                pk = #{pk}
10
	            </if>
11
	            <if test="string_type != null">
12
	                string_type = #{string_type}
13
	            </if>
14
	            <if test="int_type != null">
15
	                int_type = #{int_type}
16
	            </if>
17
	            <if test="decimal_type != null">
18
	                decimal_type = #{decimal_type}
19
	            </if>
20
	            <if test="date_type != null">
21
	                date_type = #{date_type}
22
	            </if>
23
	            <if test="datetime_type != null">
24
	                datetime_type = #{datetime_type}
25
	            </if>
26
	            <if test="null_type != null">
27
	            	<if test="null_type == true">
28
	                	null_type is null
29
	                </if>
30
	                <if test="null_type == false">
31
	                	null_type is not null
32
	                </if>
33
	            </if>
34
	        </where>
35
		</select>
36
		]]>
37
	</sql>
38
39
	<sql name="select_javabean">
40
		<![CDATA[
41
		<select id="select" parameterType="com.ai.ipu.sql.mgmt.IpuDbDemoBean" resultType="com.ai.ipu.sql.mgmt.IpuDbDemoBean" useCache="true" >
42
	        select pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type from ipu_db_demo
43
	        <where>
44
	            <if test="pk != null">
45
	                pk = #{pk}
46
	            </if>
47
	            <if test="string_type != null">
48
	                string_type = #{string_type}
49
	            </if>
50
	            <if test="int_type != null">
51
	                int_type = #{int_type}
52
	            </if>
53
	            <if test="decimal_type != null">
54
	                decimal_type = #{decimal_type}
55
	            </if>
56
	            <if test="date_type != null">
57
	                date_type = #{date_type}
58
	            </if>
59
	            <if test="datetime_type != null">
60
	                datetime_type = #{datetime_type}
61
	            </if>
62
	            <if test="null_type != null">
63
	            	<if test="null_type == true">
64
	                	null_type is null
65
	                </if>
66
	                <if test="null_type == false">
67
	                	null_type is not null
68
	                </if>
69
	            </if>
70
	        </where>
71
		</select>
72
		]]>
73
	</sql>
74
75
	<sql name="simple_select">
76
		<![CDATA[
77
        select pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type from ipu_db_demo where
78
            1 = 1
79
            <if test="pk != null">
80
	            and pk = #{pk}
81
	        </if>
82
	        <if test="string_type != null">
83
                and string_type = #{string_type}
84
	        </if>
85
	        <if test="int_type != null">
86
                and int_type = #{int_type}
87
	        </if>
88
	        <if test="decimal_type != null">
89
                and decimal_type = #{decimal_type}
90
            </if>
91
            <if test="date_type != null">
92
                and date_type = #{date_type}
93
            </if>
94
            <if test="datetime_type != null">
95
                and datetime_type = #{datetime_type}
96
            </if>
97
            <if test="null_type != null">
98
            	<if test="null_type == true">
99
                	and null_type is null
100
                </if>
101
                <if test="null_type == false">
102
                	and null_type is not null
103
                </if>
104
            </if>
105
		]]>
106
	</sql>
107
108
	<sql name="update">
109
		<![CDATA[
110
		<update id="update" parameterType="ai.ipu.data.JMap">
111
		update ipu_db_demo
112
			<set>
113
				<if test="string_type != null">
114
					string_type = #{string_type},
115
				</if>
116
				<if test="int_type != null">
117
					int_type = #{int_type},
118
				</if>
119
				<if test="decimal_type != null">
120
					decimal_type = #{decimal_type},
121
				</if>
122
				<if test="date_type != null">
123
					date_type = #{date_type},
124
				</if>
125
				<if test="datetime_type != null">
126
					datetime_type = #{datetime_type},
127
				</if>
128
				<if test="null_type != null">
129
					<if test="null_type == true">
130
						null_type = null
131
					</if>
132
					<if test="null_type == false">
133
						null_type = ''
134
					</if>
135
				</if>
136
			</set>
137
			where
138
				pk = #{pk}
139
		</update>
140
		]]>
141
	</sql>
142
143
	<sql name="simple_update">
144
		<![CDATA[
145
		update ipu_db_demo
146
		<set>
147
			<if test="string_type != null">
148
					string_type = #{string_type},
149
				</if>
150
			<if test="int_type != null">
151
				int_type = #{int_type},
152
			</if>
153
			<if test="decimal_type != null">
154
				decimal_type = #{decimal_type},
155
			</if>
156
			<if test="date_type != null">
157
				date_type = #{date_type},
158
			</if>
159
			<if test="datetime_type != null">
160
				datetime_type = #{datetime_type},
161
			</if>
162
			<if test="null_type != null">
163
				<if test="null_type == true">
164
					null_type = null
165
				</if>
166
				<if test="null_type == false">
167
					null_type = ''
168
				</if>
169
			</if>
170
		</set>
171
		where
172
		pk = #{pk}
173
		]]>
174
	</sql>
175
176
	<sql name="insert">
177
		<![CDATA[
178
		<insert id="insert" parameterType="ai.ipu.data.JMap">
179
			insert into ipu_db_demo (pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type)
180
			values (#{pk}, #{string_type}, #{int_type}, #{decimal_type}, #{date_type}, #{datetime_type},
181
			<if test="null_type == true">
182
				null
183
			</if>
184
			<if test="null_type == false">
185
				''
186
			</if>
187
			)
188
		</insert>
189
		]]>
190
	</sql>
191
192
	<sql name="simple_insert">
193
		<![CDATA[
194
		insert into ipu_db_demo (pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type)
195
		values (#{pk}, #{string_type}, #{int_type}, #{decimal_type}, #{date_type}, #{datetime_type},
196
		<if test="null_type == true">
197
			null
198
		</if>
199
		<if test="null_type == false">
200
			''
201
		</if>
202
		)
203
		]]>
204
	</sql>
205
206
	<sql name="delete">
207
		<![CDATA[
208
		<delete id="delete" parameterType="ai.ipu.data.JMap">
209
			delete from ipu_db_demo where pk = #{pk}
210
		</delete>
211
		]]>
212
	</sql>
213
214
	<sql name="simple_delete">
215
		<![CDATA[
216
			delete from ipu_db_demo where pk = #{pk}
217
		]]>
218
	</sql>
219
</sqls>

+ 40 - 0
ipu-db-example/src/main/resources/sql_mgmt_mybatis.xml

@ -0,0 +1,40 @@
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="url" value="jdbc:mysql://47.105.160.21:3307/ipu_sql_mgmt" />
23
				<property name="username" value="iputest" />
24
				<property name="password" value="iputest@321" />
25
				<!-- 连接池用完时,等待获取新连接的时间 (毫秒) -->
26
				<property name="maxWait" value="5000" />
27
				<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
28
				<!--<property name="acquireRetryAttempts" value="5" />-->
29
				<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
30
				<!--<property name="acquireRetryDelay" value="1000" />-->
31
				<property name="initialSize" value="3" />
32
				<property name="minIdle" value="3" />
33
				<property name="maxActive" value="3" />
34
				<!--<property name="maxIdleTime" value="600" />-->
35
				<!--<property name="idleConnectionTestPeriod" value="60" />-->
36
				<property name="validationQuery" value="SELECT 1" />
37
			</dataSource>
38
		</environment>
39
	</environments>
40
</configuration>