mybatis xml多表查询,子查询,连接查询,动态sql

项目结构

mybatis xml多表查询,子查询,连接查询,动态sql_第1张图片

数据库表

student_type 表

mybatis xml多表查询,子查询,连接查询,动态sql_第2张图片

student 表

mybatis xml多表查询,子查询,连接查询,动态sql_第3张图片

依赖


            mysql
            mysql-connector-java
            8.0.30
        

        
            org.mybatis
            mybatis
            3.5.5
        
        
            junit
            junit
            4.12
            test
        

实体类

Student 类

一个学生只有一个年级

package com.tmg.domain;

public class Student {
    private int id;
    private String name;
    private int age;
    private String email;
    private Integer typeId;
    private Type type;

    public Integer getTypeId() {
        return typeId;
    }

    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Student(int id, String name, int age, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }

    public Student() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                ", typeId=" + typeId +
//                ", type=" + type +
                '}';
    }
}

Type 类

一个年级有多个学生,所以用 list

package com.tmg.domain;

import java.util.List;

public class Type {
    private Integer id;
    private String name;
    private List students;

    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 List getStudents() {
        return students;
    }

    public void setStudents(List students) {
        this.students = students;
    }

    @Override
    public String toString() {
        return "Type{" +
                "id=" + id +
                ", name='" + name + '\'' +
//                ", students=" + students +
                '}';
    }
}

StudentDao

package com.tmg.dao;

import com.tmg.domain.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentDao {


    //多个参数的配置
    void insertEmp( @Param("stuName")  String name,
                   @Param("stuAge") int age, @Param("stuEmail")  String email);

    List selectByStudent(Student student);

    void update(Student employee);

    void update2(Student employee);

    List selectByIds(@Param("ids") int []id);
//    List selectById(int id);

    List selectByTypeId(int id);
    List selectAll();
}

TypeDao

package com.tmg.dao;

import com.tmg.domain.Type;

import java.util.List;

public interface TypeDao {

    List selectAll();

    Type  selectById(Integer id);
}

mybatis-config.xml配置数据源,日志等





    
        
        
    

    
        
            
            
                
                
                
                
            
        
    

    
        
        
    

TypeDao.xml

下列代码中:
1 resultMap 里面property对应实体类属性,column对应数据库字段名
2 主键用 id 标签 其他用result
3 关联查询(子查询和连接查询) 连接查询查一次
4 一个年级多个学生,所以用collection 如果一对一用association






    
        
        










        

        

    
    

    



StudentDao.xml

动态sql不理解可看以下博客:
https://blog.csdn.net/weixin_57689217/article/details/135707991?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22135707991%22%2C%22source%22%3A%22weixin_57689217%22%7D







    
    
    
    
        insert into student( id,name,age,email) values (#{id},#{name},#{age},#{email});
    



    

    
        
        
        
        
        
        
        






        

        
    




    select * from student

    

    
        update student
        
            
                stu_age=#{age},
            
            
                stu_email=#{email},
            
            
                stu_name=#{name},
            
        
        where stu_id=#{id};
    
    
        update student
        
            
                stu_age=#{age},
            
            
                stu_email=#{email},
            
            
                stu_name=#{name},
            
        
        where stu_id=#{id};
    

    

    




TypeDaoText 测试类

package com.tmg.dao;

import com.tmg.domain.Type;
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 java.io.IOException;
import java.util.List;

public class TypeDaoText {
    @Test
    public void testselectAll() throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        TypeDao mapper = sqlSession.getMapper(TypeDao.class);
        List typeList = mapper.selectAll();
        for (Type type : typeList) {
            System.out.println(type);
        }
    }


    @Test
    public void testselectById() throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        TypeDao mapper = sqlSession.getMapper(TypeDao.class);
        Type type = mapper.selectById(1);
        System.out.println(type);
    }
}

StudentDaoText 测试类

package com.tmg.dao;

import com.tmg.domain.Student;
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 java.io.IOException;
import java.util.List;

public class StudentDaoText {

    @Test
    public void testinsertEmp() throws IOException {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = factory.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        mapper.insertEmp("tmg",18,"[email protected]");
        sqlSession.commit();
    }

    @Test
    public void testselectByStudent() throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
//        student.setName("z");
//        student.setAge(18);
//        student.setEmail("[email protected]");
        List students = mapper.selectByStudent(student);
        for (Student student1 : students){
            System.out.println(student1);
        }
    }
    @Test
    public void testupdate() throws IOException {
        //创建会话工厂构建器
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        //创建会话
        SqlSession sqlSession = build.openSession();
        //获得Mapper对象
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
//        student.setName();
        student.setId(1);
        student.setAge(22);
        mapper.update(student);
        sqlSession.commit();
    }
    @Test
    public void testupdate2() throws IOException {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = new Student();
        student.setId(1);
        student.setAge(44);
        mapper.update2(student);
        sqlSession.commit();
    }

    @Test
    public void testselectByIds() throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        int []a={1};
        List students = mapper.selectByIds(a);
        for (Student student:students){
            System.out.println(student);
            System.out.println(student.getType());
        }
    }
    @Test
    public void testselectAll() throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List students = mapper.selectAll();
        for(Student student : students){
            System.out.println(student);
            System.out.println(student.getType());
        }

    }
}

你可能感兴趣的:(xml配置,mybatis,java,mybatis,xml,sql,动态sql,collection,association)