Mybatis (三)

Mybatis SQL映射文件解析

参考文章:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html

MyBatis 的真正强大在于它的映射语句,也是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的,并且比普通的方法做的更好。
Mybatis (三)_第1张图片

增删改查 的xml 配置

	
	<insert id="addEmp">
		insert into hr_o_employee (employee_ID ,employeeName,gender,email)
			values(#{employee_ID},#{employeeName},#{gender},#{email})
	insert>
	
	<insert id="addEmpForId"  useGeneratedKeys="true" keyProperty="id">
		insert into hr_o_employee (employee_ID ,employeeName,gender,email)
			values(#{employee_ID},#{employeeName},#{gender},#{email})
	insert>

对于不支持自动生成类型的数据库或可能不支持自动生成主键的 JDBC 驱动,MyBatis 有另外一种方法来生成主键。

	
	<insert id="insertAuthor">
	  <selectKey keyProperty="id" resultType="int" order="BEFORE">
	    select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
	  selectKey>
	  insert into Author
	    (id, username, password, email,bio, favourite_section)
	  values
	    (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
	insert>
	
	<delete id="delete">
		delete from hr_o_employee where employee_id=#{employee_ID}
	delete>
	
	<update id="update">
		update hr_o_employee set employeeName=#{employeeName} where employee_ID=#{employee_ID}
	update>
  • 查询:单个参数
	
	<select id="getEmp" resultType="com.myt.bean.Employee">
		select * from hr_O_Employee where employee_id = #{employee_id}
	select>
  • 查询:多个参数
	
	
	
	<select id="getEmpByMore" resultType="com.myt.bean.Employee">
		select * from hr_O_Employee where employee_id = #{0} and employeeName=#{1}
	select>
	
	
	
	<select id="getEmpByMore" resultType="com.myt.bean.Employee">
		select * from hr_O_Employee where employee_id = #{empid} and employeeName=#{eName}
	select>
	
	
	
	<select id="getEmpByMore" resultType="com.myt.bean.Employee">
		select * from hr_O_Employee where employee_id = #{employee_ID} and employeeName=#{employeeName}
	select>
	
	
	
	<select id="getEmpByMore" resultType="com.myt.bean.Employee">
		select * from hr_O_Employee where employee_id = #{empid} and employeeName=#{eName}
	select>


你可能感兴趣的:(Mybatis)