欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

荐 MyBatis基于注解的开发

程序员文章站 2022-05-07 19:00:50
注解的应用,加速度了代码开发的速度,并且不用去写xml映射文件.mybatis常用注解@Insert:实现新增@Update:实现更新@Delete:实现删除@Select:实现查询@Result:实现结果集封装@Results:可以与@Result 仪器使用,封装多个结果集@ResultMap:实现引用@Results 定义的封装@One: 实现一对一结果集封装@Many:实现一对多结果集封装@SelectProvider: 实现动态SQL映射@CacheNamespace:实现...

注解的应用,加速度了代码开发的速度,并且不用去写xml映射文件.

mybatis常用注解

@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 仪器使用,封装多个结果集
@ResultMap:实现引用@Results 定义的封装
@One: 实现一对一结果集封装
@Many:实现一对多结果集封装
@SelectProvider: 实现动态SQL映射
@CacheNamespace:实现注解二级缓存的使用(注释启动二级缓存要放到类名之前)
// 开启二级缓存 必须配置缓存 时间
@CacheNamespace(eviction = LruCache.class,flushInterval = 60000,size = 1000)

基于注解的增删改查

@Select注解 @Results注解 @Result注解 @ResultMap注解

@Results注解被@ResultMap注解标示后可以多次重复利用,@Result注解被包含在@Results注解之中

/**
* 查找所有学生
* @return
*/
@Select({"select * from student_tb"})
@Results(id = "studentMap",value = {@Result(id = true,property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "sex",column = "sex"),
@Result(property = "age",column = "age"),
@Result(property = "height",column = "height"),
@Result(property = "birthday",column = "birthday")
})
List<Student> findAllStudent();
/**
* 根据 查询学生
* @param id
* @return
*/
@Select("select * from student_tb where id = #{id}")
/*重用*/
@ResultMap(value = "studentMap")
Student findStudentById(int id);
/**
* 保存学生
* @param student
*/
@Insert(" insert into student_tb (name,age,sex,height,birthday)values (#{name},#
{age},#{sex},#{height},#{birthday})")
@SelectKey(keyColumn = "id",keyProperty = "id",resultType = Integer.class,before =
false,statement = " select last_insert_id()")
int saveStudent(Student student);
/**
* 删除学生
* @param id
* @return
*/
@Delete("delete from student_tb where id = #{id}")
int deleteStudentById(int id);
/**
* 更新学生
* @param student
* @return
*/
@Update("update student_tb set name=#{name},age=#{age},height=#{height},sex=#
{sex},birthday=#{birthday} where id=#{id}")
int updateStudent(Student student);
/**
* 根据名称模糊查询
* @param likeName
* @return
*/
@Select(" select * from student_tb where name like #{likeName}")
@ResultType(Student.class)
List<Student> findStudentListByName(String likeName);

复杂的映射开发

@Results 注解
代替的是标签
该注解中可以以使用单个@Result 注解 ,也可以使用@Result集合
@Results({@Result(),@Result()}) 或者 @Results(@Result())
@Result 注解
代替了 标签和标签
@Result 中属性介绍
id 是否是主键字段
column 数据库的列名
property 需要装配的属性名
one 需要使用的@One 注解 (@Result(one =@One()))
many 需要使用的@Many 注解(@Result (many = @Many()))
@One 注解(一对一)
代替了 标签,是夺标查询的关键,在注解中来制定子查询返回单一对象.
@One 注解属性介绍:
select 制定用来多表查询的 sqlmapper
fetchType : 会覆盖全局的配置参数 lazyLoadingEnabled,
使用格式:
@Result(colum = " " , property = " " , one =@One(select = “”,fetcheType=“FetcheType.lazy”) )
@Many 注解(一对多)
代替了标签,是多表查询的关键,在注解中用来指定子查询返回对象集合.
注意: 聚集元素用来处理"一对多" 的关系,需要指定映射的 Java 实体类的属性,属性的javaType(一般格式为 ArrayList) 但是注解中可以不定义;
使用格式:
@Result(property=" ",column = " ", many = @Many ( select = " "))

一对一复杂关系的映射(// 开启二级缓存 必须配置缓存 时间

@CacheNamespace(eviction = LruCache.class,flushInterval = 60000,size = 1000))

代码展示


// 开启二级缓存  必须配置缓存 时间
@CacheNamespace(eviction = LruCache.class,flushInterval = 60000,size = 1000)

    //------------------------------------------------

**IStudentDaoIStudentDao**
 @ResultType(IStudentDao.class)
    Student findAllStudentById(int id);


    //------------------------------------------------
**ScoreDao**
  @Select(" select * from Score ")
    @Results(id = "ScoreMap1",value = {
            @Result(id = true,property = "scoreid",column = "scoreid"),
            @Result(property = "coursename",column = "coursename"),
            @Result(property = "score",column = "score"),
            @Result(property ="student",column = "studentid",one = @One(select = "" +
                    "com.aaa.dao.IStudentDao.findAllStudentById",fetchType = FetchType.LAZY))
    })
    List<Score> findAllScoreWithStudentOne();

一对多复杂关系的映射

// 开启二级缓存  必须配置缓存 时间
@CacheNamespace(eviction = LruCache.class,flushInterval = 60000,size = 1000)

    //------------------------------------------------
**ScoreDao**
@ResultType(Score.class)
    Score findScoreById(int studentid);
    //------------------------------------------------

**IStudentDao**
@Select(" select * from student")
    @Results(id = "studentMap2",value = {@Result(id = true,property = "id",column = "id"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "height",column = "height"),
            @Result(property = "name",column = "name"),
            @Result(property = "age",column = "age"),
            @Result(property = "scoreList",column = "id",many = @Many(select =
            "com.aaa.dao.ScoreDao.findScoreById",fetchType =
            FetchType.LAZY))
    })
    List<Student> findStudentWithScoreLazy();

本文地址:https://blog.csdn.net/nnnnnnnnnnii/article/details/107324522