一、常用注解
增删改查
@Insert
@InsertProvideInsert
@Delete
@DeleteProvideDelete
@Update
@UpdataProvideUpdate
@Select
@SelectProviderSelect
@xxx 实现增删改查
@xxxProvidexxx 实现动态的增删改查
映射
@Results 多个结果映射,包含多个@Result
@Result 单个结果映射
@One 一对一的复杂映射,必须指定select属性
@Maney 集合属性的复杂映射,必须指定select属性
@Param 对mapper接口中方法参数的注解
配置选项
@Option 配置附加功能,如对某语句设置二级缓存
二、具体使用
注解实现增删改查
使用注解时在xml文件中不需要写对应的语句
//mapper接口中对方法使用注解
public interface UserMapper {
@Select("select * from tb_user where id = #{id}")
public List<User> selectbyid(@Param("id") Integer id);
}
<!--mapper.xml文件中-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wj.dao.mapper.UserMapper">
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
<!--不写对应的select语句-->
</mapper>
public interface UserMapper {
//查
@Select("select * from tb_user where id = #{id}")
public User selectbyid(@Param("id") Integer id);
//增
@Insert("insert into tb_user (id,userCode,userName,userPassword,gender,birthday," +
"email,phone,address,userDesc,userRole,imgPath,createdBy,creationDate," +
"modifyBy) values (#{id},#{userCode},#{userName},#{userPassword},#{gender}," +
"#{birthday},#{email},#{phone},#{address},#{userDesc},#{userRole},#{imgPath}," +
"#{createdBy},#{creationDate},#{modifyBy})")
public int adduser(User user);
//改
@Update("update tb_user set userPassword = #{userPassword} where id = #{id}")
public int modifybyid(@Param("id") Integer id,@Param("userPassword") String pw);
//删
@Delete("delete from tb_user where userName=#{userName}")
public int deleteuserbyname(@Param("userName") String name);
}
注解实现动态增删改查
@xxxProvidxxx注解有两个属性 type和method
type指定提供动态语句方法的类,method指定类中的方法
动态查询语句的使用
首先创建工具类
package com.wj.util;
import org.apache.ibatis.jdbc.SQL;
import java.util.Map;
import java.util.Objects;
/**
* @Description:
* @Author WJ
* @Date 2023/2/16:17:26
* @Version V1.0
**/
public class UserDynaSqlProvide {
public String getUserList(final Map<String, Objects> para){
return new SQL(){
{
SELECT("*");
FROM("tb_user");
if (null != para.get("userName") && !"".equals(para.get("userName"))) {
WHERE("userName like concat('%',#{userName},'%')");
}
if (null != para.get("userPassword") && !"".equals(para.get("userPassword"))){
WHERE("userPassword = #{userPassword}");
}
}
}.toString();
}
}
创建接口方法,添加注解
@SelectProvider(type = UserDynaSqlProvide.class,method = "getUserList")
public List<User> selectbynameandpws(@Param("userName") String name,@Param("userPassword") String pws);
在provide类中使用字符串拼接sql语句非常困难,所以mybatis提供了工具类来构造sql语句,常用的方法如下图
注解实现复杂映射
pojo中
package com.wj.dao.pojo;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* @Description:
* @Author WJ
* @Date 2023/2/14:09:16
* @Version V1.0
**/
@Setter
@Getter
@ToString
public class IdCard {
private Integer id; //id
private Integer uid; //用户id
private String code; //身份证号码
private User user; //一对一
}
public interface IdCardMapper {
@Select("select * from tb_idcard where id in(#{id1},#{id2})")
@Results({
//指定主键
@Result(id = true,column = "id",property = "id"),
//用id的值传入selectbyid的方法,值返回给idcard的user对象
@Result(column = "id" ,property = "user",
one=@One(select = "com.wj.dao.mapper.UserMapper.selectbyid")
)
})
public List<IdCard> findCodeById(@Param("id1") Integer id1,@Param("id2") Integer id2);
}
测试
@Test
public void select_all() throws IOException {
try (SqlSession sqlSession = MybatisUtil.createSqlsession()) {
IdCardMapper mapper = sqlSession.getMapper(IdCardMapper.class);
;
for (IdCard idCard: mapper.findCodeById(3,4)) {
System.out.println(idCard);
}
}
}
结果
若是pojo中嵌套的对象是单个,就使用@one,若嵌套的是列表,就用@Maney,需要注意的是一对一,则@One指定select语句返回的是对象,多对多则@Maney指定的select语句返回的是List