MyBatis框架学习2

1. <typeAliases>标签

1.1 功能  


1.2 使用方式


2. mybatis中含参数增删改查

核心


2.1  含一个参数


2.2 多个参数查询

2.2.1使用原理


2.2.2 对象封装解决案例


2.2.3 map封装解决案例


3. 事物提交处理

3.1 事物提交本质


3.2 事物提交处理案例代码


4. 接口绑定

4.1 接口绑定的理论理解



4.2 接口的要求

a) xml 文件名要和接口名一致

b) namespace 属性必须为接口的全限定路径

c) id 属性必须和接口对应的方法名一致

4.3 接口绑定的代码案例


4.4 通过接口绑定解决多参数的传递

4.4.1 方案一

a) 接口中定义方法 

User selByUP(String username, String password);

b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{index}或#{param+数字}的方式.

<select id="selByUP" resultType="user"> 

                select * from t_user where username=#{0} and password=#{1}

</select>

4.4.2 方案二

a) 接口中定义方法, 参数中使用@Param 注解设定参数名用于在 SQL 语句中使用.

User selByUP(@Param("username") String username, @Param("password") String password);

b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{参数名称}或#{param+数字}的方式.

<select id="selByUP" resultType="user"> 

    select * from t_user where username=#{username} and password=#{password}

</select>

5. 动态SQL 

作用


5.1 <if>

用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件.

<select id="sel" resultType="user"> 

                    select * from t_user where 1=1 

                                    <if test="username != null and username != ''"> 

                                                    and username=#{username} 

                                    </if>

                                    <if test="password != null and password != ''"> 

                                                     and password=#{password} 

                                     </if>

</select>

.5.2 <where>


5.3 <choose><when><otherwise>


5.4 <set>


5.5 <trim>


5.6 <bind>


5.7 <foreach>


5.8 <sql><include>


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容