MyBatis注解(@)实现增删改查

Posted 曹寰&

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis注解(@)实现增删改查相关的知识,希望对你有一定的参考价值。

MyBatis注解(@)实现增删改查

# crud
# 名称不一致
	@Select("select * from book where id = #id")
	@Results(
		@Result(property="id",column="id",id=true),
		@Result(property="name",column="b_name"),
		@Result(property="createTime",column="b_create_time"),
		@Result(property="price",column="price")
	)
	public Book findById(int id);

# 一对一
@Select("select * from chapter")
	@Results( 
		@Result(property = "id", column = "id",id=true),
		@Result(property = "name", column = "name"),
		@Result(property = "bookId", column = "book_id"),
		@Result(property = "book",column="book_id",one = @One(fetchType=FetchType.EAGER,select="com.zhiyou100.mapper.BookMapper.findById"))
	)
	public List<Chapter> findAll();

# 一对多
	@Select("select * from book")
	//解决数据库和java属性不一致
	//property:java属性
	//column:数据库属性
	//id:判断是否是主键
	@Results(
		@Result(property="id",column="id",id=true),
		@Result(property="name",column="b_name"),
		@Result(property="createTime",column="b_create_time"),
		@Result(property="price",column="price"),
		@Result(property="chapters",column="id",many=@Many(fetchType=FetchType.EAGER,select="com.zhiyou100.mapper.ChapterMapper.findByBookId"))
	)
	public List<Book> findAll();


# 多对多
	@Select("select * from user")
	@Results(
		@Result(property="id",column="id",id=true),
		@Result(property="roles",column="id",many=@Many(fetchType=FetchType.EAGER,select="com.zhiyou100.mapper.RoleMapper.findByUserId"))	
	)
	public List<User> findUserAndRole();
		@Select("SELECT * from role r INNER JOIN  user_role ur ON r.id = ur.role_id where ur.user_id = #userId")
	public List<Role> findByUserId(int userId);

package com.prms.project.mapper;

import com.prms.project.domain.ProElement;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author CaoPengCheng
 * @version 1.0
 * @date 2021/11/14 19:47
 */
@Mapper
public interface ProElementMapper 
   
    @Results(
            @Result(property = "elementId", column = "element_id", id = true),
            @Result(property = "elementName", column = "element_name"),
            @Result(property = "orderNum", column = "order_num"),
            @Result(property = "component", column = "component"),
            @Result(property = "status", column = "status"),
            @Result(property = "createBy", column = "create_by"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateBy", column = "update_by"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "remark", column = "remark")
    )
    @Select("select element_id, element_name, order_num, component, status, create_time from prms_element order by order_num")
    List<ProElement> selectElementList();

    @Results(
            @Result(property = "elementId", column = "element_id", id = true),
            @Result(property = "elementName", column = "element_name"),
            @Result(property = "orderNum", column = "order_num"),
            @Result(property = "component", column = "component"),
            @Result(property = "status", column = "status"),
            @Result(property = "createBy", column = "create_by"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateBy", column = "update_by"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "remark", column = "remark")
    )
    @Select("select element_id, element_name, order_num, component, status, create_time " +
            "from prms_element " +
            "order by order_num" +
            "where element_id = #element_id")
    ProElement selectElementById(@Param("element_id") Long element_id);
    
    
    @Insert("insert into prms_element(element_name,order_num,component,status,create_by,create_time) " +
            "VALUES('#element_name',#order_num,'#component',#status,'#create_by','#create_time')")
    int  insertElement(
            @Param("element_name") String element_name,
            @Param("order_num") int order_num,
            @Param("component") String component,
            @Param("status") int status,
            @Param("create_by") String create_by,
            @Param("create_time") String create_time
    );

    @Update("UPDATE prms_element" +
            "set element_name='#element_name'," +
            "order_num=#order_num," +
            "component='#component'," +
            "status=#status," +
            "update_by='#update_by'," +
            "update_time='#update_time'" +
            "where element_id =  #element_id ")
    int updateElement(
            @Param("element_id") Long element_id,
            @Param("element_name") String element_name,
            @Param("order_num") int order_num,
            @Param("component") String component,
            @Param("status") int status,
            @Param("update_by") String update_by,
            @Param("update_time") String update_time
    );

    @Delete("delete from prms_element where element_id =  #element_id")
    int deleteElementById(@Param("element_id") Long element_id);
    


以上是关于MyBatis注解(@)实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis注解(@)实现增删改查

MyBatis注解(@)实现增删改查

mybatis生成的增删改查怎么用

mybatis怎么进行增删改查

SpringBoot整合Mybatis实现增删改查的功能

[mybatis]快速搭建一个mybatis程序,实现对数据的增删改查