资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Mybatis的基础知识点

小编给大家分享一下Mybatis的基础知识点,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站建设、项城网络推广、重庆小程序开发公司、项城网络营销、项城企业策划、项城品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供项城建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

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
Mybatis的基础知识点

sql 多对一处理

数据库 :Mybatis的基础知识点
pojo
数据库中teacher-table表 对应实体类 Teacher

package com.kuang.w.pojo;

import lombok.Data;

/**
 * @author W
 */
@Data
public 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,getTeacherTest2
Logging 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 Connection
Created 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测试 同上

以上是“Mybatis的基础知识点”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


新闻标题:Mybatis的基础知识点
分享链接:http://cdkjz.cn/article/pgdcci.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220