Home > Database > Mysql Tutorial > MyBatis 不封装对象获取数据库值的解决方案

MyBatis 不封装对象获取数据库值的解决方案

WBOY
Release: 2016-06-07 15:06:02
Original
1370 people have browsed it

MyBatis 不封装对象获取数据库值的解决方案 思路:如果我们不想封装对象,使用 Map 来获得 SQL 查询的结果集是一个理想的方案。下面就是一个实现类示例。 List Map String , String listMap = ( List Map String , String ) this.getSqlMapClientTemplate().

MyBatis 不封装对象获取数据库值的解决方案
思路:如果我们不想封装对象,使用 Map 来获得 SQL 查询的结果集是一个理想的方案。下面就是一个实现类示例。

<code><span>List</span>MapString, <span>String</span>>> listMap = (<span>List</span>MapString, <span>String</span>>>) this.getSqlMapClientTemplate().queryForList(<span>"writingEssay.queryAssignmentErrorAndReminderByAssId"</span>, assignment_id);
<span>String</span> context = getEssayContextByJson(listMap.get(<span>0</span>).get(<span>"assignment_history_content"</span>), listMap.get(<span>0</span>).get(<span>"essay_set_format"</span>));</code>
Copy after login

我们再来看看底层的 SQL 语句。

<code>    <!-- 通过作业 id 查询,查询作业内容和作业格式和要求的单词 -->
    <select id="<span">"queryAssignmentErrorAndReminderByAssId" resultClass=<span>"java.util.HashMap"</span> parameterClass=<span>"java.lang.Integer"</span>>
        SELECT
            a<span>.assignment</span>_history_content AS assignment_history_content,
            c<span>.essay</span>_set_format AS essay_set_format,
            d<span>.essay</span>_reminder AS essay_reminder
        FROM
            t_assignment_content a,
            t_assignment b,
            t_essay_set c,
            t_essay d
        WHERE
            b<span>.assignment</span>_history_id = a<span>.assignment</span>_history_id
        <span>AND</span> b<span>.assignment</span>_id = $assignment_id$
        <span>AND</span> b<span>.essay</span>_id = d<span>.essay</span>_id
        <span>AND</span> d<span>.essay</span>_set_id = c<span>.essay</span>_set_id<span>;</span>
    </select> </code>
Copy after login

查询数据库得到的结果集。
MyBatis 不封装对象获取数据库值的解决方案
分析:因为我们明确地知道结果集返回一条数据。

下面的知识点和本小节无关。

<code>        <span>if</span> (StringUtils.isNotEmpty(reminder)) {
            <span>for</span> (String <span>str</span> : reminder.split(<span>"#"</span>)) {
                <span>int</span> flag = context.indexOf(<span>str</span>);
                <span>if</span> (flag == -<span>1</span>) {
                    mapReminder.put(<span>str</span>, <span>false</span>);
                } <span>else</span> {
                    mapReminder.put(<span>str</span>, <span>true</span>);
                }
            }
        }</code>
Copy after login

MyBatis 不封装对象获取数据库值的解决方案

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template