目录
<mapper>
<mapper>元素是SQL映射文件的顶级元素。它用于指定命名空间,即DAO接口的全限定类名或包名。只有一个属性namespace,用于区分不同的mapper,必须全局唯一
<cache>
<cache>元素用于配置缓存,如启用二级缓存等设置。
<resultMap>
<resultMap>元素用于定义结果集映射,比如数据库字段到Java对象属性的映射关系。
<sql>
<sql>元素用于定义可重用的SQL代码片段,可以在其他语句中引用。提高了维护sql语句的效率
<select>
<select>元素用于定义查询语句,如查询列表或单个对象。
<insert>
<insert>元素用于定义插入语句。映射insert语句,执行新增操作
<update>
<update>元素用于定义更新语句。映射update语句,执行修改操作
<delete>
<delete>元素用于定义删除语句。映射delete语句,执行删除操作
代码如下
//定义DAO接口方法
public interface UserMapper {
User getUser(Long id);
}
<?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">
//在SQL映射文件中添加对应的命名空间
<mapper namespace="com.example.UserMapper">
//定义查询语句
<select id="getUser" parameterType="long" resultType="com.example.User">
select * from user where id = #{id}
</select>
</mapper>
最后测试
SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.getUser(1L);
对于多条件查询,MyBatis 框架提供了多种方法实现条件赋值,resultType
1。将查询条件封装成Java对象作为入参,步骤:
//实体类
public class User {
private int id;
private String name;
// getter and setter
}
//Mapper接口
interface UserMapper {
User selectById(int id);
List<User> selectAll();
}
- Mapper配置文件,注意看resultType
<?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.example.UserMapper">
<select id="selectById" resultType="User">
select id, name from user where id = #{id}
</select>
<select id="selectAll" resultMap="UserResultMap">
select id, name from user
</select>
<resultMap id="UserResultMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
</mapper>
测试:
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectById(1);
List<User> users = mapper.selectAll();
2。将查询条件封装成Java对象作为入参,步骤:
Mapper接口:
List<User> select(Map<String, Object> params);
Mapper配置 (xml)
<select id="select">
select * from user
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
</where>
</select>
测试
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
List<User> users = userMapper.select(params);
这种方式更加简单,直接使用Map作为条件参数,省去了定义参数对象和映射的步骤。
当查询条件较简单时,直接使用Map封装条件就可以满足需求了。
3.使用@Param注解可以实现多个参数入参的查询
定义接口方法,使用@Param指定参数名
List<User> selectByParams(@Param("id") Integer id,
@Param("name") String name);
Mapper配置文件
<select id="selectByParams">
select * from user
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
</where>
</select>
调用方法
userMapper.selectByParams(1, "John");
使用@Param可以给每个参数指定明确的名字,同时在SQL中使用#{paramName}引用,更清晰直观。
这种方式支持多个参数,相比使用Map参数更清晰明了每个参数的意义。
当查询条件较多,参数类型明确的时候,使用@Param注解是一种不错的选择。
区别:
-
作用对象
-
resultType用于指定返回结果集映射到哪个POJO类型
-
resultMap用于指定更复杂的结果集映射关系
-
映射能力
-
resultType只支持简单的自动映射
-
resultMap支持手动定义每个列到属性的映射关系
-
使用场景
-
如果结果集与POJO字段一一对应,可以使用resultType
-
如果结果集结构复杂(如一对多,多对一映射等),必须使用resultMap
-
映射规则
-
resultType依赖于自动映射规则(列名与属性名一致)
-
resultMap可以手动指定任意列到属性的映射关系
-
同时使用
-
一个Select标签只能指定resultType或resultMap其中一个
-
resultMap会覆盖resultType
所以:
-
对于简单映射,使用resultType
-
对于复杂映射,使用resultMap手动定义映射关系
1.增加操作
<insert id="insert">
insert into user (name, age)
values (#{name}, #{age})
</insert>
2.删除操作
<delete id="delete">
delete from user where id = #{id}
</delete>
3.修改操作
<update id="update">
update user
set name=#{name}, age=#{age}
where id = #{id}
</update>
4.Mapper接口
int insert(User user);
int delete(int id);
int update(User user);
5.测试
// 增加
int rows = userMapper.insert(new User("张三", 20));
// 删除
int rows = userMapper.delete(1);
// 更新
user.setName("李四");
int rows = userMapper.update(user);