Mybatis学习笔记三(Mybatis SQL映射文件)


前面学习的都是一些配置,mybatis的精华也就集中在SQL的映射文件上,相比实现相同功能的jdbc代码,节约了95%的代码量。


<!-- 配置给定命名空间的缓存 -->
<cache></cache>

<!-- 从其他命名空间引用缓存配置 -->
<cache-ref namespace="" />

<!-- 描述如何将db中查询的结果集加载成对象 -->
<resultMap type="" id="">
<constructor>
<idArg />
<arg />
<arg />
<arg />
</constructor>
</resultMap>

<!-- 定义可重用的sql语句 -->
<sql id=""></sql>

<!-- 映射dml语句 -->
<insert id=""></insert>
<select id=""></select>
<update id=""></update>
<delete id=""></delete>



一、select可以可以说是使用最多的元素,使用也很简单

    <select id="getUserById" parameterType="int" resultType="User">
        select * from tbl_user where id = #{id}
    </select>

<select>元素中的一些属性(红色为用的较多的属性)

id 在命名空间中唯一的标识符,可以被用来引用这条语句
parameterType将会传入这条语句的参数类的完全限定名或别名。
resultType 从这条语句中返回的期望类型的类的完全限定名或别名。
意集合情形,那应该是集合可以包含的类型,
而不能是集合本身。使用 resultType或 resultMap,
但不能同时使用
。(可以是基本类型int等,
复合类型User,集合类型map,list等)
resultMap 命名引用外部的resultMap。返回map是MyBat is
 最具力量的特性,对其有一个很好的理解的话,
多复杂映射的情形就能被解决了。
使用 resultMap 或resultType,但不能同时使用
flushCache 将其设置为 true,不论语句什么时候被带哦用,
都会导致缓存被清空。默认值:false
useCache 将其设置为 true,将会导致本条语句的结果被缓存。
默认值:true
timeout  这个设置驱动程序等待数据库返回请求结果,并抛出
异常时间的最大等待值。默认不设置(驱动自行处理)
fetchSize 这是暗示驱动程序每次批量返回的结果行数。
默认不设置(驱动自行处理)
statementType STATEMENT,PREPARED 或 CALLABLE 的一种。
这会让 MyBat is使用选择使用
Statement,PreparedStatement或 CallableStatement。
默认值:PREPARED。
resultSetTypeFORWARD_ONLY,
SCROLL_SENSITIVE,
SCROLL_INSENSITIVE中的一种。
默认是不设置(驱动自行处理)
  
  

二、insert插入数据库,进行插入操作时主要是要拿到插入数据自增的主键

id 在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType 
将会传入这条语句的参数类的完全限定名或别名
flushCache 将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被清空。默认值:false。
timeout这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的最大等待值。
默认不设置(驱动自行处理)。 
statementTypeSTATEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBat is使用选择使用
Statement,PreparedStatement 或 CallableStatement。默认值:PREPARED。 
useGeneratedKeys仅对 insert 有 用 ) 这 会 告 诉 MyBat is 使用 JDBC 的getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。
keyProperty仅对insert有用) 标记一个属性, MyBat is会通过getGeneratedKeys或者通过 insert 语句的selectKey 子元素设置它的值。默认:不设置。
  

在进行insert,update,delete之后session要进行commit操作,不然数据库不会更新到数据库

insert的Demo

另一种获取主键的方法

    <insert id="addUserLastId" parameterType="User">
    	<selectKey resultType="int" order="AFTER" keyProperty="id">
    		SELECT LAST_INSERT_ID() AS id
    	</selectKey>
    	insert into tbl_user(name,age) values(#{name},#{age})
    </insert>



    <insert id="addUser" parameterType="User" keyProperty="id" useGeneratedKeys="true" >
    	insert into tbl_user(name,age) values(#{name},#{age})
    </insert>
	@Test
	public void testInsert() throws IOException {
		UserMapper mapper = session.getMapper(UserMapper.class) ; 
		User user = new User() ;
		user.setName("zhangss") ;
		user.setAge(22) ;
		int i = mapper.addUser(user) ;
		session.commit() ;
		session.close() ;
		System.out.println("id:"+i+"--"+user.getId());
	}


update的Demo

    <update id="updateUser" parameterType="User">
    	update tbl_user set name = #{name} ,age = #{age} where id = #{id}
    </update>

	@Test
	public void testUpdate(){
		UserMapper mapper = session.getMapper(UserMapper.class) ; 
		User user = new User() ;
		user.setId(27) ;
		user.setName("zhang27") ;
		user.setAge(227) ;
		int i = mapper.updateUser(user) ;
		session.commit() ;
		session.close() ;
		System.out.println("id:"+i+"--"+user.getId());
	}


三、sql定义可重用的sql语句

<sql id="selectItem">id,name,age</sql>


    <select id="getUserById" parameterType="int" resultType="User">
    	
    	<!-- 
        select * from tbl_user where id = #{id}
         -->
         select <include refid="selectItem"/> from tbl_user where id = #{id}
    </select>


四、Parameter


#{department, mode=OUT, jdbcType=CURSOR, javaType=ResultSet,resultMap=departmentResultMap} 


五、resultMap所做的工作就是将从数据库中获取的ResultSet结果集放入指定的对象中,避免大量的setter getter代码,实现自动装配的功能,实现结果的映射


1、简单映射

<select id=”selectUsers” parameterType=”int” resultType=”hashmap”> 
	select id, username, hashedPassword from some_table where id = #{id} 
</select> 

所有列被自动映射到 HashMap 的键上,key为列名,value为数据库中的数据


<select id=”selectUsers” parameterType=”int” resultType=”com.someapp.model.User”> 
	select id, username, hashedPassword from some_table where id = #{id} 
</select> 

将所有从数据库中取得数据自动装配到JavaBean中,如果列名与属性名相同,则无需作任务的干预即可完成装配。

如果数据库中的列名与javabean的属性名称不同可以在查询的时候取别名,如

<select id=”selectUsers” parameterType=”int” resultType=”User”> 
	select user_id as “id”, user_name as “userName”, hashed_password as “hashedPassword”  from some_table where id = #{id} 
</select> 

也可以使用resultMap进行映射的指定

<resultMap id="userResultMap" type="User"> 
	<id property="id" column="user_id" /> 
	<result property="username" column="username"/> 
	<result property="password" column="password"/> 
</resultMap> 

<select id=”selectUsers” parameterType=”int”  resultMap=”userResultMap”> 
	select user_id, user_name, hashed_password from some_table where id = #{id} 
</select> 


2、resultMap高级映射


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值