目录
- myBATis parameterType传入map条件
- mapper类(dao层)
- userMapper.XML配置文件
- 测试类
- 总结
mybatis parameterType传入map条件
mapper类(dao层)
package com.gyf.mapper; import com.gyf.model.User; import com.gyf.vo.UserQueryVO; import Java.util.List; import java.util.Map; public interface UserMapper { public List<User> findUserByMap(Map<String, Object> map); }
userMapper.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.gyf.mapper.UserMapper"> <select id="findUserByMap" parameterType="hashmap" resultType="user"> SELECT u.* FROM user u WHERE username like '%${username}%' and sex = #{sex} </select> </mapper>
测试类
package com.gyf.test; import com.gyf.mapper.UserMapper; import com.gyf.model.编程User; import com.gyf.vo.UserQueryVO; 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.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; public class Demo05 { SqlSession session; @Before public void before() throws IOException { //读取配置文件 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml"); //通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build编程(is); session = sessionFactory.openSession(); } @Aft编程er public void after(){ session.close(); } @Test public void test2() throws IOException { //查找用户 //通过session拿到接口的代理,代理可以直接转为接口 UserMapper userMapper = session.getMapper(UserMapper.class); Map<String,Object> map = new HashMap<Stripythonng, Object>(); map.put("username","张"); map.put("sex","1"); List<User> users = userMapper.findUserByMap(map); System.out.println(users); } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论