MyBatis-12 参数处理


使用Map

映射文件

<?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.cd.StudentMapper">

    <select id="selectStudentByNameAndAge" resultType="Student" parameterType="map">
        select * from stu_tbl
        where name = #{name} and age = #{age}
    </select>

</mapper>

接口

package com.cd;

import java.util.List;
import java.util.Map;

public interface StudentMapper {
    public List<Student> selectStudentByNameAndAge(Map<String, String> m);
}

测试

package com.cd;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
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;

public class Test {

    public static void main(String[] args) {
        String resource = "com/cd/mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = (InputStream) Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            StudentMapper mapper = session.getMapper(StudentMapper.class);

            Map<String,String> map = new HashMap<>();
            map.put("name", "rose");
            map.put("age","18");

            List<Student> stu_list = mapper.selectStudentByNameAndAge(map);
            System.out.println(stu_list);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

使用JavaBean

映射文件

<?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.cd.StudentMapper">

    <select id="selectStudentByNameAndAge" resultType="Student" parameterType="StudentBean">
        select * from stu_tbl
        where name = #{name} and age = #{age}
    </select>

</mapper>

JavaBean

package com.cd;

public class StudentBean {

    private String name;
    private int age;

    public StudentBean() {
        super();
    }

    public StudentBean(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return this.name+","+this.age;
    }
}

接口

package com.cd;

import java.util.List;

public interface StudentMapper {
    public List<Student> selectStudentByNameAndAge(StudentBean bean);
}

映射文件

<?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.cd.StudentMapper">

    <select id="selectStudentByNameAndAge" resultType="Student" parameterType="StudentBean">
        select * from stu_tbl
        where name = #{name} and age = #{age}
    </select>
</mapper>

测试

package com.cd;

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;

public class Test {

    public static void main(String[] args) {
        String resource = "com/cd/mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = (InputStream) Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            StudentMapper mapper = session.getMapper(StudentMapper.class);

            StudentBean bean = new StudentBean();
            bean.setName("rose");
            bean.setAge(18);

            List<Student> stu_list = mapper.selectStudentByNameAndAge(bean);
            System.out.println(stu_list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用@Param

映射文件

<?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.cd.StudentMapper">

    <select id="selectStudentByNameAndAge" resultType="Student">
        select * from stu_tbl
        where name = #{myname} and age = #{myage}
    </select>

</mapper>

接口

package com.cd;

import java.util.List;

import org.apache.ibatis.annotations.Param;

public interface StudentMapper {
    public List<Student> selectStudentByNameAndAge(@Param("myname") String name,@Param("myage") int age);
}

测试

package com.cd;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {

    public static void main(String[] args) {
        String resource = "com/cd/mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = (InputStream) Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            StudentMapper mapper = session.getMapper(StudentMapper.class);

            StudentBean bean = new StudentBean();
            bean.setName("rose");
            bean.setAge(18);

            List<Student> stu_list = mapper.selectStudentByNameAndAge("rose", 18);
            System.out.println(stu_list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


原文链接:http://codingdict.com/