使用MyBatis查询user表中所有的女性用户并输出

使用MyBatis查询user表中所有的女性用户并输出_第1张图片
使用MyBatis查询user表中所有的女性用户并输出_第2张图片

sex:0代表女性,1代表男性

User

package entity;

import java.sql.Date;

public class User {
    private String username;
    private String password;
    private Integer age;
    private int sex;
    private Date birthdate;
    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;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    public Date getBirthdate() {
        return birthdate;
    }
    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + ", age=" + age + ", sex=" + sex
                + ", birthdate=" + birthdate + "]";
    }

}

UserMapper.xml




        
        

database.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms
username=root
password=lpc123456

mybatis-config.xml




 

 
 
   
   
    
        
        
            
            
            
            
        
    
 
   
   

UserMapperTest

package test;
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;
import org.junit.Test;

import entity.User;

public class UserMapperTest {
    @Test
    public void testFindUserBySex() throws IOException{
        //1.加载mybatis核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //2.获取SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //3.得到执行sql的对象SqlSession 
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.执行sql语句
        List list = sqlSession.selectList("UserMapper.findUsrBySex",0);
        //5.处理结果
        
        for (User user: list) {
        System.out.println(user.toString());
        }
        //6.关闭数据库会�?
        sqlSession.close();

    }
}

你可能感兴趣的:(java,eclipse)