企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> 与`MyBatis根据名称模糊查询用户1`类似,不同点是UserMapper.xml中的语句 ### 修改UserMapper.xml > 这是一种字符串拼接的方式。了解即可。 ```xml <?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.gosuncn.dao.UserMapper"> <select id="findUserByName" parameterType="java.lang.String" resultType="com.gosuncn.entity.User"> select id, name, age, address from user where name like '%${value}%' </select> </mapper> ``` > ```mysql > select id, name, age, address from user where name like '%${value}%' > ``` 其中,`'%${value}%'`必须是`value`,具体看源码。 测试函数中,不需要用`%`了 ```java package com.gosuncn; import com.gosuncn.dao.UserMapper; 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 java.io.InputStream; public class MainTest { public static void main(String[] args) throws Exception { InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println(userMapper.findUserByName("poll")); sqlSession.close(); inputStream.close(); } } ```