MyBatis学习之SQL查询

文章目录

  • resultMap结果映射
    • 使用resultMap结果映射
    • 是否开启驼峰命名自动映射
  • 返回总记录条数

resultMap结果映射

使用resultMap结果映射

  • 专门定义一个结果映射,在这个结果映射当中指定数据库表的字段名和Java类的属性名的对应关系
  • type属性:用来指定POJO类的类名
  • id属性:指定resultMap的唯一标识。这个id将来要在select标签中使用。

  • 有关于resultMap的子标签及子标签的属性
  • id:如果数据库表中有主键(一般都是有主键的,要不然不符合数据库设计的第一范式。),建议这里配置一个id标签,注意:这不是必须的。但是官方的解释是什么呢?这样的配置可以让mybatis提高效率
  • result的property属性填写的是POJO类的属性名
  • result的column属性填写的是数据库表的字段名
<resultMap id="carResultMap" type="Car">
	<id property="id" column="id" />
	<result property="carNum" column="car_num"/>
	<result property="guidePrice" column="guide_price"/>
	<result property="produceTime" column="produce_time"/>
	<result property="carType" column="car_type"/>
resultMap>
<select id-"selectAllByResultMap" resultMap="carResultMap">
	select * from t_car
select>

select标签的resultMap属性,用来指定使用哪个结果映射。resultMap后面的值是resulrMap的id

是否开启驼峰命名自动映射

使用这种方式的前提是:属性名遵循Java的命名规范,数据库的列名遵循SQL的命名规范。
Java命名规范:首字母小写,后面每个单词首字母大写,遵循驼峰命名方式。
SQL命名规范:全部小写,单词之间采用下划线分割。
在这里插入图片描述


<setting>
	<setting name="mapUnderscoreToCamelCase" value="true" />
setting>
//CarMapper接口
List<Car> selectAllByMapUnderscoreToCamelCase();

<select id="selectAllByMapUnderscoreToCamelCase" resultType="Car">
	select * from t_car
select>

返回总记录条数

//CarMapper接口
Long selectTotal();
//CarMapper.xml
<select id="selectTotal" resultType="long">
	select count(*) from t_car
select>

你可能感兴趣的:(mybatis学习笔记,mybatis,学习,sql)