【MyBatis】| Annotation development of MyBatis

Table of contents

One: Annotation development of MyBatis

1. @Insert annotation

2. @Delete annotation

3. @Update annotation

4. @Select annotation

5. @Results annotation


One: Annotation development of MyBatis

Annotation-based development is also provided in MyBatis, using annotations can reduce the configuration of Sql mapping files . Of course, if you use annotation development, the sql statement is written in the java program , and this method will also bring costs to the maintenance of the sql statement.

This is what the official says:

Writing complex SQL using annotations looks like this:

Principle: simple sql can be annotated, and complex sql can use xml!

After using annotation development, the SqlMapper.xml file, one of the three brothers, is no longer needed!

1. @Insert annotation

One of the two brothers CarMapper interface, used to write methods

Using the @Insert annotation method, you can write SQL statements on the annotations. For the variables in the SQL statements, they are the variable names corresponding to the pojo class Car.

package com.bjpowernode.mybatis.mapper;

import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;

public interface CarMapper {
    // 使用注解式开发,插入数据
    @Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
    int insert(Car car);
}

CarMapperTest, the second of the two brothers, is used for testing

package com.bjpowernode.mybatis.test;

import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class CarMapperTest {
    @Test
    public void testInsert(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        // 创建Car对象
        Car car = new Car(null, "666", "丰田霸道", 32.0, "2023-1-9", "燃油车");
        int count = mapper.insert(car);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
}

Results of the:

2. @Delete annotation

One of the two brothers CarMapper interface, used to write methods

package com.bjpowernode.mybatis.mapper;

import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;

public interface CarMapper {
    // 使用注解式开发,删除数据
    @Delete("delete from t_car where id = #{id}")
    int deleteById(Long id);
}

CarMapperTest, the second of the two brothers, is used for testing

package com.bjpowernode.mybatis.test;

import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class CarMapperTest {
   @Test
    public void testDeleteById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int count = mapper.deleteById(40L);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
}

Results of the:

3. @Update annotation

 One of the two brothers CarMapper interface, used to write methods

package com.bjpowernode.mybatis.mapper;

import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;

public interface CarMapper {
    // 使用注解式开发,更新数据
    @Update("update t_car set car_num=#{carNum},brand=#{brand},guide_price=#{guidePrice},produce_time=#{produceTime},car_type=#{carType} where id = #{id}")
    int update(Car car);
}

CarMapperTest, the second of the two brothers, is used for testing

package com.bjpowernode.mybatis.test;

import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class CarMapperTest {
   @Test
    public void testUpdate(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        // 创建Car对象,根据id进行更新
        Car car = new Car(34L, "666", "丰田霸道", 32.0, "2023-1-9", "燃油车");
        int count = mapper.update(car);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
    }
}

Results of the:

4. @Select annotation

 One of the two brothers CarMapper interface, used to write methods

package com.bjpowernode.mybatis.mapper;

import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.Insert;

public interface CarMapper {
    // 使用注解式开发,查询数据
    @Select("select * from t_car where id = #{id}")
    Car selectById(Long id);
}

CarMapperTest, the second of the two brothers, is used for testing

package com.bjpowernode.mybatis.test;

import com.bjpowernode.mybatis.mapper.CarMapper;
import com.bjpowernode.mybatis.pojo.Car;
import com.bjpowernode.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class CarMapperTest {
   @Test
    public void testSelectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        Car car = mapper.selectById(41L);
        System.out.println(car);
        sqlSession.close();
    }
}

Results of the:

5. @Results annotation

We know that the fields in the database table and the attribute names of the pojo class are different. The reason why we can find out the data completely is because it is configured in the core configuration file mybatis-config.xml: enable automatic mapping of hump naming

    <!--启⽤驼峰命名⾃动映射-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

If we do not enable it, the corresponding field is null, and the query data is as follows:

What else can I do?

You can also use the @Results annotation to specify the mapping relationship (use the @Results annotation to annotate the previous resultMap, and use @ResultMap to annotate the resultMap referenced by the result set)!

Note: The @Results annotation must be in the same method as the @Select query, and the @Results annotation cannot be defined separately!

Note: It can also be seen from here that development using annotations is okay for simple SQL, but it is too much trouble for slightly more complex query statements!

package com.bjpowernode.mybatis.mapper;

import com.bjpowernode.mybatis.pojo.Car;
import org.apache.ibatis.annotations.*;

public interface CarMapper {
    // 使用注解式开发,查询数据
    @Select("select * from t_car where id = #{id}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "carNum",column = "car_num"),
            @Result(property = "brand",column = "brand"),
            @Result(property = "guidePrice",column = "guide_price"),
            @Result(property = "produceTime",column = "produce_time"),
            @Result(property = "carType",column = "car_type"),
    })
    Car selectById(Long id);
}

In this calculation, we can query the data normally without enabling the automatic mapping of the camel case

Conclusion: Until today, the study of MyBatis has been perfect, and then the study of Spring will start, so stay tuned!

Guess you like

Origin blog.csdn.net/m0_61933976/article/details/128621699