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 accesslimt implements paging
sql statement: select * from table name limt 0,5;
The first type: using Mybatis1 interface
List2mapeer.xml 2-1 Result set mappinggetUserByLimit(Map map);
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
/** * 测试使用RowBounds实现分页 */@Test public void getUserByLimitRowBoundsTest() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); RowBounds rowBounds = new RowBounds (0, 2); ListuserList = 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
sql Many-to-one processing
Database:
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
List2.xml configuration implementation interfacegetStudentList();
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); ListstudentList = 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 query1 interface
List2.1 xml implementation interface 2.2 Mapping configurationgetTeacher(int tid);
3Test
/*测试一对多*/ @Test public void getTeacherTest2() { SqlSession sqlSession = MyBatisUtils.getSqlSession (); TeacherMapper mapper = sqlSession.getMapper (TeacherMapper.class); ListResultteacher = 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)Second way: Subquery1Interface
List2 Implement interfacegetTeacher(int tid); 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!