目的:
- 学会编写的 mybatis 的综合查询、使用别名的方法配置 mapper
完成内容
- 编写一个 MyBatis 的程序 ,使用代理方式,进行综合查询
对客户表的编号,名称,地址,电话进行模糊查询
- 修改前面的作业的 mapper 配置文件,使用别名的方法配置,测试配置正确
ClientMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yang.mapper.ClientMapper">
<select id="getClientsByLike" resultType="com.yang.pojo.Client">
select * from client where name like #{name} or id like #{id} or address like #{address} or phone like #{phone}
</select>
</mapper>
ClientMapper.java
package com.yang.mapper;
import com.yang.pojo.Client;
import java.util.List;
public interface ClientMapper {
public List<Client> getClientsByLike(Client c);
}
Test
package com.yang.mybatis;
import com.yang.pojo.Client;
import com.yang.pojo.Goods;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class QueryMultiParamTest {
static SqlSessionFactory ssf = null;
static {
InputStream is;
try{
is = Resources.getResourceAsStream("sqlMapConfig.xml");
ssf = new SqlSessionFactoryBuilder().build(is);
} catch(IOException e) {
e.printStackTrace();
}
}
@Test
public void testQueryMultiParam() throws IOException {
SqlSession ss = ssf.openSession();
Client c = new Client();
c.setId("%1%");
c.setAddress("%贵%");
c.setName("%赵%");
c.setPhone("%88%");
List<Client> cList = ss.selectList("com.yang.mapper.ClientMapper.getClientsByLike", c);
for (Client C : cList) {
System.out.println(C);
}
ss.close();
}
}
起别名
<typeAliases>
<typeAlias type="com.yang.pojo.Client" alias="client"/>
<package name="com.yang.pojo"/>
</typeAliases>