Mybatis--------mapper配置文件介绍

1.获取自增主键
①MySQL:以自增的方式生成主键

<insert id="saveCustomer" parameterType="Customer" useGeneratedKeys="true" keyProperty="custId">
			INSERT INTO tbl_cust (cust_name, cust_age) VALUES (#{custName}, #{custAge})
</insert>
		
②Oracle:以序列方式生成主键		
<insert id="add" parameterType="Person">
			<selectKey order="BEFORE" keyProperty="id" resultType="_int">
				select crm_seq.nextval from dual
			</selectKey>
			INSERT INTO tbl_cust (cust_name, cust_age) VALUES (#{custName}, #{custAge})
		</insert>		
③基于注解开发方式
	@Insert("INSERT INTO tbl_cust (cust_name, cust_age) VALUES (#{custName}, #{custAge})")
	@Options(useGeneratedKeys=true,keyProperty="id")
	public void add(Person person);

2.parameterType输入参数类型
①传入简单类型,比如按照id查Person
②传入POJO类型
③传入Map/HashMap
[1]接口方法:List<Customer> getCustomerByCondition(Map<String, Object> map);
[2]映射文件:

<select id="getCustomerByCondition" parameterType="map" resultType="Customer">
				select cust_id custId,cust_name custName,cust_age custAge
				from tbl_cust <![CDATA[where cust_age>#{minAge} and cust_age<#{maxAge}]]>
			</select>
④字符串替换
	默认情况下,使用#{}格式的语法会导致MyBatis创建预处理语句属性并以它为背景设置安全的值(比如?)。
	这样做很安全,很迅速,也是首选的做法,有时你只是想直接在SQL语句中插入一个不改变的字符串。
	比如,像ORDER BY,你可以这样来使用:
	ORDER BY ${columnName}
	这里MyBatis不会修改或转义字符串。
	重要:接受从用户输出的内容并提供给语句中不变的字符串,这样做是不安全的。
	这会导致潜在的SQL注入攻击,因此你不应该允许用户输入这些字段,或者通常自行转义并检查。

⑤口袋POJO,混合型传入参数,进公司看团队技术流风格(介绍一下即可)



public class UserAdressBean{
			User user ;
			Address address ;
		}
		public class UserAdressBean{
			String username ;
			String city ;
			String code ;
		}

3.resultType输出类型
①单个POJO
②List
③Map/HashMap
查询结果必须在列名和属性名一致的情况下才能够封装为POJO,如果不一致则可以考虑封装为Map返回。
字段名作为Map的键,字段值作为Map的值
④单个值:Integer/double/…

4.resultMap结果集映射
①映射普通字段

<resultMap type="Customer" id="customerResultMap">
			<id property="custId" column="cust_id"/>
			<result property="custName" column="cust_name"/>
			<result property="custAge" column="cust_age"/>
		</resultMap>

		<select id="getCustomerById" parameterType="Integer" resultMap="customerResultMap">
			select cust_id,cust_name,cust_age
			from tbl_cust where cust_id=#{custId}
		</select>
②映射POJO属性
	

association:给POJO属性指定赋值方式
		javaType:指定POJO属性的类型
		<resultMap type="com.sirius.mybatis.entity.Emp" id="empResultMap">
			<id column="emp_id" property="empId"/>
			<result column="emp_name" property="empName"/>
			<association property="dept" javaType="com.sirius.mybatis.entity.Dept">
				<id column="dept_id" property="deptId"/>
				<result column="dept_name" property="deptName"/>
			</association>
		</resultMap>

		<select id="getEmpById" resultMap="empResultMap" parameterType="Integer">
			SELECT tbl_dept.dept_id,tbl_dept.dept_name,tbl_emp.emp_id,tbl_emp.emp_name 
			FROM tbl_emp,tbl_dept 
			WHERE tbl_emp.dept_id=tbl_dept.dept_id AND tbl_emp.emp_id=#{empId}
		</select>
	
③映射集合属性
	collection:给集合属性中的元素对象赋值 
	ofType:指定元素对象的类型
	

<resultMap type="com.sirius.mybatis.entity.Dept" id="deptResultMap">
			<id property="deptId" column="dept_id"/>
			<result property="deptName" column="dept_name"/>
			<collection property="empSet" ofType="com.sirius.mybatis.entity.Emp">
				<id column="emp_id" property="empId"/>
				<result column="emp_name" property="empName"/>
			</collection>
		</resultMap>

		<select id="getDeptById" resultMap="deptResultMap">
			select d.dept_id,d.dept_name,e.emp_id,e.emp_name 
			from tbl_dept d,tbl_emp e 
			where d.dept_id=e.dept_id and d.dept_id=#{deptId}
		</select>		
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值