mybatis一对一

我们以前的理解是,部门和员工的关系是一对多,员工和部门的关系是多对一;

但是我们拿出单个员工来说,一个员工只属于一个部门,因此在MyBatis中,多对一的关系简化成了一对

实际操作

班级

package com.q.bena;

public class ClassInfo {
    private Integer cid;
    private String cname;
    private String cinfo;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCinfo() {
        return cinfo;
    }

    public void setCinfo(String cinfo) {
        this.cinfo = cinfo;
    }

    @Override
    public String toString() {
        return "ClassInfo{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", cinfo='" + cinfo + '\'' +
                '}';
    }
}

学生

package com.q.bena;

public class Student {
    private Integer sid;
    private String sname;
    private String sex;
    private String phone;
    private String address;
    private ClassInfo classInfo;

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public ClassInfo getClassInfo() {
        return classInfo;
    }

    public void setClassInfo(ClassInfo classInfo) {
        this.classInfo = classInfo;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", classInfo=" + classInfo +
                '}';
    }
}

接口

package com.q.dao;

import com.q.bena.ClassInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ClassInfoDao {
    @Select("select * from classInfo")
    List selectAll();

    @Select("select * from classInfo where cid=#{cid}")
    ClassInfo selectByCid(int cid);

    @Insert("insert into classInfo(cname,cinfo) values(#{cname},#{cinfo})")
    int insert(ClassInfo classInfo);

    @Delete("delete from classInfo where cid=#{cid}")
    int delete(int cid);

}
package com.q.dao;

import com.q.bena.Student;

import java.util.List;

public interface StudentDao {
    List selectAll();

    List seach(Student student);

    int update(Student student);

    Student selectById(int id);
}

配置文件




    
        
        
        
        
        
         
    
    
    
    
    
        update student
        
             sname=#{sname}, 
             sex=#{sex}, 
             phone=#{phone}, 
             address=#{address}, 
        
        where sid=#{sid}
    

测试

package com.q.test;

import com.q.bena.Student;
import com.q.dao.StudentDao;
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.After;
import org.junit.Before;
import org.junit.Test;

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

public class StudentTest {
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    StudentDao studentDao=null;

    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        studentDao=sqlSession.getMapper(StudentDao.class);
    }

    @Test
    public void testSelectAll(){
        List studentList = studentDao.selectAll();
        for (Student student : studentList) {
            System.out.println(student);
        }
    }

    @Test
    public void selNA(){
        Student student=new Student();
        student.setSname("");
        student.setSex("");
        student.setPhone("136");
        student.setAddress("");
        List studentList=studentDao.seach(student);
        for(Student student1:studentList){
            System.out.println(student1);
        }
    }

    @Test
    public void testUpdate(){
        Student stu = studentDao.selectById(5);
        stu.setSname("99");
        stu.setSex("");
        stu.setPhone("");
        stu.setAddress("");
        studentDao.update(stu);
        Student newStu = studentDao.selectById(5);
        System.out.println(newStu);
    }

    @After
    public void colse() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}

你可能感兴趣的:(mybatis)