-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
9.20 mybatis的ResultMap和ResultType #22
Comments
Mybatis:resultMap的使用总结 |
关联的嵌套查询(在collection中添加select属性): 商品结果集映射resultMap: <resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
<result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
<collection column="{skuId=ID}" property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" select="getAttribute" >
</collection>
</resultMap> collection的select会执行下面的查询属性语句: <select id="getAttribute" resultMap="AttributeResultMap">
select a.ID,s.ATTRIBUTE_NAME
from t_shop_attribute a
where a.ID = #{skuId,jdbcType =BIGINT};
</select> 属性结果集映射: <resultMap id="AttributeResultMap" type="com.meikai.shop.entity.TShopAttribute">
<id column="ID" jdbcType="BIGINT" property="id" />
<result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName" />
</resultMap> BasePlusResultMap包含了属性查询语句的Collection 所以通过下面的查询商品语句就可获得商品以及其包含的属性集合: <select id="getById" resultMap="BasePlusResultMap">
select s.ID,s.SKU_NAME,s.CATEGORY_ID
from t_shop_sku s
where s.ID = #{id,jdbcType =BIGINT};
</select> |
11.2 写的一个工具 #!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
lines = """
//从java类自动生成mapper.xml的resultMap映射行
public class ABCDS
{
private String node;
private String name;
@Column(name = "first_key")
private String firstKey;
@Column(name = "second_key")
private String secondKey;
private List links;
private int num = 0;
}
<resultMap id="ABCDSMap"
type="com....ABCDS">
<result column="node" jdbcType="VARCHAR" property="node"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="first_key" jdbcType="VARCHAR" property="firstKey"/>
<result column="second_key" jdbcType="VARCHAR" property="secondKey"/>
</resultMap>
"""
def get_column_name_line(items, pos):
#最多只倒着往上查五行
for i in range(5):
if(len(items[pos-i]) < 3):
return -1
if "Column" in items[pos-i]:
return pos-i
return -1
def get_column_name_by_line(items, pos):
# result = re.search("@Column\(name\s+=\s+(\w+)", '@Column(name = "second_key")'.replace('"', ''))
line = get_column_name_line(items, pos)
if line != -1:
pattern = "@Column\(name\s+=\s+(\w+)"
result = re.search(pattern, items[line].replace('"', ''))
if result:
return result.groups()[0]
def make_result_columns(lines, is_log=True):
items = lines.split('\n')
results = []
# i = 0
for i, item in enumerate(items):
if "private" in item:
pattern = "private\s+(\w+)\s+(\w+)"
# result = re.search("private\s+(\w+)\s+(\w+)", 'private List links;')
result = re.search(pattern, item)
if result:
key = result.groups()[1]
column = get_column_name_by_line(items, i)
if column:
results.append('<result column="{0}" jdbcType="VARCHAR" property="{1}"/>'.format(column, key))
else:
results.append('<result column="{0}" jdbcType="VARCHAR" property="{1}"/>'.format(key, key))
if is_log:
print "\n".join(results)
return "\n".join(results)
if __name__ == "__main__":
make_result_columns(lines) 运行情况:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。
如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。
The text was updated successfully, but these errors were encountered: