MyBatis Universal Mapper

Writing of Entity Classes

Keep in mind one principle: Number of fields in entity classes >= Number of fields that need to be operated on in database tables. By default, all fields in the entity class will operate as fields in the table, and if there are additional fields, the @Transient annotation must be added.

@Table(name = "tb_spec_group")
public class SpecGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long cid;

    private String name;

    @Transient
    private List<SpecParam> params;

   // getter and setter ellipsis
   }

Explain:

  1. The default table name is the class name, and the hump is underlined (only with uppercase letters), such as UserInfo, whose default table name is user_info.
  2. Table names can be specified using @Table(name = tableName), and table names can be specified in this way if they do not conform to the first default rule.
  3. Fields, like @Column, are all table fields by default, and table fields are underscored by the Field name hump of a Java object.
  4. You can use @Column(name = fieldName) to specify field names that do not conform to rule 3
  5. The @Transient annotation ignores the field, and the field added with the annotation will not be used as a table field.
  6. It is suggested that there must be a field with @Id annotations as primary keys, and fields with multiple @Id annotations as joint primary keys.
  7. If it's a MySQL self-incremental field, add @GeneratedValue(generator = JDBC). If it is another database, you can refer to the official documents.

DAO Writing

In the traditional Mybatis writing method, the DAO interface needs to be associated with Mapper file, that is to say, it needs to write SQL to implement the method in the DAO interface. In general Mapper, DAO only needs to inherit a common interface to have rich methods:

To inherit a generic apper, you must specify generics

public interface SpecGroupMapper extends Mapper<SpecGroup> {
}

Once Mapper is inherited, the inherited Mapper has all the common methods of Mapper:

Select
Method: List select(T record);
Description: Query according to the attribute value in the entity, and use the equal sign for the query condition.

Methods: T selectByPrimary Key (Object key);
Description: According to the primary key field for query, method parameters must contain the complete primary key properties, query conditions using equal sign

Method: List selectAll();
Explanation: By querying all the results, the select(null) method can achieve the same effect.

Methods: T select One (T record);
Description: According to the attributes in the entity, only one return value can be queried, and many results throw an exception. The query condition uses the equal sign.

Method: int select Count (T record);
Description: According to the total number of attribute queries in the entity, the query condition uses the equal sign.

Insert
Method: int insert(T record);
Description: Save an entity, null attributes will also be saved, will not use the database default value

Method: int insert Selective (T record);
Note: Save an entity, null attributes will not be saved, and database defaults will be used

Update
Method: int update ByPrimary Key (T record);
Note: Update all fields of entity according to primary key, null value will be updated

Method: int update ByPrimary Key Selective (T record);
Note: Update the value of an attribute that is not null based on the primary key

Delete
Method: int delete(T record);
Description: Delete according to entity attribute as condition, and use equal sign for query condition.

Method: int deleteByPrimary Key (Object key);
Description: According to the primary key field to delete, method parameters must contain the complete primary key properties.

Example method
Method: List selectByExample(Object example);
Description: Query according to Example condition
Important: This query supports specifying query columns by Example class and query columns by selectProperties method.

Method: int select CountByExample (Object example);
Description: Total number of queries based on Example conditions

Methods: int updateByExample(@Param("record") T record, @Param("example") Object example);
Note: Update all attributes contained in entity record according to Example condition, and null value will be updated

Methods: int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
Note: Update entity record contains attribute values other than null according to Example conditions

Method: int deleteByExample(Object example);
Note: Delete data according to Example condition

Use in code
mapper is injected into service, and it can be used.

    @Autowired
    private SpecGroupMapper specGroupMapper;

Specific examples

query
Users querying age=28

@Test
    public void  test1(){
        User user = new User();
        user.setAge(28);
        List<User> list = userMapper.select(user);
        list.stream().forEach(System.out::println);

    }

Query sex=0, and there is a "b" user in the name

@Test
    public void  test4(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sex",0);
        criteria.andLike("name","%b%");
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

There is no "baby" user in the query name

@Test
    public void  test13(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotLike("name","%baby%");
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

Query age users between 10 and 50 years of age

@Test
    public void  test6(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andBetween("age",10,50);
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

Query Users Aged > 10

@Test
    public void  test8(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();     
        //criteria.andBetween("age",10,50);
        criteria.andGreaterThan("age",10);
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

Query Users Aged > 10

@Test
    public void  test9(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThan("age",50);
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

Query age > = 10-year-old users

 @Test
    public void  test10(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThanOrEqualTo("age",10);
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

Users whose query age is empty

@Test
    public void  test11(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("age");
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

Query age for non-empty users

@Test
    public void  test12(){

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNotNull("age");
        List<User> list = userMapper.selectByExample(example);
        list.stream().forEach(System.out::println);

    }

delete
delete user

@Test
    public void  test14(User user){
        userMapper.delete(user);

    }

Delete users according to primary key

 @Test
    public void  test15(Long  id){
        userMapper.deleteByPrimaryKey(id);

    }

Delete users whose age is empty

@Test
    public void  test16(){
    
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("age");
        userMapper.deleteByExample(example);

    }

Add to
Adding users

@Test
    public void  test17(User user){
    
        userMapper.insert(user);

    }

To update
Users with an update id of 35

@Test
    public void  test19(){
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",35);
        userMapper.updateByExample(new User(),example);

    }

@Test
    public void  test20(){
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",35);
        userMapper.updateByExampleSelective(new User(),example);

    }
 @Test
    public void  test22(){
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        User user = new User();
        user.setId(35L);
        userMapper.updateByPrimaryKey(user);

    }

Tags: Attribute Database Java MySQL

Posted on Thu, 29 Aug 2019 06:06:35 -0700 by craigerjs