一、在mapper中几个常用元素
- select –映射查询语句
- insert -映射插入语句
- update –映射更新语句
- delete –映射删除语句
- sql –可以重用的 SQL 块,也可以被其他语句引用
- bind –将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值
- resultMap –将数据库查询结果集映射到对象中
二、基本结构
<?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="problemMapper">
....
</mapper>
namespace: 标识Mapper.xml文件,让Dao层通过SqlSessionTemplate类的方法来找到。在配置文件application.properties中mybatis.mapper-locations=classpath:mapper/*Mapper.xml有这样配置,那么不需要写完整路径,只需要如上段代码所示…Mapper即可。一定需要写成这样的格式,不要写错,不然会报找不到的错误。Dao层代码如下:
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public List<Problem> selectAll() {
return sqlSessionTemplate.selectList("problemMapper.selectAll");
}
三、 常用标签元素使用
1. select
<!--- 根据用户id查询用户信息,接收一个integer类型的参数,并将返回结果映射 -->
<select id="findById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from user where 1=1
<if test="_parameter != null">
and _id = #{
id,jdbcType=Int