Pārlūkot izejas kodu

ipu-database的示例

liutong3 5 gadi atpakaļ
vecāks
revīzija
d18a9a1d2c

+ 17 - 1
ipu-db-example/pom.xml

@ -4,6 +4,12 @@
4 4
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 5
    <modelVersion>4.0.0</modelVersion>
6 6
7
    <parent>
8
        <groupId>com.ai.ipu</groupId>
9
        <artifactId>ipu-aggregator</artifactId>
10
        <version>3.1-SNAPSHOT</version>
11
    </parent>
12
7 13
    <groupId>com.ai.ipu</groupId>
8 14
    <artifactId>ipu-db-example</artifactId>
9 15
    <version>1.0-SNAPSHOT</version>
@ -15,15 +21,25 @@
15 21
    </properties>
16 22
17 23
    <dependencies>
24
        <!-- ipu-database -->
18 25
        <dependency>
19 26
            <groupId>com.ai.ipu</groupId>
20 27
            <artifactId>ipu-database</artifactId>
21 28
            <version>${ipu}</version>
22 29
        </dependency>
30
31
32
        <!-- ipu-sql-mgmt(内部依赖了ipu-database,所有上面的ipu-database依赖实际可去掉) -->
23 33
        <dependency>
24 34
            <groupId>com.ai.ipu</groupId>
25 35
            <artifactId>ipu-sql-mgmt</artifactId>
26
            <version>${ipu}</version>
36
            <version>3.2-SNAPSHOT</version>
37
        </dependency>
38
        <dependency>
39
            <groupId>junit</groupId>
40
            <artifactId>junit</artifactId>
41
            <version>4.12</version>
42
            <scope>compile</scope>
27 43
        </dependency>
28 44
    </dependencies>
29 45

+ 86 - 0
ipu-db-example/src/main/java/com/ai/ipu/example/db/IpuDatabaseExample.java

@ -0,0 +1,86 @@
1
package com.ai.ipu.example.db;
2
3
import com.ai.ipu.example.db.model.IpuDbDemoDto;
4
import com.ai.ipu.example.db.service.IpuDbDemoService;
5
import org.junit.Before;
6
import org.junit.Test;
7
8
import java.math.BigDecimal;
9
import java.util.Date;
10
11
/**
12
 * @author liutong3
13
 * @team IPU
14
 * @date 2019/10/15 16:05
15
 */
16
public class IpuDatabaseExample {
17
    private IpuDbDemoService service;
18
19
    @Before
20
    public void before() throws Exception {
21
        service = new IpuDbDemoService();
22
    }
23
24
    /**
25
     * 普通查询
26
     */
27
    @Test
28
    public void testSelect() throws Exception {
29
        System.out.println(service.select());
30
    }
31
32
    /**
33
     * 条件查询
34
     */
35
    @Test
36
    public void testSelectById() throws Exception {
37
        System.out.println(service.selectById(0));
38
    }
39
40
    /**
41
     * 新增
42
     */
43
    @Test
44
    public void testInsert() throws Exception {
45
        int pk = 999;
46
        System.out.println(service.selectById(pk));
47
        System.out.println("--------------------");
48
        IpuDbDemoDto ipuDbDemoDto = new IpuDbDemoDto();
49
        ipuDbDemoDto.setPk(pk);
50
        ipuDbDemoDto.setString_type("测试新增");
51
        ipuDbDemoDto.setInt_type(10);
52
        ipuDbDemoDto.setDecimal_type(new BigDecimal(10.10));
53
        ipuDbDemoDto.setDate_type(new Date());
54
        ipuDbDemoDto.setDatetime_type(new Date());
55
        ipuDbDemoDto.setNull_type("");
56
        System.out.println(service.insert(ipuDbDemoDto));
57
        System.out.println("--------------------");
58
        System.out.println(service.selectById(pk));
59
    }
60
61
    /**
62
     * 条件删除
63
     */
64
    @Test
65
    public void testDeleteById() throws Exception {
66
        int pk = 1;
67
        System.out.println(service.selectById(pk));
68
        System.out.println("--------------------");
69
        System.out.println(service.deleteById(pk));
70
        System.out.println("--------------------");
71
        System.out.println(service.selectById(pk));
72
    }
73
74
    /**
75
     * 条件更新
76
     */
77
    @Test
78
    public void testUpdateString_typeById() throws Exception {
79
        int pk = 1;
80
        System.out.println(service.selectById(pk));
81
        System.out.println("--------------------");
82
        System.out.println(service.updateString_typeById(pk, "测试修改"));
83
        System.out.println("--------------------");
84
        System.out.println(service.selectById(pk));
85
    }
86
}

+ 87 - 0
ipu-db-example/src/main/java/com/ai/ipu/example/db/dao/IpuDbDemoDao.java

@ -0,0 +1,87 @@
1
package com.ai.ipu.example.db.dao;
2
3
import com.ai.ipu.database.dao.impl.AbstractBizDao;
4
import com.ai.ipu.example.db.model.IpuDbDemoDto;
5
6
import java.io.IOException;
7
import java.math.BigDecimal;
8
import java.util.*;
9
10
/**
11
 * @author liutong3
12
 * @team IPU
13
 * @date 2019/10/15 16:10
14
 * @desc dao层,根据sql调用ipu-database的execute方法连接数据库
15
 */
