mybatis核心配置文件添加
启用驼峰命名,将字段的下划线转换成驼峰模式
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
案例
接口
public interface NameDao {
Name getNameById(int id);
List<Name> listNames();
long countName();
Map<String,Object> mapName(int id);
List<Map<String,Object>> mapNames();
Name getNameByName(@Param("id") int id,@Param("name") String name);
}
配置文件
<?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">
<!-- namespace指定接口的完整类名 -->
<mapper namespace="top.lldwb.mybatis.dao.NameDao">
<!-- resultType返回类型 -->
<select id="getNameById" resultType="name" parameterType="int">
select id, name
from name
where id = #{id}
</select>
<!-- 在返回集合时,resultType中输入集合的类型(并不是List<Name>) -->
<select id="listNames" resultType="name">
select id, name
from name
</select>
<!-- 返回特定的值 -->
<select id="countName" resultType="long">
select count(*)
from name
</select>
<!-- 返回map -->
<select id="mapName" resultType="map" parameterType="int">
select id, name
from name
where id = #{id}
</select>
<!-- 返回List<Map> -->
<select id="mapNames" resultType="map">
select id, name
from name
</select>
<!-- 多参数查询 -->
<select id="getNameByName" resultType="name">
select id, name
from name
where id = #{id}
and name = #{name}
</select>
</mapper>
Map案例
<!-- 声明resultMap用于User实体类和user_info的表映射-->
<resultMap id="userMap" type="user">
<!-- id用于映射主键,property指定实体的字段名,column指定表列的名称 -->
<id property="userId" column="user_id"/>
<!-- 其他属性使用result标签来映射-->
<id property="userName" column="user_name"/>
<id property="userPassword" column="user_password"/>
<id property="userMail" column="user_mail"/>
<id property="userState" column="user_state"/>
<id property="userKey" column="user_key"/>
</resultMap>
<select id="getUserByName" resultMap="userMap">
select user_id, user_name, user_password, user_mail, user_state, user_key
from user
where user_name = #{name}
</select>