Mybaits--一对多关系配置文件(带源码)

一方的配置文件


<mapper namespace="Command">
    <resultMap type="com.imooc.bean.Command" id="Command">

        <!-- column对应的为sql语句中字段名,如果给字段取别名,column必须配置为别名才能被识别 -->
        <id column="C_ID" jdbcType="INTEGER" property="id" />

        <result column="NAME" jdbcType="VARCHAR" property="name" />
        <result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />

        <!-- 跨文件引用resultMap要把namespace属性加上resultMap="namespace.resultMapID" 例如:resultMap="CommandContent.Content" -->
        <!-- 【一方】配置文件中【引用】 【一方】实体类定义的【多方】的集合 -->
        <collection property="contentList" resultMap="CommandContent.Content" />
    </resultMap>


    <select id="queryCommandList" parameterType="com.imooc.bean.Command"
        resultMap="Command">
        select co.ID
        C_ID,co.NAME,co.DESCRIPTION,cc.ID,cc.CONTENT,cc.COMMAND_ID
        from COMMAND
        co left join
        COMMAND_CONTENT cc on co.ID = cc.COMMAND_ID
        where 1=1
        <!-- &&=&amp;&amp;=and " "=&quot;&quot; #{} = ? -->
        <if test="name !=null &amp;&amp;!&quot;&quot;.equals(name.trim())">
            and co.NAME=#{name}
        </if>
        <if test="description != null and !&quot;&quot;.equals(description.trim())">
            and co.DESCRIPTION like '%' #{description} '%'
        </if>
    </select>


</mapper>

多方的配置文件


<mapper namespace="CommandContent">
  <resultMap type="com.imooc.bean.CommandContent" id="Content">
    <id column="ID" jdbcType="INTEGER" property="id"/>
    <result column="CONTENT" jdbcType="VARCHAR" property="content"/>
    <result column="COMMAND_ID" jdbcType="VARCHAR" property="commandId"/>
  </resultMap>
</mapper>
</mapper>

Configuration.xml 引用配置资源 以及与数据库的连接配置的配置文件

//与数据库的连接配置
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC">
                <property name="" value="" />
            </transactionManager>
            <dataSource type="UNPOOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
                <property name="username" value="scott" />
                <property name="password" value="tiger" />
            </dataSource>
        </environment>
    </environments>
    //引用 配置资源
    <mappers>
        <mapper resource="com/imooc/config/sqlxml/Command.xml" />
        <mapper resource="com/imooc/config/sqlxml/CommandContent.xml" />
    </mappers>
</configuration>

DAO层的源码及测试代码


   后台测试映射配置是否正确

    public static void main(String[] args) {
        CommandDao commandDao = new CommandDao();
        List<Command> commandList = new ArrayList<Command>();
        commandList = commandDao.queryCommandList("段子", null);
        if (commandList.size()>0) {
            List<CommandContent> contentList = commandList.get(0).getContentList();
            //在[0,contentList.size())中取随机数,满足随机返回消息的功能
            int i = new Random().nextInt(contentList.size());
            System.out.println(contentList.get(i).getContent());

        }

    }

     根据查询条件查询指令表


    public List<Command> queryCommandList(String name, String description) {
        DBAcess dbAcess = new DBAcess();

        List<Command> commandList = new ArrayList<Command>();

        SqlSession sqlSession = null;
        try {
            sqlSession = dbAcess.getSqlSession();
            Command command = new Command();
            command.setName(name);
            command.setDescription(description);

            // 通过sqlSession执行SQL语句
            commandList = sqlSession.selectList("Command.queryCommandList",command);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();

            }
        }
        return commandList;
    }

声明:此方法是根据幕课网教程整理所得,详细内容可以到幕课网的mybaits教程学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值