####

@Insert

编写insert语句,实现数据的新增

   @Insert("insert into users(name, age) values(#{name}, #{age})")
public int add(Users user);

2、@Update

编写update语句,实现数据的修改

   @Update("update users set name = #{name}, age = #{age} where id = #{id}")
public int update(Users user);

3、@Delete

编写delete语句,实现数据的删除

   @Delete("delete from users where id = #{id}")
public int deleteById(int id);

4、@Select

编写select语句,实现数据的查询

   @Select("select * from users where id = #{id}")
public Users getUserById(int id);

5、@Options

参数设置
常用属性:
keyProperty
useGeneratedKeys 获取插入成功后自增主键的值 执行添加操作之后,直接访问对象的主键字段属性即可取得对应值。
statementType
keyColumn
timeout

6、@ResultType

标记结果集的数据类型,Class<?>,等价于xml中的resulttype

7、@Results

标记结果集的数据类型,等价于xml中的

@ResultMap

标记结果集的数据类型配置文件id,关联对应的XML配置文件中的id属性

8、@Result

结果集的具体的数据对应
常用属性:
id:id
property:映射属性
column:数据库列

property,column属性相同时可省略

one:一对一或多对一
many:集合,一对多
javaType:对应的java中数据类型

9、@One
用于@Result注解中
实现嵌套查询,一般用于一对一或多对一的关系中
常用属性:
select:关联查询语句
fetchType:抓取策略
取值:
FetchType.EAGER 急加载
FetchType.LAZY 懒加载
FetchType.DEFAULT

使用DEFAULT时可省略该属性

10、@Many
用于@Result注解中
实现嵌套查询,一般用于一对多或多对多
常用属性:
select:关联查询语句
fetchType:抓取策略
取值:
FetchType.EAGER
FetchType.LAZY
FetchType.DEFAULT

使用DEFAULT时可省略该属性

1、FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载。

2、FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载。

注解版联合查询语句需要分开单读写,即两个表单独查询后,合并结果集。

一对一、一对多实例


public interface TbUserMapper {

//嵌套对象:一对一
//连接查询
@Select("select u.*,c.* from tb_user u left join tb_cart c on u.cid=c.id")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="usename" ,column="usename"),
@Result(property="password" ,column="password"),
// cart为嵌套对象
@Result(property="cart.id",column="cid"),
@Result(property="cart.money",column="money"),
@Result(property="cart.count",column="count")
})
List<TbUser> queryAllByOne1();

//嵌套对象:一对一
//嵌套查询
@Select("select * from tb_user")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="usename" ,column="usename"),
@Result(property="password" ,column="password"),
@Result(property="cart",column="cid",
// column 为关联查询提供查询参数
one=@One(select="org.qf.dao.TbCartMapper.queryById",fetchType=FetchType.EAGER))
})
List<TbUser> queryAllByOne2();

//一对多的实现
//嵌套查询
@Select("select * from tb_user")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="usename" ,column="usename"),
@Result(property="password" ,column="password"),
@Result(property="orders",column="cid",
// column 为关联查询提供查询参数
many=@Many(fetchType=FetchType.EAGER,select="org.qf.dao.TbOrderMapper.queryByUid"))
})
List<TbUser> queryAllByMany1();

//连接查询
@Select("select u.*,o.*,o.id odid from tb_user u left join tb_order o on u.id=o.uid")
@ResultMap("org.qf.dao.TbUserMapper.rm1")
List<TbUser> queryAllByMany2();
}

org.qf.dao.TbUserMapper关联查询


@Select("select * from cart where cartid=#{cartid}")
cart queryByUid(String cartid);

foreach
In查询 / 批量操作

collection:表示传入过来的参数的数据类型。该参数为必选。要做 foreach 的对象,作为入参时,List 对象默认用 list 代替作为键,数组对象有 array 代替作为键,Map 对象没有默认的键。当然在作为入参时可以使用 @Param(“keyName”) 来设置键,设置 keyName 后,list,array 将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果 User 有属性 List ids。入参是 User 对象,那么这个 collection = “ids” 如果 User 有属性 Ids ids;其中 Ids 是个对象,Ids 有个属性 List id;入参是 User 对象,那么 collection = “ids.id”
如果传入的是单参数且参数类型是一个 List 的时候,collection 属性值为 list
如果传入的是单参数且参数类型是一个 array 数组的时候,collection 的属性值为 array
如果传入的参数是多个的时候,我们就需要把它们封装成一个 Map 了,当然单参数也可以封装成 map。
item: 循环体中的具体对象。支持属性的点路径访问,如 item.age,item.info.details。具体说明:在 list 和数组中是其中的对象,在 map 中是 value,该参数为必选。(它是每一个元素进行迭代时的别名)
index:在 list 和数组中,index 是元素的序号;在 map 中,index 是元素的 key。

foreach标签主要用于构建in条件,他可以在sql中对集合进行迭代。如下:

  <delete id="deleteBatch"> 

    delete from user where id in

    <foreach collection="array" item="id" index="index" open="(" close=")" separator=",">

      #{id}

    </foreach>

  </delete>

  我们假如说参数为—- int[] ids = {1,2,3,4,5} —-那么打印之后的SQL如下:

  delete form user where id in (1,2,3,4,5)

  释义:

    collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array

    item : 表示在迭代过程中每一个元素的别名

    index :表示在迭代过程中每次迭代到的位置(下标)

    open :前缀

    close :后缀

    separator :分隔符,表示迭代时每个元素之间以什么分隔

我们通常可以将之用到批量删除、添加等操作中。

@Select("<script>"
+ "SELECT IDFA FROM t_xxx WHERE IDFA IN "
+ "<foreach item='item' index='index' collection='strList' open='(' separator=',' close=')'>"
+ "#{item}"
+ "</foreach>"
+ "</script>")
@Results(value = { @Result(column = "user_name", property = "username") })
public List<String> getXxxList(@Param("strList") List<String> strList);

Mybatis 使用懒加载后,JSON 序列化错误

为SpringMVC里默认序列化使用的 com.fasterxml.jackson.databind.ObjectMapper 设置其属性

SerializationFeature.FAIL_ON_EMPTY_BEANS 为false。

3.配置json转换器属性SerializationFeature.FAIL_ON_EMPTY_BEANS为false

springboot application.yml

spring:
jackson:
serialization: {FAIL_ON_EMPTY_BEANS : false}

或者 闭该查询的懒加载 fetchType=”eager”

2.返回的类加上注解

@JsonIgnoreProperties(value = { “handler” })

参数获取

有三种方式:
1、就是普通写法,在文件中通过arg或param获取
2、使用Map集合,在文件中使用#{key}获取
3、使用注解@Param,在文件中使用#{名称}

1、arg或param获取

接口对应的方法:

int update1(String xh,int id);

映射文件的获取:

<!-- update tb_car set xh=#{arg0} where id=#{arg1} -->
<update id="update1" >
update tb_car set xh=#{param1} where id=#{param2}
</update>

可以选择使用arg获取也可以使用param获取,但是arg从0开始,而param从1开始。

2、Map集合传递多参数

接口对应的方法:

//第二种:封装成集合
int update3(Map<String,Object> map);

映射文件获取

<!--多参之二:Map集合  -->
<update id="update3" parameterType="map">
update tb_car set color=#{c} where id=#{id}
</update>

3、注解@Param传递多参数

接口对应的方法:

int update5(@Param(“xh”) String x,@Param(“id”) int i);

映射文件获取:

<update id="update5" >
update tb_car set xh=#{xh} where id=#{id}
</update>