06Mybatis

Mybatis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<foreach collection="ids" item="item" open="(" separator="," close=")">
#{item}
</foreach>

<if test="enterpriseName != null and enterpriseName != ''">
enterprise_name like concat('%',#{enterpriseName},'%') and
</if>
<if test="rectInfo != null">
where longitude <![CDATA[ <= ]]> #{rectInfo.maxLon} and longitude > #{rectInfo.minLon} and latitude <![CDATA[ <= ]]> #{rectInfo.maxLat} and latitude > #{rectInfo.minLat}
</if>

<resultMap id="queryByEnterpriseIdMap" type="xx.xx.xx.domain.entity.enterpriseFill.EnterpriseFillInfo">
<id column="id" javaType="java.lang.Long" property="id"></id>
<result column="enterprise_id" javaType="java.lang.Long" property="enterpriseId"></result>
<result column="open_id" javaType="java.lang.String" property="openId"></result>
<result column="lampblack_handle" javaType="java.lang.String" property="lampblackHandle"></result>
<result column="create_date" javaType="java.util.Date" property="createDate"></result>
<result column="update_date" javaType="java.util.Date" property="updateDate"></result>
<association column="{fillId=id}" property="washInfoCount" select="queryWashInfoCountByFillId"></association>
<association column="{fillId=id}" property="monitorInfoCount" select="queryMonitorInfoCountByFillId"></association>
</resultMap>

<resultMap id="queryWashInfoMap" type="xx.xx.xx.domain.vo.EnterpriseFill.EnterpriseEquipmentWashInfoWrapper">
<id column="id" property="enterpriseEquipmentWashInfo.id"></id>
<result column="fill_id" property="enterpriseEquipmentWashInfo.fillId"></result>
<result column="year_num" property="enterpriseEquipmentWashInfo.yearNum"></result>
<result column="month_num" property="enterpriseEquipmentWashInfo.monthNum"></result>
<result column="create_date" javaType="java.util.Date" property="enterpriseEquipmentWashInfo.createDate"></result>
<result column="update_date" javaType="java.util.Date" property="enterpriseEquipmentWashInfo.updateDate"></result>
<collection property="fileInfos" column="{sourceId=id,sourceType=source_type}" javaType="ArrayList"
ofType="xx.xx.xx.domain.entity.file.FileInfo" select="xx.xx.xx.mapper.UploadFileMapper.queryBySourceId">
</collection>
</resultMap>


<!-- ===================================================================== -->
<!-- MAP嵌套 最多两层 -->
<resultMap id="systemInfoMap" type="xx.xx.filesystem.domain.entity.system.SystemInfo">
<id column="id" javaType="java.lang.Long" property="id"></id>
<result column="name" property="name"></result>
<collection property="serverInfos" column="{systemId=id}" javaType="ArrayList"
ofType="xx.xx.filesystem.domain.entity.system.ServerInfo" select="queryServerInfosBySystemId">
</collection>
</resultMap>

<resultMap id="serverInfoMap" type="xx.xx.filesystem.domain.entity.system.ServerInfo">
<id column="id" javaType="java.lang.Long" property="id"></id>
<result column="system_id" property="systemId"></result>
<result column="server_name" property="serverName"></result>
<result column="server_ip" property="serverIp"></result>
<collection property="userInfos" column="{serverId=id}" javaType="ArrayList"
ofType="xx.xx.filesystem.domain.entity.system.UserInfo" select="queryUserInfosByServerId">
</collection>
</resultMap>
<select id="queryAllSystemInfos" resultMap="systemInfoMap">
select * from file_system_name
</select>

<select id="queryServerInfosBySystemId" resultMap="serverInfoMap">
select id,system_id,server_name,server_ip from file_system_server where system_id = #{systemId}
</select>

<select id="queryUserInfosByServerId" resultType="xx.xx.filesystem.domain.entity.system.UserInfo">
select id,server_id,user_account,user_password,user_name from file_system_user where server_id = #{serverId}
</select>
<!-- ===================================================================== -->

<select>
<!-- case-when使用方法 -->
<!-- 当colume 与condition 条件相等时结果为result -->
case colume
when condition then result
when condition then result
when condition then result
else result
end
<!--当满足某一条件时,执行某一result-->
case
when condition then result
when condition then result
when condition then result
else result
end
<!-- 当满足某一条件时,执行某一result,把该结果赋值到new_column_name 字段中 -->
case
when condition then result
when condition then result
when condition then result
else result
end new_column_name
</seelct>

<!-- ===================================================================== -->
<select>
<!-- -->
<!-- group_concat -->
<!-- group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) -->
<!--默认逗号分隔-->
select GROUP_CONCAT(id) from (
SELECT id FROM wc_enterprise_info LIMIT 10
) as t
<!-- 2,3,4,5,6,7,8,9,10,11-->
select GROUP_CONCAT(id ORDER BY id desc) from (
SELECT id FROM wc_enterprise_info LIMIT 10
) as t
<!--11,10,9,8,7,6,5,4,3,2 -->
select GROUP_CONCAT(id SEPARATOR '-') from (
SELECT id FROM wc_enterprise_info LIMIT 10
) as t
<!--2-3-4-5-6-7-8-9-10-11 -->


