Home>Article>Database> Understand the basics of Mybatis

Understand the basics of Mybatis

coldplay.xixi
coldplay.xixi forward
2021-01-22 09:29:55 1812browse

Understand the basics of Mybatis

Free learning recommendation:mysql video tutorial

##mybatis

mybatis-config.xml detailed configuration (when configuring, you must delete the redundant attributes and cannot have Chinese characters, otherwise an error will be reported!)

nbsp;configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">                                           

Paging

Reduce the amount of data access

limt implements paging
sql statement: select * from table name limt 0,5;

    0: The starting position of the data
  • 5: Data length

The first type: using Mybatis1 interface

List getUserByLimit(Map map);
2mapeer.xml

2-1 Result set mapping

  
3 Test

@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 (); }

Second: Use RowBounds method1. Interface
List getUserList();
2. Implement the interface

3. Test:

/** * 测试使用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 (); }

Third method: Use Mybatis’ paging plug-in pageHeIper
Understand the basics of Mybatis

sql Many-to-one processing

Database:

Understand the basics of Mybatis
pojoThe teacher-table table in the database corresponds to the entity class Teacher

package com.kuang.w.pojo; import lombok.Data; /** * @author W */ @Data public class Teacher { private int tId; private String tName; }
The user table in the database corresponds to the entity class 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.Interface

List getStudentList();
2.xml configuration implementation interface

           
           
mybatis-config.xm configuration

nbsp;configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">                        
3 Test

@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 one-to-many processing

The entity class corresponding to the data table structure remains unchanged

First way: Multi-table joint query

1 interface

List getTeacher(int tid);
2.1 xml implementation interface

2.2 Mapping configuration

           
3Test

/*测试一对多*/ @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 (); }
Result

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)Second way: Subquery

1Interface

List getTeacher(int tid);
2 Implement interface

         
3Test the same as above

. . . .

Related free learning recommendations:mysql database(Video)

The above is the detailed content of Understand the basics of Mybatis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete