<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="cn.gakki.MybatisTest"></package>
</typeAliases>
<environments default="yui">
<environment id="yui">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysqldriver}"></property>
<property name="url" value="${mysqlurl}"></property>
<property name="username" value="${mysqlusername}"></property>
<property name="password" value="${mysqlpassword}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper05.xml"></mapper>
</mappers>
</configuration>
1.if
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.gakki.MybatisTest.EmpMapper01">
<select id="selectMore" resultType="emp">
select * from emp where 1=1
<if test="param1!=null and param1!=''">
and ename=#{param1} //param1:方法传入的参数
</if>
<if test="param2!=null and param2!=''">
and job=#{param2}
</if>
</select>
</mapper>
public class Test3 {
public static void main(String[] args) throws Exception {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis05.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession session = factory.openSession();
EmpMapper01 mapper=session.getMapper(EmpMapper01.class);
List<EMP> list=mapper.selectMore("","");
for(EMP emp:list){
System.out.println(emp);
}
session.close();
}
}
2.where
3.bind :用于模糊查询
4.include:引用<sql>中的sql语句
5.foreach
collection : 集合类型:list, array
open :开始符号
close:结束符号
separator :分隔
item:迭代主量