ssm--mybatis2

动态(dynamic)拼接sql

<mapper>
<select id="findUserByCondition" parameterType="com.baidu.domain.User" >
    	select * from user  where 1= 1   //注意,这里不能写";",如果写了分号,底层会拼接进去
    <where> //如果使用where标签,上面的 where 1= 1就省略,但是标签内容体
不变,第一个if标签的and可以省略,其他的不能省略
    <!--根据用户名模糊查询-->
    <if test="username != null and username != '' ">  //test中的username对应实体类属性
		and username like  #{username} / '${username}'   //注意,传参的时候模糊查询要带上%%
    </if>
    <!--根据性别查询-->
    <if test="sex != null and sex != '' ">
        and sex = #{sex} / '${sex}'
    </if>
     <!--根据id遍历查询-->
       <foreach collection="ids" item="id" open=" and id in (" close=")" separator=",">
           	#{id}  //#{id}跟item对应,在jstl中,collection-->items , item-->var 
           //ids对应parameterType的集合属性值,separator分隔符
        </foreach>
    </where>
</select>
</mapper>
注意:
    1.where标签会赠送一个where,但是不赠送and,如果使用where标签依然自己拼接where 1 = 1,得到的sql就有两个where。第一个if标签的and会自动被过滤掉 select * from user WHERE username like '%王%' and sex = '女' ;
    2.在mybatis中映射配置文件中的连接条件只支持"and"不支持"&",也不支持"&&",只要在IUserDao.xml页面出现了"&"无论在什么位置,都会报错
	3. test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法
	4.<foreach>
        collection:代表要遍历的集合元素,注意编写时不要写#{}
        open:代表语句的开始部分
        close:代表结束部分 
        item:代表遍历集合的每个元素,生成的变量名,注意遍历item的时候需要#{}
        separator:代表分隔符 
	</foreach>
	5.动态拼接sql时,在每次换行后面都要带上空格,否则拼接很容易就导致连在一起,最好多打几个空格
引入一段sql语句
<mapper>
    <!--定义sql片段-->
	<sql id="includeSQL">
        select * from user
    </sql>
    <select id="findUserByName" parameterType="com.baidu.domain.User" resultType="com.baidu.domain.User">
        <!--引入sql片段-->
        <include refid="includeSQL"></include> //相当于把sql标签声明的sql语句在这里再写一次
    </select>
</mapper>	

sql id : 唯一标识,用来给statement引用
include refid : 指定sql id ,引用sql语句
多表查询
一对一(多对一):关键字association(协会)
	方式一:
	自定义一个实体类,继承第一张表的实体类,添加第二张表的字段名,再重写toString()方法
时,加上super.toString(),resultType="UserAccount"
	eg: UserAccount extends User //一对一查询user表和account表
	方式二:
	在User实体类中包装一个Account类,使用resultMap
	private 从表实体类 从表;
<mapper>
<resultMap id="userAccountOne" type="com.baidu.domain.User">
    <!--指定主表的实体类属性-->
    <id column="id" property="id"></id>   //id,result写自闭合,围堵都可以
    <result column="username" property="username"></result>
    <result column="address" property="address"></result>
    <!--指定从表的实体类属性-->
    <association javaType="com.baidu.domain.Account" property="account" column="uid">
        <id column="uid" property="uid">
        <result column="money" property="money"></result>
    </association>
</resultMap>
</mapper>
    在association标签内指定从表实体类属性,javaType匹配从表实体类的全限定类名,property匹配从表属性名,
column匹配主表外键
    public class User{
    	private Integer id;
    	private Account account; 
    	...
    }
      public class Account{
    	private Integer uid;
    	...
    }
一对多(多对多):关键字collection集合)
	private List<从表实体类> list;
	<resultMap id="userAccountMore" type="com.baidu.domain.User">
        <!--指定主表的实体类属性-->
         <id column="id" property="id"></id>  
		<result column="username" property="username"></result>
		<result column="address" property="address"></result>
        <!--指定从表的实体类属性-->
        <collection javaType="java.lang.List"
 ofType="com.baidu.domain.Account"  property="accounts" column="uid">
             <id column="uid" property="uid">
        	<result column="money" property="money"></result>
        </collection>
	</resultMap>
collection javaType: 指定从表在主表中的属性对应的数据类型
collection ofType: 指定集合元素的数据类型,即List中的对象类型	
collection property: 指定从表在主表中的属性名
注意:
	1.一对一,多对一指定从表的实体类属性使用association标签,javaTyp属性
	2.一对多,多对多指定从表的实体类属性使用collection标签,ofType属性
	3.一对一/多对一 : private Account account; 
	  一对多/多对多 : private List<Account> accounts;
	4.主表都是在resultType标签上指定,都需要把从表的实体类包装成主表的属性
	5.association/collection标签中的column指定主表跟从表关联的主表外键
企业开发中,如果涉及多张表的多表查询,通常在一个domain中,定义所有表的id,
public class Tables{
	private int aid;
	private int bid;
	private int cid;	
		...
}
在配置文件中配置
<resultMap id="tableMap" type="com.baidu.domain.Tables">
    <id column="aid" property="uid"/>
    <result column="bid" property="bid" />
    <result column="cid" property="cid" />
    ...
    <!--不使用association/collection-->
</resultMap>
select a.id,b,id,c.id from ...
SqlMapConfig.xml配置
<dataSource type="POOLED">
type的取值:
UNPOOLED 	不使用连接池的数据源,MyBatis会创建unPooledDataSource实例
POOLED    	使用连接池的数据源,MyBatis创建pooledDataSource实例
JNDI  采用服务器提供的JNDI技术实现,来获取DataSource对象,不同的服务器所能拿到DataSource是不一样。
<dataSource>
<property name="driver" value="${jdbc.driver}"> //这里不能使用#{},#{}是sql预编译处理,这个不是预编译,只需要字符串的替换即可
</dataSource>
事务Transaction
	mybatis底层自动设置了connection.setAutoCommit(false),所以执行增删改(DML)操作时,需要手动提交事务,sqlSession.commit();也可以SqlSession sqlSession = factory.openSession(true),获取sqlSession对象
时使用true构造方法,表示自动提交事务,但是这样不安全,所以通常还是手动提交事务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值