mybatis注解

1.mybatis向mysql插入数据后返回插入的主键值,返回的主键会赋给参数。

@Insert(value = "insert into projects (id,resumeId,projectName,startDate,endDate,projectUrl,projectRole,projectExp) " +
            "values(#{id},#{resumeId},#{projectName},#{startDate},#{endDate},#{projectUrl},#{projectRole},#{projectExp})")
    @Options(useGeneratedKeys = true)
    int save(Projects projects);

 

 

2.MyBatis中使用@Results注解来映射查询结果集到实体类属性。

(1)@Results的基本用法。当数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来。column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。  

 @Select({"select id, name, class_id from my_student"})
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)
    })
 List<Student> selectAll();

  如上所示的数据库字段名class_id与实体类属性名classId,就通过这种方式建立了映射关系。

(2)@ResultMap的用法。当这段@Results代码需要在多个方法用到时,为了提高代码复用性,我们可以为这个@Results注解设置id,然后使用@ResultMap注解来复用这段代码。   

@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();

   

@Select({"select id, name, class_id from my_student where id = #{id}"})
@ResultMap(value="studentMap")
Student selectById(integer id);

(3)@One的用法。当我们需要通过查询到的一个字段值作为参数,去执行另外一个方法来查询关联的内容,而且两者是一对一关系时,可以使用@One注解来便捷的实现。比如当我们需要查询学生信息以及其所属班级信息时,需要以查询到的class_id为参数,来执行ClassesMapper中的selectById方法,从而获得学生所属的班级信息。可以使用如下代码。   

@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="class_id ", property="myClass", javaType=MyClass.class,
            one=@One(select="com.my.mybatis.mapper.MyClassMapper.selectById"))
})
List<Student> selectAllAndClassMsg();

(4)@Many的用法。与@One类似,只不过如果使用@One查询到的结果是多行,会抛出TooManyResultException异常,这种时候应该使用的是@Many注解,实现一对多的查询。比如在需要查询学生信息和每次考试的成绩信息时。   

@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER),
        @Result(column="id", property="gradeList", javaType=List.class,
            many=@Many(select="com.my.mybatis.mapper.GradeMapper.selectByStudentId"))
})
List<Student> selectAllAndGrade();

3.mybatis @Select注解中如何拼写动态sql

如果使用xml来配置的话可以用

<if test='id != null'>
  where id = #{id}
</if>
如果是用@Select 这种该如何做呢?

方法:用script标签包围,然后像xml语法一样书写

 @Select(value = "<script>select id, resumeId, projectName, startDate, endDate, projectUrl, projectRole, projectExp from projects  <if test='id != null'> where id = #{id}</if> </script>")
List<Projects> findOne(@Param("id") Long id);

<foreach collection='roleIds' item='id' open='(' separator=',' close=')'>

#{id}

</foreach>

@Select({
            "<script>" +
                    "select" +
                    " m.`id`,m.`serviceId`,m.`code`,m.`p_code`,m.`p_id`,m.`name`,m.`url`,m.`is_menu`,m.`level`,m.`sort`,m.`status`,m.`icon`,m.`create_time`,m.`update_time`" +
                    "FROM `sys_menu` m left join `sys_privilege` p  on  m.id=p.menu_id " +
                    "where p.role_id in " +
                    "<foreach collection='roleIds' item='id' open='(' separator=',' close=')'>" +
                    "#{id}" +
                    "</foreach>" +
                    "</script>"
    })
 List<Menu> getPermissionsByRoleIds(@Param("roleIds") Collection<Long> roleIds);

4.update

@Update("<script>update my_student" +
            "<trim prefix='set' suffixOverrides=','>" +
            "<if test='status != null'>status=#{status},</if>" +
            "<if test='title != null'>title=#{title},</if> " +
            "<if test='catalog != null'>catalog=#{catalog},</if> " +
            "<if test='catalog2 != null'>catalog2=#{catalog2},</if> " +
            "<if test='content != null'>content=#{content},</if> " +
            "<if test='abbr != null'>abbr=#{abbr},</if> " +
            "<if test='updateUid != null'>updateUid=#{updateUid},</if> " +
            "<if test='updateTime != null'>updateTime=#{updateTime},</if> " +
            "</trim> where id=#{id}</script>")
int update(Knowledge knowledge);

5.引用java代码里面的常量

//java 代码中获取字符串
String s = com.enums.KnowledgeType.class.getName();
@Select("select codeName from sys_data where codeValue=#{codeValue} and typeName='${@com.enums.KnowledgeType@class.getName()}'")
public String getNameByValue(String codeValue);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值