MyBatis学习笔记04

注解增删改查

1.在工具类创建的时候自动提交事务

java 复制代码
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {//使用MyBatis第一步获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }//SqlSession完全包含了面向数据库执行SQL命令事务所有方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
}

2.接口

java 复制代码
 @Select("select * from user where id = #{id}")
    User getUserById(@Param("id")int id);
    @Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
    int addUser(User user);
    @Update("update user set name=#{name},pwd=#{pwd} where id = #{id}")
    int updateUser(User user);
    @Delete("delete from user where id = #{id}")
    int deleteUser(@Param("id") int id);

3.测试

java 复制代码
@Test
    public void testDemo1(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(7);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void testDemo2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(new User(8,"sun","123456"));
        sqlSession.close();
    }
    @Test
    public void testDemo3(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(8,"tom2","123456"));
        sqlSession.close();
    }
    @Test
    public void testDemo4(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(8);
        sqlSession.close();
    }

关于@Param()注解

1.基本类型的参数或者String类型,需要加上

2.引用类型不需要加

3.如果只有一个基本参数类型,可以忽略

4.在SQL中引用的是@Param("id")中设定的属性名

lombok使用步骤

1.在idea中安装插件

2.在项目中导入jar包

xml 复制代码
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.26</version>
    <scope>provided</scope>
</dependency>

3.在实体类中加注解

java 复制代码
package pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private  String pwd;

}

一对多

xml 复制代码
<mapper namespace="dao.TeacherMapper">
<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from mybatis.student s,mybatis.teacher t
    where s.tid=t.id and t.id=#{tid}
</select>
    <resultMap id="TeacherStudent" 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>

</mapper>

测试

java 复制代码
public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);

        sqlSession.close();
    }

多对一

xml 复制代码
<select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname
        from mybatis.student s,mybatis.teacher t
        where s.tid=t.id;
    </select>
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>

测试

java 复制代码
public void testStudent2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> student = mapper.getStudent2();
        for (Student student1 : student) {
            System.out.println(student1);
        }
        sqlSession.close();
    }
相关推荐
三品吉他手会点灯3 小时前
C语言学习笔记 - 50.流程控制4 - 流程控制为什么非常非常重要
c语言·开发语言·笔记·学习
chushiyunen5 小时前
langchain4j笔记、tools
笔记·python·flask
sunfdf6 小时前
知识学习场景下的智能应用实践大纲
学习
MartinYeung57 小时前
[论文学习]重新思考大型语言模型忘却目标:梯度视角与超越
人工智能·学习·语言模型
影视飓风TIM7 小时前
数据结构 | 链表超全笔记(单链表+双链表+高频算法题)
数据结构·笔记·链表
二哈赛车手7 小时前
新人笔记---最终版智能体图片分析完整方案,包括一些总结于经验,以及各种优化点讲解
java·笔记·spring·ai·springboot
_李小白7 小时前
【智能驾驶:视觉感知后处理 阅读笔记】Day4: 相机成像模型与畸变
笔记·数码相机
十月的皮皮7 小时前
C语言学习笔记20260615-有序升序序列合并
c语言·笔记·学习
JAVA面经实录9177 小时前
前端系统化学习计划表(含完整知识思维导图)
前端·学习
worilb8 小时前
Spring Cloud 学习与实践(9):Gateway + JWT 统一鉴权
学习·spring cloud·gateway