创建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.mapper.UserMapper">
<!-- 根据用户名、密码查询用户 -->
<select id="getUser" parameterType="map" resultType="com.po.User">
select * from t_user where username=#{username} and password=#{password}
</select>
</mapper>
创建UserDao
package com.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import com.factory.SessionFactory;
import com.po.User;
public class UserDao {
private SqlSession session=SessionFactory.getSqlSession();
private User user;
private List<User> users;
public SqlSession getSession() {
return session;
}
//根据用户名、密码查询用户
public User getUser(String username,String password) {
Map<String, Object> parameterMap=new HashMap<String, Object>();
parameterMap.put("username", username);
parameterMap.put("password", password);
user=session.selectOne("com.mapper.UserMapper.getUser", parameterMap);
return user;
}
}
创建Test
package com.test;
import com.dao.UserDao;
import com.po.User;
public class Test {
public static void main(String[] args) {
UserDao userDao=new UserDao();
User user=userDao.getUser("admin", "123");
System.out.println(user);
}
}