16
public class IpuDbDemoDao extends AbstractBizDao {
17
    public IpuDbDemoDao(String connName) throws IOException {
18
        super(connName);
19
    }
20
21
    public List<IpuDbDemoDto> select() throws Exception {
22
        String sql = "select pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type from ipu_db_demo";
23
        List<Map<String, Object>> result = dao.executeSelect(sql);
24
        List<IpuDbDemoDto> list = new ArrayList<IpuDbDemoDto>();
25
        for (Map<String, Object> obj : result) {
26
            IpuDbDemoDto ipuDbDemoDto = new IpuDbDemoDto();
27
            ipuDbDemoDto.setPk((int) obj.get("pk"));
28
            ipuDbDemoDto.setString_type((String) obj.get("string_type"));
29
            ipuDbDemoDto.setInt_type((int) obj.get("int_type"));
30
            ipuDbDemoDto.setDecimal_type((BigDecimal) obj.get("decimal_type"));
31
            ipuDbDemoDto.setDate_type((Date) obj.get("date_type"));
32
            ipuDbDemoDto.setDatetime_type((Date) obj.get("datetime_type"));
33
            ipuDbDemoDto.setNull_type((String) obj.get("null_type"));
34
            list.add(ipuDbDemoDto);
35
        }
36
        return list;
37
    }
38
39
    public IpuDbDemoDto selectById(int id) throws Exception {
40
        String sql = "select pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type from ipu_db_demo where pk=#{pk}";
41
        Map<String, Object> param = new HashMap<String, Object>();
42
        param.put("pk" ,id);
43
        List<Map<String, Object>> result = dao.executeSelect(sql, param);
44
        if (result == null || result.size() != 1) {
45
            return null;
46
        } else {
47
            Map<String, Object> obj = result.get(0);
48
            IpuDbDemoDto ipuDbDemoDto = new IpuDbDemoDto();
49
            ipuDbDemoDto.setPk((int) obj.get("pk"));
50
            ipuDbDemoDto.setString_type((String) obj.get("string_type"));
51
            ipuDbDemoDto.setInt_type((int) obj.get("int_type"));
52
            ipuDbDemoDto.setDecimal_type((BigDecimal) obj.get("decimal_type"));
53
            ipuDbDemoDto.setDate_type((Date) obj.get("date_type"));
54
            ipuDbDemoDto.setDatetime_type((Date) obj.get("datetime_type"));
55
            ipuDbDemoDto.setNull_type((String) obj.get("null_type"));
56
            return ipuDbDemoDto;
57
        }
58
    }
59
60
    public int insert(IpuDbDemoDto ipuDbDemoDto) throws Exception {
61
        String sql = "insert into ipu_db_demo(pk, string_type, int_type, decimal_type, date_type, datetime_type, null_type) values (#{pk}, #{string_type}, #{int_type}, #{decimal_type}, #{date_type}, #{datetime_type}, #{null_type})";
62
        Map<String, Object> param = new HashMap<String, Object>();
63
        param.put("pk", ipuDbDemoDto.getPk());
64
        param.put("string_type", ipuDbDemoDto.getPk());
65
        param.put("int_type", ipuDbDemoDto.getInt_type());
66
        param.put("decimal_type", ipuDbDemoDto.getDecimal_type());
67
        param.put("date_type", ipuDbDemoDto.getDate_type());
68
        param.put("datetime_type", ipuDbDemoDto.getDatetime_type());
69
        param.put("null_type", ipuDbDemoDto.getNull_type());
70
        return dao.executeInsert(sql, param);
71
    }
72
73
    public int deleteById(int id) throws Exception {
74
        String sql = "delete from ipu_db_demo where pk = #{pk}";
75
        Map<String, Object> param = new HashMap<String, Object>();
76
        param.put("pk", id);
77
        return dao.executeDelete(sql, param);
78
    }
79
80
    public int updateString_typeById(int id, String value) throws Exception {
81
        String sql = "update ipu_db_demo set string_type = #{string_type} where pk = #{pk}";
82
        Map<String, Object> param = new HashMap<String, Object>();
83
        param.put("pk", id);
84
        param.put("string_type", value);
85
        return dao.executeDelete(sql, param);
86
    }
87
}

+ 96 - 0
ipu-db-example/src/main/java/com/ai/ipu/example/db/model/IpuDbDemoDto.java

@ -0,0 +1,96 @@
1
package com.ai.ipu.example.db.model;
2
3
import java.math.BigDecimal;
4
import java.util.Date;
5
6
/**
7
 * @author liutong3
8
 * @team IPU
9
 * @date 2019/10/16 17:16
10
 * @desc ipu_db_demo表的dto
11
 */
