渚漪Day28——SSM【Mybatis05】

8、使用注解

底层主要应用反射UserMapper.class

8.1、注解分析

@Select("select * from mybatis.user")
List<User> getUsers();
@Test
//查询全部
public void test1(){
    try(SqlSession session = MybatisUtils.getSqlSession()) {
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

等价于

interface+xml

List<User> getUsers();
<select id="getUsers" resultType="com.ijuy.pojo.User">
    select * from mybatis.user;
</select>

8.2、注解完成增删改查

  • CRUD

  • @Param()的使用

    • 基本类型的参数或者String类型需要加上
    • 引用类型不需要加
    • 如果只有一个基本类型的话可以忽略,但是建议加上
    • 在SQL中引用的就是@parm中设定的属性名
  • 自动事务提交

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
    

9、Lombok

  1. idea是真的加载不出来这个插件(可能被抛弃了?)

  2. lombok简化了java操作,但是有缺点,改变人们的编程思维。

    综上不用

10、多对一处理

多对一复杂环境的搭建

建立师生SQL语句

CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO teacher(`id`, `name`) VALUES (1, '秦老师'); 

CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');

测试环境搭建

按照查询嵌套处理

<select id="getStudent" resultMap="TeacherStudent">
    select* from mybatis.student
</select>

<resultMap id="TeacherStudent" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!--复杂的属性,单独处理
        对象:association
        集合:collection-->
    <association property="teacher" column="tid" javaType="teacher" select="getTeacher"/>
</resultMap>

<select id="getTeacher" resultType="teacher">
    select * from mybatis.teacher where id=#{id}
</select>

按照结果嵌套查询

<select id="getStudent2" resultMap="TeacherStudent2">
        select s.id sId,s.name sName,t.id tId,t.name tName
        from mybatis.student as s,mybatis.teacher as t
#         where s.tid=t.id
    </select>

    <resultMap id="TeacherStudent2" type="student">
        <result property="id" column="sId"/>
        <result property="name" column="sName"/>
        <association property="teacher" javaType="teacher">
            <result property="id" column="tId"/>
            <result property="name" column="tName"/>
        </association>
    </resultMap>

结论:狂神查出的结果

teacher id = 0 是应为查询当中没有在association 标签中查询对象中的id属性

11、一对多处理

原理和多对一类似

代码

<select id="getTeacher1" resultMap="TeacherStudent1">
    select s.id sId,s.name sName,t.name tName,t.id tId
    from mybatis.teacher as t,mybatis.student as s
    where s.tid = #{tid}
</select>
<resultMap id="TeacherStudent1" type="teacher">
    <result property="id" column="tId"/>
    <result property="name" column="tName"/>
    <collection property="students" ofType="student">
        <result property="id" column="sId"/>
        <result property="name" column="sName"/>
        <result property="tid" column="tId"/>
    </collection>
</resultMap>






<select id="getTeacher2" resultMap="TeacherStudent2">
    select id,name from mybatis.teacher where id = #{tid}
</select>
<select id="getStudentByTeacherId" resultType="student">
    select s.id,s.name,s.tid from mybatis.student as s where tid = #{tid}
</select>
<resultMap id="TeacherStudent2" type="teacher">
    <result column="id" property="id"/>
    <collection property="students" javaType="ArrayList" ofType="student" select="getStudentByTeacherId" column="id"/>
</resultMap>

遇到的问题

此处加了“#” 报错

删除第九行却不报错

可能是两个#在转义的过程中形成了 ‘/注释’ where s.tid = ‘/注释’ {tid}这样的语句。导致去掉注释内容,还剩 {tid}。

错误无法得到验证,人为去掉注释内容,idea自动检测语法格式错误。

posted @ 2020-05-17 22:57  渚漪  阅读(113)  评论(0编辑  收藏  举报