<!--concat -->
<!--CONCAT(string1,string2,…) 只要其中一个是NULL,那么将返回NULL -->
enterprise_name like concat('%',#{enterpriseName},'%') and

<!--CONCAT_WS--->
<!--CONCAT_WS(separator,str1,str2,...)-->
<!--
concat_ws 代表 concat with separator,第一个参数是其它参数的分隔符。
分隔符的位置放在要连接的两个字符串之间。
分隔符可以是一个字符串,也可以是其它参数。
如果分隔符为 NULL,则结果为 NULL。
函数会忽略任何分隔符参数后的 NULL 值。
-->
select concat_ws('#','courseName=','NX',null) AS nx_courseName;
<!--
+--------------+
| nx_courseName |
+---------------+
| courseName=#NX|
+---------------+
-->
<select>


<!-- in条件里面有多组参数查询方式 -->
<select>
SELECT
id
FROM
t3
WHERE
(n1, n2) IN (
SELECT
n1,
n2
FROM
t3
WHERE
id <= 2
)
<!-- ===================== -->
SELECT
id
FROM
t3
WHERE
(n1, n2) IN ((1, 'a'),(2, 'b'))
</select>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包
C:\maven\apache-maven-3.6.1\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar
-->
<classPathEntry location="C:\maven\apache-maven-3.6.1\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- JavaBean 实现 序列化 接口 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- 生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://x:3306/fruit_develop?useSSL=false&amp;serverTimezone=GMT&amp;generateSimpleParameterMetadata=true"
userId="root"
password="xxxxxx">

</jdbcConnection>
<!-- 类型转换 -->
<javaTypeResolver >
<!-- 是否使用bigDecimal,
false: 把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer(默认)
true: 把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="priv.king.server.dao" targetProject="src/main/java">
<!-- 默认false 是否允许子包 -->
<property name="enableSubPackages" value="true" />
<!-- 默认false 是否对model添加 构造函数 -->
<property name="constructorBased" value="false"/>
<!-- 默认false 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
<!-- 默认false 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="priv.king.server.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!-- <table tableName="risk_model_order" domainObjectName="DSRiskModelOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="tel_bill_record" domainObjectName="DSTelBillRecord" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->

<!--<table tableName="message" domainObjectName="Message" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>-->
<!--<table tableName="user_info" domainObjectName="UserInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>-->
<table tableName="user_to_user" domainObjectName="UserToUser" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>



</context>
</generatorConfiguration>


06Mybatis
https://jiajun.xyz/2021/08/05/java/quickstart/06Mybatis/
作者
Lambda
发布于
2021年8月5日
许可协议