Java框架--Mybaits(二)

3. 高级映射

前期准备:

  • 数据库表
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `sid` int(0) NOT NULL AUTO_INCREMENT,
  `sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `tid` int(0) NULL DEFAULT NULL,
  PRIMARY KEY (`sid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '小红', 1);
INSERT INTO `student` VALUES (2, '小明', 1);
INSERT INTO `student` VALUES (3, '小王', 1);
INSERT INTO `student` VALUES (4, '小赵', 1);

SET FOREIGN_KEY_CHECKS = 1;

====================================================>
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`  (
  `tid` int(0) NOT NULL,
  `tname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`tid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (1, 'A老师');

SET FOREIGN_KEY_CHECKS = 1;
  • 数据库结构图


    image.png
image.png

3.1 多对一

多对一的意思是指多个学生对应一个老师,有可能是一个实体类A对象里面有另外一个实体类B对象,但是数据库查询的时候并没有B对象的属性的情况使用resultMap进行高级映射

示例:查询学生和老师姓名的对应关系

3.1.1 创建实体类

  • 学生对象包含老师对象
package pojo;

public class Student {

    private int id;
    private String name;
    //老师对象存在
    private Teacher teacher;
    //getter&setter省路
}
  • 教师对象
package pojo;

import java.util.List;

public class Teacher {
    private int id;
    private String name;

3.1.2 创建查询SQL语句

可以看到查询语句中对应的Java实体类Student对象中并没有tname的字段,只有teacher对象,这种情况下就需要用resultMap来进行映射

SELECT s.sid,s.sname,t.tname FROM student s INNER JOIN teacher t ON s.tid = t.tid

3.1.3 创建StudentMapper.xml

  • property:在Java程序中的字段
  • column:在数据库中的字段
  • type:返回的类型




    

    
        
        
        
            
            
        
    

3.1.4 创建StudentMapper接口

package dao;

import pojo.Student;
import pojo.Teacher;

import java.util.List;

public interface StudentMapper {
    List getStudent();
}

3.1.5 创建测试类

public class MybatisTest {
    public static void main(String[] args) throws Exception{

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List students = mapper.getStudent();

       for (Student student:students) {
           System.out.println(student);
        }
    }
}

3.2 多对一

和一对多情况类似,就是一个老师对应多个学生,创建教师实体类时包含了一个集合对象

3.2.1 创建实体类对象

  • 包含学生集合的情况
package pojo;

import java.util.List;

public class Teacher {

    private int id;
    private String name;
    private List students;
   // getter&setter
}
  • 学生实体类
package pojo;

public class Student {

    private int id;
    private String name;
    private int tid;
 // getter&setter
}

3.2.2 创建查询语句

老师的实体类中并没有学生对象的属性只有集合,所以需要resultMap对集合进行映射

SELECT t.tid,t.tname,s.sid,s.sname FROM student s INNER JOIN teacher t ON s.tid = t.tid

3.2.3 创建TeacherMapper.xml

  • ofType:要查出集合元素的类型





    

    
        
        

        
            
            
            
        
    


3.2.4 创建TeacherMapper接口

package dao;

import pojo.Student;
import pojo.Teacher;

import java.util.List;

public interface UserMapper {
   List getTeacher();
}

3.2.5 创建测试类

public class MybatisTest {
    public static void main(String[] args) throws Exception{

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List teachers = mapper.getTeacher();
        for (Teacher teacher:teachers) {
            System.out.println(teacher);
        }
  }
}

4. 动态SQL

动态SQL就是根据不同的条件进行SQL的拼接,实现动态SQL需要使用标签跟JSTL类似

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

4.1 数据库环境准备

image.png

4.2 if标签

test=""内部写判断条件即可





  

代码示例

DAO层

public interface BlogMapper {
    List queryBlog1(Map map);
}

测试类

public class MybatisTest {
    public static void main(String[] args) throws Exception{
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
       
        Map map1 = new HashMap<>();
        map1.put("title","Java如此简单");

        List blogs = mapper.queryBlog1(map1);

        for (Blog blog:blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
 }
}
效果展示
image.png

补充

可以把1=1 换成标签 下面代码仅修改了BlogMapper.xml代码

  

4.2 choose标签

  • :相当于case
  • :相当于default

4.3 set标签


    update blog
    
      title = #{title}
      author = #{author}
    
    where id = #{id}
  

4.4 foreach标签

TODO有用到再更新

你可能感兴趣的:(Java框架--Mybaits(二))