https://blog.csdn.net/sun_soul/article/details/111773947 SQL映射文件 增删改标签 <insert id="insertEmployee"> INSERT INTO t_employee(empname,gender,email) VALUES(#{empName},#{gender},#{email})
获取自增主键的值
让MyBatis自动的将自增的id赋值给传入的employee对象的id属性:
- 原生jdbc中的getGeneratedKeys 获取由于执行此statement对象而创建的所有自动生成的键。
<!--增加-->
<!--让MyBatis自动的将自增的id赋值给传入的employee对象的id属性
useGeneratedKeys="true":开启使用自增
keyProperty="id":自增的值赋值给id属性
-->
<insert id="insertEmployee" useGeneratedKeys="true" keyProperty="id">
insert into t_employee(empname,gender,email) values(#{empName},#{gender},#{email})
</insert>
获取非自增主键的值
selectKey查询主键:
<insert id="insertEmployee2">
<!--查询主键
order="BEFORE":在核心sql语句执行之前先运行一个sql查到id
keyProperty="id":将查到数据库id的最大值加1赋值给javaBean的id属性
resultType="integer":指定返回类型未integer类型
-->
<selectKey order="BEFORE" resultType="integer" keyProperty="id">
select max(id)+1 from t_employee
</selectKey>
insert into t_employee(id,empname,gender,email) values(#{id},#{empName},#{gender},#{email})
</insert>
参数的各种取值
@Param指定封装时的参数名
public Employee getEmpByIdAndEmpName(@Param("id") Integer id, @Param("empName") String empName);
<select id="getEmpByIdAndEmpName" resultType="smq.javabean.Employee">
select * from t_employee where id=#{id} and empname=#{empName}
</select>
//传入pojo
public Employee getEmployeeByIdAndEmpName(Map<String, Object> map);
<select id="getEmployeeByIdAndEmpName" resultType="smq.javabean.Employee">
select * from t_employee where id=#{id} and empname=#{empName}
</select>
map.put("id", 1);
map.put("empName", "tomcat1");
- 多个参数自动封装map。如: method(@Param("id) Integer id, String empName, Employee employee);
Integer id---->#{id} String empName---->#{param2} Employee employee中的email---->#{param3.email}
javaType 通常可以从参数对象中来去确定 如果 null 被当作值来传递,对于所有可能为空的列,jdbcType 需要被设置 对于数值类型,还可以设置小数点后保留的位数: mode 属性允许指定 IN,OUT 或 INOUT 参数。如果参数为 OUT 或 INOUT,参数对象属性的真实值将会被改变,就像在获取输出参数时所期望的那样。
#{}与${}
select * from t_employee where id=#{id} and empname=#{empName} select * from t_employee where id=? and empname=?
select * from t_employee where id=${id} and empname=#{empName} select * from t_employee where id=1 and empname=? 一般都是用#{},因为#{}预编译安全;在不支持预编译的位置(如表名)要进行取值需要使用${}。
查询返回list
public List<Employee> getAllEmps();
// resultType:如果返回的是集合,返回类型应写集合里面元素的类型
<select id="getAllEmps" resultType="smq.javabean.Employee">
select * from t_employee
</select>
查询返回map
//查询多条记录返回多个map,记录的主键作为key,该记录封装好的对象作为value
@MapKey("id")//指定id的值作为key封装
public Map<Integer, Employee> getAllEmpsReturnMap();
<!--查询多条记录返回一个map,返回类型使用元素类型-->
<select id="getAllEmpsReturnMap" resultType="smq.javabean.Employee">
select * from t_employee
</select>
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">
<!--namespace:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。-->
<mapper namespace="smq.dao.CatDao">
<!--resultMap="mycat"指定查询数据封装结果的时候使用mycat自定义封装规则-->
<select id="getCatById" resultMap="mycat">
select * from t_cat where id = #{id}
</select>
<!--type指定为哪个javaBean自定义封装规则,id是唯一标识-->
<resultMap id="mycat" type="smq.javabean.Cat">
<!--id指定主键列的对应规则,column指定哪一列是主键列,property指定cat的哪个属性封装id这个列数据-->
<id column="id" property="id"/>
<!--普通列-->
<result column="cName" property="name"/>
<result column="cgender" property="gender"/>
<result column="cAge" property="age"/>
</resultMap>
</mapper>
使用select属性指定分步查询
public interface KeyDao {
public Key getKeyByIdSimple(Integer id);
}
public interface LockDao {
public Lock getLockByIdSimple(Integer 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">
<!--namespace:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。-->
<mapper namespace="smq.dao.KeyDao">
<select id="getKeyByIdSimple" resultMap="mykey">
select * from t_key where id=#{id}
</select>
<!--type指定为哪个javaBean自定义封装规则,id是唯一标识-->
<resultMap id="mykey" type="smq.javabean.Key">
<!--id指定主键列的对应规则,column指定哪一列是主键列,property指定cat的哪个属性封装id这个列数据-->
<id property="id" column="id"/>
<!--普通列-->
<result property="keyName" column="keyname"/>
<!--接下来的属性是一个lock对象,自定义这个对象的封装规则
select指定一个查询SQL的唯一标识,mybatis自动调用指定的sql将查出的lock封装
column指定将哪一列的数据传递进去
-->
<association property="lock" select="smq.dao.LockDao.getLockByIdSimple" column="lockid">
</association>
</resultMap>
</mapper>
<?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:名称空间。写接口给的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的。-->
<mapper namespace="smq.dao.LockDao">
<select id="getLockByIdSimple" resultType="smq.javabean.Lock">
select * from t_lock where id=#{id}
</select>
</mapper>
按需加载和延时加载
- 对于分布查询只发送一个sql
- 按需加载:需要的时候再去查询。开启全局按需加载策略。
- 这样只有使用某属性时才会去查询该属性。
<settings>
<!--开启延迟加载对象-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--开启属性按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
标签:empName,映射,employee,SQL,Mybatis,empname,where,id,select
From: https://blog.51cto.com/u_15993308/6237180