Mybatis --03.mybatis中parameterType引數型別
阿新 • • 發佈:2018-12-19
parameterType有三種類型的輸入引數
1、基本資料型別
2、hashmap
3、pojo包裝類
前面已使用過了基本資料型別和pojo型別的引數。下面使用hashmap型別
UserDaoMapper.xml中新增login
<?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"> <!--對映檔案的作用:用來存放sql語句 namespace:名稱空間 --> <mapper namespace="UserDaoMapper"> <!--登入--> <select id="login" resultType="User" parameterType="hashmap" > select * from tb_user where user_name = #{userName} and password = #{password} </select> </mapper>
UserDao介面中新增login方法:
package com.sswblog.dao; import com.sswblog.domain.User; import java.util.List; /** * @ Author :ShaoWei Sun. * @ Date :Created in 0:06 2018/11/5 */ /** * 登入 * @param userName * @param password * @return */ public User login(String userName, String password); }
UserDaoImpl中實現login方法:
package com.sswblog.dao.Impl; import com.sswblog.dao.UserDao; import com.sswblog.domain.User; import org.apache.ibatis.session.SqlSession; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @ Author :ShaoWei Sun. * @ Date :Created in 0:06 2018/11/5 */ public class UserDaoImpl implements UserDao { //dao層增刪除改查需要SqlSession private SqlSession sqlSession = null; //構造方法 public UserDaoImpl(SqlSession sqlSession) { this.sqlSession = sqlSession; } @Override public User login(String userName, String password) { Map<String,Object> map = new HashMap<>(); map.put("userName",userName); map.put("password",password); User user = this.sqlSession.selectOne("UserDaoMapper.login", map); return user; } }
UserDaoTest中新增testLogin方法
package com.sswblog.test;
import com.sswblog.dao.Impl.UserDaoImpl;
import com.sswblog.dao.UserDao;
import com.sswblog.domain.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.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
/**
* @ Author :ShaoWei Sun.
* @ Date :Created in 15:05 2018/11/5
*/
public class UserDaoImplTest {
private UserDao userDao = null;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
userDao = new UserDaoImpl(sqlSession);
}
@Test
public void login(){
User user = userDao.login("小王", "111111");
if (user == null){
System.out.println("沒有登入");
}else {
System.out.println("登入成功");
}
System.out.println(user);
}
}