Basic knowledge points of Mybatis
Editor to share with you the basic knowledge of Mybatis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Mybatis
Mybatis-config.xml detailed configuration (extra attributes should be deleted during configuration. No Chinese is allowed or an error will be reported!)
Pagination
Reduce data access
Paging with limt
Sql statement: select * from table name limt 0jie 5
0: where the data starts
5: the length of the data
The first one: use Mybatis
1 interface
List getUserByLimit (Map map)
2mapeer.xml
Select * from mybatis.user limit ${starIndex}, ${pageSize}
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 the RowBounds method
1. Interface
List getUserList ()
two。 Implementation interface
Select * from mybatis.user
3. Test:
/ * Test uses RowBounds for paging * / @ 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) } / / close sqlSession.close ();}
Third: pageHeIper, a paging plug-in using Mybatis
Sql many-to-one processing
Database:
Pojo
Teacher-table table in database corresponds to entity class Teacher
Package com.kuang.w.pojo;import lombok.Data;/** * @ author W * / @ Datapublic class Teacher {private int tId; private String tName;}
User table in database corresponds to 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
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 configuration
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 classes corresponding to the structure of the data table remain unchanged
The first way: multi-table joint search
1 interface
List getTeacher (int tid)
2.1 xml implementation interface
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 Mapping configuration
3 Test
/ * Test one-to-many * / @ 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);} / commit transaction shelf without sqlSession.commit () / / close sqlSession.close ();}
Result
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)