12
public class IpuDbDemoDto {
13
    private int pk;
14
15
    private String string_type;
16
17
    private int int_type;
18
19
    private BigDecimal decimal_type;
20
21
    private Date date_type;
22
23
    private Date datetime_type;
24
25
    private String null_type;
26
27
    public void setPk(int pk) {
28
        this.pk = pk;
29
    }
30
31
    public void setString_type(String string_type) {
32
        this.string_type = string_type;
33
    }
34
35
    public void setInt_type(int int_type) {
36
        this.int_type = int_type;
37
    }
38
39
    public void setDecimal_type(BigDecimal decimal_type) {
40
        this.decimal_type = decimal_type;
41
    }
42
43
    public void setDate_type(Date date_type) {
44
        this.date_type = date_type;
45
    }
46
47
    public void setDatetime_type(Date datetime_type) {
48
        this.datetime_type = datetime_type;
49
    }
50
51
    public void setNull_type(String null_type) {
52
        this.null_type = null_type;
53
    }
54
55
    public int getPk() {
56
        return pk;
57
    }
58
59
    public String getString_type() {
60
        return string_type;
61
    }
62
63
    public int getInt_type() {
64
        return int_type;
65
    }
66
67
    public BigDecimal getDecimal_type() {
68
        return decimal_type;
69
    }
70
71
    public Date getDate_type() {
72
        return date_type;
73
    }
74
75
    public Date getDatetime_type() {
76
        return datetime_type;
77
    }
78
79
    public String getNull_type() {
80
        return null_type;
81
    }
82
83
    @Override
84
    public String toString() {
85
        StringBuilder stringBuilder = new StringBuilder("{")
86
                .append("pk").append(":").append(pk).append(",")
87
                .append("string_type").append(":").append(string_type).append(",")
88
                .append("int_type").append(":").append(int_type).append(",")
89
                .append("decimal_type").append(":").append(decimal_type).append(",")
90
                .append("date_type").append(":").append(date_type).append(",")
91
                .append("datetime_type").append(":").append(datetime_type).append(",")
92
                .append("null_type").append(":").append(null_type)
93
                .append("}");
94
        return stringBuilder.toString();
95
    }
96
}

+ 44 - 0
ipu-db-example/src/main/java/com/ai/ipu/example/db/service/IpuDbDemoService.java

@ -0,0 +1,44 @@
1
package com.ai.ipu.example.db.service;
2
3
import com.ai.ipu.database.dao.IpuDaoManager;
4
import com.ai.ipu.example.db.dao.IpuDbDemoDao;
5
import com.ai.ipu.example.db.model.IpuDbDemoDto;
6
7
import java.util.List;
8
9
/**
10
 * @author liutong3
11
 * @team IPU
12
 * @date 2019/10/15 17:02
13
 * @desc service层,绑定dao
14
 */
15
public class IpuDbDemoService {
16
    private IpuDbDemoDao dao;
17
18
    /**
19
     * 获取test的dao
20
     */
21
    public IpuDbDemoService() throws Exception {
22
        dao = IpuDaoManager.takeDao(IpuDbDemoDao.class, "test");
23
    }
24
25
    public List<IpuDbDemoDto> select() throws Exception {
26
        return dao.select();
27
    }
28
29
    public IpuDbDemoDto selectById(int id) throws Exception {
30
        return dao.selectById(id);
31
    }
32
33
    public int insert(IpuDbDemoDto ipuDbDemoDto) throws Exception {
34
        return dao.insert(ipuDbDemoDto);
35
    }
36
37
    public int deleteById(int id) throws Exception {
38
        return dao.deleteById(id);
39
    }
40
41
    public int updateString_typeById(int id, String value) throws Exception {
42
        return dao.updateString_typeById(id, value);
43
    }
44
}

+ 39 - 0
ipu-db-example/src/main/resources/ipu-mybatis-config.xml

@ -0,0 +1,39 @@
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
        <!-- 分页插件,可根据参数定制化 -->
12
	    <plugin interceptor="com.github.pagehelper.PageInterceptor">
13
	        <!-- config params as the following -->
14
		</plugin>
15
	</plugins>
16
	<environments default="test">
17
        <environment id="test">
18
            <transactionManager type="JDBC" />
19
            <dataSource type="com.ai.ipu.database.datasource.DruidDataSourceFactory">
20
                <property name="driverClass" value="com.mysql.jdbc.Driver" />
21
                <property name="url" value="jdbc:mysql://47.105.160.21:3307/test" />
22
                <property name="username" value="iputest" />
23
                <property name="password" value="iputest@321" />
24
				<!-- 连接池用完时,等待获取新连接的时间 (毫秒) -->
25
				<property name="maxWait" value="5000" />
26
				<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
27
				<!--<property name="acquireRetryAttempts" value="5" />-->
28
				<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
29
				<!--<property name="acquireRetryDelay" value="1000" />-->
30
				<property name="initialSize" value="3" />
31
				<property name="minIdle" value="3" />
32
				<property name="maxActive" value="3" />
33
				<!--<property name="maxIdleTime" value="600" />-->
34
				<!--<property name="idleConnectionTestPeriod" value="60" />-->
35
				<property name="validationQuery" value="SELECT 1" />
36
            </dataSource>
37
        </environment>
38
	</environments>
39
</configuration>