<select id="selectWebsite" resultType="net.biancheng.po.Website"> <bind name="pattern" value="'%'+_parameter+'%'" /> SELECT id,name,url,age,country FROM website WHERE name like #{pattern} </select>bind 元素属性如下。
public List<Website> selectWebsite(Website site);SQL 映射文件代码如下。
<select id="selectWebsite" resultType="net.biancheng.po.Website"> <bind name="pattern_name" value="'%'+name+'%'" /> <bind name="pattern_url" value="'%'+url+'%'" /> SELECT id,name,url,age,country FROM website WHERE name like #{pattern_name} AND url like #{pattern_url} </select>测试代码如下。
public class Test { public static void main(String[] args) throws IOException { // 读取配置文件mybatis-config.xml InputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // 根据配置文件构建 SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config); // 通过SqlSessionFactory创建SqlSession SqlSession ss = ssf.openSession(); Website site = new Website(); site.setname("编程"); site.setUrl("http"); List<Website> siteList = ss.selectList("net.biancheng.mapper.WebsiteMapper.selectWebsite", site); for (Website ws : siteList) { System.out.println(ws); } } }运行结果如下。
DEBUG [main] - ==> Preparing: SELECT id,name,url,age,country FROM website WHERE name like ? AND url like ?
DEBUG [main] - ==> Parameters: %编程%(String), %http%(String)
DEBUG [main] - <== Total: 1
Website[id=1,name=编程帮,url=https://www.lmcjl.com/,age=10,country=CN]
本文链接:http://task.lmcjl.com/news/5233.html