2

java 日常-打印sql语句,in查询,取集合内元素

 2 years ago
source link: http://www.hechunbo.com/index.php/archives/408.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java打印sql语句

<!--打印SQL-->
<logger name="java.sql.Connection" level="DEBUG" />
<logger name="java.sql.Statement" level="DEBUG" />
<logger name="java.sql.PreparedStatement" level="DEBUG" />

<root level="DEBUG">
            <appender-ref ref="generalDailyFileAppender"/>
            <appender-ref ref="errorDailyFileAppender"/>
    <appender-ref ref="console" />
</root>

取list<user>中指定元素的集合

List<Integer> lsAttach=fundManagers.stream().map(TblFundManager::getId).collect(Collectors.toList());

#对指定元素进行分隔.
String ownIds=StringUtils.join(lsAttach.toArray(),",");

取集合中的某一元素

用optional 防止空

Optional<TblAttachInfo> attach=managerAttachInfos.stream().filter(s->s.getOwnId()==manager.getId()).findFirst();
                if (attach.isPresent()) {
                    fundManager.setAttachPath(attach.get().getAttachPath());
                }

mybatis中使用in

<select id="selectByIdSet" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from table1
    where own_id in  (${ownIds,jdbcType=VARCHAR})
  </select>

上面的不能防止sql注入,用$替换, 下面的是#加list循环.

List<User> selectByIdSet(List idList);
 
<select id="selectByIdSet" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
      from table1
    where own_id in  
    <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
      #{id}
    </foreach>
</select>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK