zoukankan      html  css  js  c++  java
  • MyBatis的mapper

    在前面的学习中,我们还在写一些接口啊,实现类啊,是不是感觉好low的。。。

    其实,我们是可以不用写接口的实现类的,今天就带着大家一起学习一下,当然,我是回顾的。

    看下面的结构,是不是没实现类呢!

    源码介绍:

    1.首先,好多东西都和我上一个例子相同,不做介绍

     http://www.cnblogs.com/zhangzongle/p/6188987.html

    2.StudentDAO.xml (namespace必须指定到接口名)

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="cn.zhang.dao.StudentDao"><!--注意的地方-->
    
        <!-- 查询所有 -->
        <select id="findAll" resultType="Student">
            select * from student
        </select>
    
    </mapper>

    3.MyTest.java (使用了getMapper()方法获得dao实例)

    package cn.zhang.test;
    
    import java.io.IOException;
    import java.util.List;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Before;
    import org.junit.Test;
    import cn.zhang.dao.StudentDao;
    import cn.zhang.entity.Student;
    import cn.zhang.util.MybatisUtil;
    
    public class MyTest {
        
        StudentDao dao;
        @Before
        public void initData() throws IOException{
            SqlSession session = MybatisUtil.getSession();
            dao = session.getMapper(StudentDao.class);//使用了getMapper()方法获得dao实例
        }
        
        
        /**
         * 查询所有学生
         * @throws IOException
         */
        @Test
        public void findAll() throws IOException{
            List<Student> list = dao.findAll();
            for (Student student : list) {
                System.out.println("编号: "+student.getStuno()+"姓名:"+student.getStuname());
            }
            
        }
    }

     是吧!现在看起来是不是有点高大上了。。。

  • 相关阅读:
    不同类型的磁盘存储在Ubuntu下的性能测试
    Ubuntu16.04编译安装Redis Desktop Manager
    gnu screen的用法
    MySQL Workbench常用快捷键
    Libevent例子(二)
    Libevent例子(一)
    Ubuntu下的init.d管理update-rc.d
    Centos7 修改终端文字显示颜色
    通过socks tunnel设置http代理
    linux:C++的socket编程
  • 原文地址:https://www.cnblogs.com/zhangzongle/p/6202248.html
Copyright © 2011-2022 走看看