1.模糊查询用like代替=
而且不能用#{id},而是:
1.模糊查询用like代替=
而且不能用#{id},而是:
在xml中写sql语句
在user中调用xml映射
for循环中用于输出list中给存放的结果集。
在mybatis中 模糊查询(like)中 我们在sql中写%王%在mybatis中用%${王}%,用${}去包裹字符串
*******根据名字模糊查询********
1> HelloMyBatis.java
public class HelloMyBatis{
@Test
public void Test2() throws IOException{
//配置文件
String resource = "sqlMapConfig.xml";
//读取配置文件
InputStream in = Resouces.getResourceAsStream(resource);
//需要sqlSessionFactoryBuilder
sqlSessionFactoryBuilder ssfb = new sqlSessionFactoruBuilder();
//创建sqlSessionFactory
sqlSessionFactory ssf = ssfb.builder(in);
//产生一个sqlSession
SqlSession session = ssf.openSession();
//操作数据库
//参数1:要操作的sql语句 参数2:sql语句的参数
List<User> list= session.selectList("UserMapper.selectUserByName","王");
for(User u : list){
System.out.println(u);
}
}
}
2> UserMapper.xml
<!-- ${value}遵循的是OGNL表达式,内部必须为value,其他值会报错 -->
<select id="selectUserByName" parameterType="String" resultType="com.XXX.bean.User">
SELECT * FROM USER WHERE u_username LIKE '%${value}%'
</select>