mybatis05(配置文件实现一对多的查询)

 

注意:一对多时,数据库外键设置在一对多的 多 表中,这样设计数据库比较简洁(本例为一个班级对应多个学生)

 

1.class的javabean(此时设置变量students来表示多个学生)

package com.bean;

import java.util.List;

public class Classes {

 private Integer id;
 private Integer code;
 private List students;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public Integer getCode() {
  return code;
 }
 public void setCode(Integer code) {
  this.code = code;
 }
 public List getStudents() {
  return students;
 }
 public void setStudents(List students) {
  this.students = students;
 }
 @Override
 public String toString() {
  return "Classes [id=" + id + ", code=" + code +"]";
 }
}

2.

package com.bean;

public class Student {

 private Integer id;
 private String name;
 private Integer age;
 private String sex;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 @Override
 public String toString() {
  return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
 }
}

 

 

 

3.log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.mapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

 

4.jdbc.properties(对一些变量的设置)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis
username=root
password=123456

 

 

 

5.mybatis配置文件



   
    
 
 
  
  
  
  
  
   
  
  
  
   
   
    
    
    
    
     
     
     
     
    
   
  
  
  
  
   
   
   
  
 

 

 

 

6.数据库操作的配置文件






 
 
  
  
  
 
 
 
 
 
 
  
  
  
   
   
   
   
  
 
 
 
 
 
 
  
  
  
   
   
   
   
  
 
 


7.查询

package com.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.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.junit.Test;

import com.bean.Classes;
import com.bean.Student;

public class test {

 @Test
 public void selectById() throws IOException
 {
  InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
  SqlSession session = sessionFactory.openSession();
  Classes classes=session.selectOne("com.mapper.UserMapper.selectClass",2);
  List list=classes.getStudents();
  System.out.println(classes);
  list.forEach(student->System.out.println(student));
  /*for(Student student:list)
  {
   System.out.println(student);
  }*/
 }
 @Test
 public void selectById2() throws IOException
 {
  InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
  SqlSession session = sessionFactory.openSession();
  Classes classes=session.selectOne("com.mapper.UserMapper.selectClass2",2);
  List list=classes.getStudents();
  System.out.println(classes);
  list.forEach(student->System.out.println(student));//这句话等同于下面注释的for循环
  /*for(Student student:list)
  {
   System.out.println(student);
  }*/
 }
 @Test
 public void select() throws IOException
 {
  InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
  SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
  SqlSession session = sessionFactory.openSession();
  Classes classes=session.selectOne("com.mapper.UserMapper.select", 2);
  List list=classes.getStudents();
  System.out.println(classes);
  list.forEach(student->System.out.println(student));
 }
}

 

你可能感兴趣的:(面包)