Recently, there was a query using the general mapper of mybatis. Since I contacted the general mapper, I simply summarized the records so as to help later people.
1, First, put the interface code Mapper.class of mybatis general mapper:
package tk.mybatis.mapper.common; import tk.mybatis.mapper.annotation.RegisterMapper; @RegisterMapper public interface Mapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { }
For our project, many custom mapper mappings for database tables are inherited from general mapper. To inherit, you must specify generics.
Take my project as an example: suppose I have an entity class Student.java that corresponds to the Student table attributes and fields in my database one by one, then I want to use various methods of general Mapper
Create a new StudentMapper.java file in the mapper folder. The code is as follows:
package com.project.business.mapper; import com.project.common.model.business.Student; import org.springframework.stereotype.Component; import tk.mybatis.mapper.common.Mapper; /** * @Author cyl * @Date 2021/10/15 09:55 * @Version 1.0 **/ @Component public interface StudentMapper extends Mapper<Student> { }
2, Define the service and serviceIml implementation in your own service directory, and inject the StudentMapper object as an attribute through the annotation @ Autowired in the implementation class; Then you can use all kinds of methods of general mapper in the methods in the class.
@Transactional @Service public class StudentServiceImpl implements StudentService { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private StudentMapper studentMapper; @Override public StudentVO findInfo(String s_id) throws BusinessException {}
2.1 selectByPrimaryKey() query
When using primary key query, you should pay attention to several places:
- There is only one primary key, and multiple primary keys do not work;
- The primary key to be queried must be marked with @ Id * * * on the corresponding entity class attribute of * * *. This method can recognize it as the primary key and query according to the primary key. Otherwise, all fields will only be queried as joint primary keys!
Specific use:
Student student = studentMapper.selectByPrimaryKey(s_id); if(student == null) { throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR, s_id + " The student does not exist"); }
2.2 non primary key fields are queried with selectByExample()
When the field I want to query is not a primary key, I can call the selectByExample() method:
Example o = new Example(Student.class); Example.Criteria criteria = o.createCriteria(); o.setOrderByClause("s_score desc"); // Query criteria are sorted in descending order by score criteria.andLike("s_name","%"+s_name+"%"); // Fuzzy query by name criteria.andNotEqualTo("deleted",1); // Query students not deleted (deleted 1 means deleted) List<Student> students= studentMapper.selectByExample(o); // General query logger.info("query result students size: " + students.size()); if(students.size() > 0) { logger.info(" result s_id: " + students.get(0).getS_id()); }
Explanation of Example:
In the reverse engineering of Mybatis, an instance and its corresponding example will be generated. Example is used to add conditions, which is equivalent to the filter conditions after the WHERE keyword of the SQL statement.
The above only records primary key query and common query instances. For more details, see the methods of adding, deleting, modifying and querying
Another article specifically records the common functions of general mapper and the common filter conditions of Example:
Portal: Summary of common functions of general mapper
Reference blog:
- Mybatis general Mapper usage details: https://www.jianshu.com/p/5854bfd0f0e6