Mybatis参数绑定

一、单个参数绑定

1、在java.com.by.dao文件夹下新建UserDao接口,在接口中定义getUserById()方法。

public interface UserDao {
    User getUserById(Integer id);
}

2、在java.com.by.dao文件夹下新建UserDao.xml文件。













3、编写测试类

public class MyBatisTest {
    @Test
    public void getUserById() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sessionFactory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        User user = userDao.getUserById(43);
        System.out.println(user);

        //关流
        sqlSession.close();
        inputStream.close();
    }
}

二、多个参数绑定

多个参数绑定有4中方法:

     1、序号传递多个参数
        WHERE id=#{arg0} AND username=#{arg1}
        WHERE id=#{param1} AND username=#{param2}
      2、注解传递多个参数---【推荐】
        User getUser2(@Param("id") Integer id, @Param("username") String username);
        select * from user where id=#{id} and username=#{username}
      3、pojo传递多个参数---【推荐】
        User getUser3(User user);
        select * from user where id=#{id} and username=#{username}
      4、map传递多个参数
        User getUser3(Map user);
        select * from user where id=#{id} and username=#{username}                                                     多个参数绑定代码:

UserDao.xml文件




    
    


    
    

    
    

    
    

    
    




   

MyBatisTestjava文件
package com.by.test;

import com.by.dao.UserDao;
import com.by.pogo.User;
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 MyBatisTest {
    private SqlSession sqlSession;
    private UserDao userDao;
    private InputStream inputStream;

  //使用@Before注解,在运行类里面方法时先运行类里面的内容
    @Before
    public void start() throws IOException {
        String resource = "mybatis-config.xml";
        inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        userDao = sqlSession.getMapper(UserDao.class);
    }

  //传递单个参数
    @Test
    public void getUserById() throws IOException {

        User user = userDao.getUserById(43);
        System.out.println(user);

    }


    //映射传递多个参数
    @Test
    public void getUser() throws IOException {

        User user = userDao.getUser(42, "宋远桥");
        System.out.println(user);
    }

    //注解传递多个参数
    @Test
    public void getUser1() throws IOException {

        User user = userDao.getUser1(42, "宋远桥");
        System.out.println(user);
    }


    //pojo传递多个参数
    @Test
    public void getUser2() throws IOException {
        User user1 = new User();
        user1.setId(42);
        user1.setUsername("宋远桥");
        User user = userDao.getUser2(user1);
        System.out.println(user);
    }


    //map传递多个参数
    @Test
    public void getUser3() throws IOException {
        Map map = new HashMap();
        map.put("id", 42);
        map.put("username", "宋远桥");
        User user = userDao.getUser3(map);
        User user3 = userDao.getUser3(map);
        System.out.println(user3);
    }

    //使用@After注解,运行完类中的方法后自动运行
    @After
    public void end() throws IOException {
        //关流
        sqlSession.close();
        inputStream.close();
    }


}

    UserDao接口

package com.by.dao;

import com.by.pogo.User;
import org.apache.ibatis.annotations.Param;

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

public interface UserDao {
    //传递单个参数
    User getUserById(Integer id);
    //映射传递多个参数
    User getUser(Integer id, String username);
    //注解传递多个参数
    User getUser1(@Param("id")Integer id, @Param("name")String username);
    //pojo传递多个参数
    User getUser2(User user);
    //map传递多个参数
    User getUser3(Map map);
}

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

你可能感兴趣的:(mybatis)