笔记代码

分割线

面向接口编程

  • 主要目的就是解耦,分层开发中,上层业务不用管具体的实现.

  • 使得 规范实现 两部分分离,接口作为项目骨架.


注解开发

简单上手

  • 接口上直接注解 SQL 语句

    public interface UserMapper {
    @Select("select * from mybatis.user")
    List<User> getUsers();
    }
  • 绑定接口

    <mappers>
    <!--之前是用xml绑定的-->
    <!--<mapper resource="Mappers/UserMapper.xml"/>-->

    <!--这里换成绑定接口-->
    <mapper class="dao.UserMapper"/>
    </mappers>
  • 实现原理: 通过反射机制获取接口中的信息,然后用动态代理实现业务.

    代理模式.drawio
  • 调试

    20210522000831

分割线

原理分析

mybatis 的执行流程

MyBatis流程.drawio

CRUD

  • UserMapper

    package dao;

    import org.apache.ibatis.annotations.*;
    import pojo.User;

    import java.util.List;
    import java.util.Map;

    public interface UserMapper {

    @Select("select * from mybatis.user")
    List<User> getUsers();

    /*
    基本类型的参数或者String类型,需要加上
    引用类型不需要加
    如果只有一个基本类型的话,可以忽略,但是建议都加上
    我们在SQL中引用的就是我们这里的@Param()中设定的属性名
    重要: 这里方法不能重载! 两个同名方法会使MyBatis运行报错(开发时JDK不会报错)
    */
    @Select("select * from mybatis.user where id = #{id}")
    User getUserById(int id);

    // 多个参数的情况下,必须加上@Param注解
    @Select("select * from mybatis.user where id = #{id} and name = #{name}")
    User getUserByIdAndName(@Param("id") int id, @Param("name") String name);

    @Insert("insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd})")
    int addUser(User user);

    //注意SQL格式,set 和 where顺序不能颠倒
    @Update("update mybatis.user set name = #{name},pwd = #{pwd} where id = #{id}")
    int updateUser(User user);

    @Delete("delete from mybatis.user where id = #{id}")
    int deleteUserById(int id);
    }

  • test

    package dao;

    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    import pojo.User;
    import utils.MyBatisUtils;

    import java.util.List;

    public class UserMapperTest {

    @Test
    public void getUsers() {
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = userMapper.getUsers();
    for (User user : userList) {
    System.out.println(user);
    }
    sqlSession.close();
    }

    @Test
    public void testGetUserById() {
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User user = userMapper.getUserById(1);
    System.out.println(user);
    sqlSession.close();
    }

    @Test
    public void getUserById() {
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User user = userMapper.getUserByIdAndName(1, "张三");
    System.out.println(user);
    sqlSession.close();
    }

    @Test
    public void addUser() {
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    int i = userMapper.addUser(new User(7, "七七", "45648"));
    if (i > 0)
    sqlSession.commit();
    sqlSession.close();
    }

    @Test
    public void updateUser() {
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    int i = userMapper.updateUser(new User(7, "七七", "12345678"));
    if (i > 0)
    sqlSession.commit();
    sqlSession.close();
    }

    @Test
    public void deleteUserById() {
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    if (userMapper.deleteUserById(7) > 0)
    sqlSession.commit();
    sqlSession.close();
    }
    }

分割线

对应关系

  • SQL初始化:

多对一