MyBatis三个查询方法_selectList_selectOne_selectMap

mybatis-cfg.xml的配置:



  
  
  
  
  	
  
  	
 		
 			
 			
 				
 				
 				
 				
 				
 			
 		
  	
  	
  		
  	
  

db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.5.129:3306/bjsxt
jdbc.username=root
jdbc.password=root

log4j.properties:

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=info, CONSOLE
# log4j.rootCategory=DEBUG, CONSOLE, LOGFILE

# 单独设置SQL语句的输出级别为DEBUG级别
# 方法级别
# log4j.logger.com.bjsxt.mapper.UserMapper.selAll=DEBUG
# 类级别
# log4j.logger.com.bjsxt.mapper.UserMapper=DEBUG
# 包级别
log4j.logger.com.bjsxt.mapper=DEBUG

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%l%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:/test.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n

User实体类

package com.bjsxt.pojo;

import java.io.Serializable;

public class User implements Serializable{
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id != other.id)
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
	public User() {
		super();
	}
	
}

 

selectList_selectOne_selectMap:



  
  	
  	
  	
  	
  
package com.bjsxt.TestSelect;

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

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.Test;

import com.bjsxt.pojo.User;

public class TestSelect {
	
	@Test
	public void testSelectList() throws IOException {
		InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		
		List list = session.selectList("com.bjsxt.mapper.UserMapper.sellAll");
		
		for (User user : list) {
			System.out.println(user);
		}
		session.close();
	}
	
	@Test
	public void testSelectOne() throws IOException {
		InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		
		User u = session.selectOne("com.bjsxt.mapper.UserMapper.sellOne");
		System.out.println(u);
		session.close();
	}
	
	@Test
	public void testSelectMap() throws IOException {
		InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		
		Map map = session.selectMap("com.bjsxt.mapper.UserMapper.sellAll", "id");
		Set ks = map.keySet();
		for (Integer k : ks) {
			System.out.println(map.get(k));
		}
		session.close();
	}
	
}

MyBatis三个查询方法_selectList_selectOne_selectMap_第1张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据库学习)