这里介绍基础的mabtis中动态sql的使用
导入必要的jar包
mybatis
JUnit4做测试需要用到的包
mysql操作数据库要用到的包
log4j本次测试使用了log4j.proirtes文件
mybaties_config.xml配置文件
<?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>
<!-- porperties使用 -->
<properties resource="db.properties">
<property name="db.username" value="xxx"/>
<property name="db.password" value="xxx"/>
</properties>
<!-- 设置参数 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 别名 -->
<typeAliases>
<package name="cn.com.pojo"/>
</typeAliases>
<!-- 环境变量 -->
<environments default="dev">
<!-- 开发环境 -->
<environment id="dev">
<!-- 事务管理 JDBC 使用简单JDBC完成事务的提交和回滚-->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源配置 POOLED表示开启链接池,mybatis自带的dbcp连接池-->
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<!-- 单个文件映射 -->
<mapper resource="cn/com/pojo/UsertblMapper.xml"/>
</mappers>
</configuration>
实体类
提供set() get() 以及toString方法
编写对应的mppe.xml文档 以及接口 和测试类
<mapper namespace="cn.com.mapper.xxx"> <!--xxx是对应的接口类名-->
<!-- resultMap 是做返回结构映射的
type是说明要映射那个类型
id 标识 为后面的返回结果引用
的resultMap
-->
<resultMap type="Studenttbl" id="stuResultMap">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
<collection property="coursetbls" javaType="list" ofType="Coursetbl">
<id column="cid" property="cid"/>
<result column="cname" property="cname"/>
</collection>
</resultMap>
<!--if的用法-->
<select id="findbyCondition" parameterType="map" resultMap="stuResultMap">
select * from studenttbl s where 1=1
<if test="sid!=null and sid != ''">
and s.sid>#{sid}
</if>
<if test="sname!=null and sname != ''">
<![CDATA[
and s.sname=#{sname}
]]>
</if>
</select>
提供相应接口
public List<Studenttbl> findbyCondition(Map<String, Object> m);
测试类
@Test
public void test1(){
SqlSession session=this.sessionFactory.openSession();
StudenttblMapper mapper=session.getMapper(StudenttblMapper.class);
Map<String, Object> map=new HashMap<>();
map.put("sid", 2);
//map.p );
List<Studenttbl> ls=mapper.findbyCondition(map);
System.out.println(ls);
session.close();
}
下面的不包含具体的测试类,只介绍几个动态sql方法
<!--where-->
<select id="findbywhere" parameterType="map" resultMap="stuResultMap">
select * from studenttbl s
<where>
<if test="sid!=null and sid != ''">
and s.sid>#{sid}
</if>
<if test="sname!=null and sname != ''">
and s.sname=#{sname}
</if>
</where>
</select>
<!-- foreach -->
<select id="findbyin" parameterType="List" resultMap="empResultMap">
select * from emptbl where empid in
<foreach collection="list" item="it" open="(" close=")" separator=",">
#{it}
</foreach>
</select>
<!-- 模糊查询 -->
<select id="findbylike" parameterType="Emptbl" resultMap="empResultMap">
select * from emptbl where empname like '%${empname}%'
</select>
<select id="findbybind" parameterType="string" resultMap="empResultMap">
<bind name="pattern" value="'%'+_parameter+'%'"/>
select * from emptbl where empname like #{pattern}
</select>