- bind标签
bind标签就像是再做一次字符串拼接,方便后续使用
如本例,在模糊查询的基础上,把模糊查询改为bind标签。
<?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="com.how2java.pojo"> <!-- 本来的模糊查询方式 --> <!-- <select id="listProduct" resultType="Product"> --> <!-- select * from product_ where name like concat('%',#{0},'%') --> <!-- </select> --> <select id="listProduct" resultType="Product"> <bind name="likename" value="'%' + name + '%'" /> select * from product_ where name like #{likename} </select> </mapper>
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; 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 com.how2java.pojo.Product; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Map<String, String> params =new HashMap(); params.put("name", "product"); List<Product> ps = session.selectList("listProduct",params); for (Product p : ps) { System.out.println(p); } session.commit(); session.close(); } }
Mybatis动态SQL-bind标签
最新推荐文章于 2022-04-21 12:42:28 发布