了解Mybatis的基础

了解Mybatis的基础

免费学习推荐:mysql视频教程

mybatis

mybatis-config.xml详细配置(配置时要把多余的属性删除 不能有中文 否则报错!)

                                                                                                                                                                                                                                                                                                                

分页

减少数据访问量
limt实现分页
sql语句: select * from 表名 limt 0,5;

0:数据开始的位置5:数据的长度

第一种:使用Mybatis
1接口

  List getUserByLimit(Map map);

2mapeer.xml

           select *        from mybatis.user        limit ${starIndex},${pageSize}    

2-1结果集映射

            

3测试

 @Test    public void getUserByLimitTest() {        SqlSession sqlSession = MyBatisUtils.getSqlSession ();        UserMapper mapper = sqlSession.getMapper (UserMapper.class);        HashMap hashMap = new HashMap ();        hashMap.put ("starIndex", 1);        hashMap.put ("pageSize", 2);        List userByLimit = mapper.getUserByLimit (hashMap);        for (Object o : userByLimit) {            System.out.println (o);        }        sqlSession.close ();    }

第二种:使用RowBounds方法
1.接口
List getUserList();
2.实现接口

        select *        from mybatis.user    

3.测试:

 /**     * 测试使用RowBounds实现分页     */@Test    public void getUserByLimitRowBoundsTest() {        SqlSession sqlSession = MyBatisUtils.getSqlSession ();        RowBounds rowBounds = new RowBounds (0, 2);        List userList = sqlSession.selectList ("com.kuang.w.dao.UserMapper.getUserList", null, rowBounds);        for (User user : userList) {            System.out.println (user);        }        //关闭        sqlSession.close ();    }

第三种:使用Mybatis的分页插件 pageHeIper
my在这里插入图片描述

sql 多对一处理

数据库 :在这里插入图片描述
pojo
数据库中teacher-table表 对应实体类 Teacher

package com.kuang.w.pojo;import lombok.Data;/** * @author W */@Datapublic class Teacher {    private int tId;    private String tName;}

数据库中user表 对应 实体类Student

package com.kuang.w.pojo;import lombok.Data;/** * @author W */@Datapublic class Student {    private int id;    private int tid;    private String name;    private String password;    private Teacher teacher;}

1.接口

   List getStudentList();

2.xml配置实现接口

              select *        from mybatis.user;                                                                                select *        from mybatis.teacher_table        where tid = #{id};    
             select u.id       uid,               u.name     uname,               u.password upassword,               u.tid      utid,               t.tname        from mybatis.user u,             mybatis.teacher_table t        where t.tid = u.tid;                                                                             

mybatis-config.xm配置

                                                                                                                                                                                    <!--              -->                    

3 测试

 @Test    public void getStudentListTest() {        SqlSession sqlSession = MyBatisUtils.getSqlSession ();        StudentMapper mapper = sqlSession.getMapper (StudentMapper.class);        List studentList = mapper.getStudentList ();        for (Student student : studentList) {            System.out.println (student);        }        sqlSession.commit ();        sqlSession.close ();    }

sql 一对多处理

数据表结构 对应的实体类 不变

第一种方式: 多表联查
1接口

    List getTeacher(int tid);

2.1 xml实现接口

          select t.tid, t.tname, u.id, u.name, u.password        from mybatis.user u,             mybatis.teacher_table t        where t.tid = u.tid          and t.tid = #{tid};    

2.2映射配置

                                                                                                        

3测试

 /*测试一对多*/    @Test    public void getTeacherTest2() {        SqlSession sqlSession = MyBatisUtils.getSqlSession ();        TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class);        List teacher = mapper.getTeacher (1);        for (Teacher teacher1 : teacher) {            System.out.println (teacher1);        }//提交事务   架子  这里可以不要        sqlSession.commit ();        // 关闭        sqlSession.close ();    }

结果

com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.kuang.w.dao.myTest,getTeacherTest2Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.PooledDataSource forcefully closed/removed all connections.Opening JDBC ConnectionCreated connection 164974746.Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]==>  Preparing: select t.tid, t.tname, u.id, u.name, u.password from mybatis.user u, mybatis.teacher_table t where t.tid = u.tid and t.tid = ?; ==> Parameters: 1(Integer)<==    Columns: tid, tname, id, name, password<==        Row: 1, 狂神, 1, 天王盖地虎, 111<==        Row: 1, 狂神, 2, 小波, 123<==        Row: 1, 狂神, 3, 雷神, 922<==        Row: 1, 狂神, 5, 马儿扎哈, 123<==      Total: 4Teacher(tId=1, tName=狂神, students=[Student(id=1, tid=1, name=天王盖地虎, password=111), Student(id=2, tid=1, name=小波, password=123), Student(id=3, tid=1, name=雷神, password=922), Student(id=5, tid=1, name=马儿扎哈, password=123)])Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@9d5509a]Returned connection 164974746 to pool.Process finished with exit code 0

第二种方式: 子查询
1接口

    List getTeacher(int tid);

2 实现接口

             select *        from mybatis.teacher_table        where tid = #{tid};                                                                select *        from mybatis.user        where tid = #{tid};    

3测试 同上
。。。。

相关免费学习推荐:mysql数据库(视频)

以上就是了解Mybatis的基础的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/189356.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月1日 12:26:27
下一篇 2025年11月1日 12:28:41

发表回复

登录后才能评论
关注微信