自学内容网 自学内容网

【mybatis】注解开发

MyBatis注解开发

        注解方式比较简单,但是实际开发不推荐使用注解,使用配置文件的方式,不需要改源代码。

@Insert:添加

@Update:修改

@Delete:删除

@Select:查询

@Result:实现结果集封装

@Results:可以和@Result一起使用,封装多个结果集

@One:实现一对一和多对一的结果集封装

@Many:实现一对多结果级封装

 一、使用注解完成CRUD

1. SqlMapConfig.xml配置文件

<!--第一种方式:class引入接口,只能引入一个接口-->
        <mapper class="com.qcby.dao.UserAnnoDao"/>
 <mappers>
        <!--第二种方式:针对com.qcby.dao包下的所有的接口-->
        <package name="com.qcby.dao"/>
</mappers>

2. UserDao接口方法和注解的编写

①.查询所有

    @Select("select * from user")
    @Results(id="userMap",value = {
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "address",column = "address")
    })
    List<User> findAll();

②.通过ID查询

    @Select("select * from user where id = #{id}")
    @ResultMap(value = "userMap")
    User findById(int id);

③.添加

@Insert("insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})")
@SelectKey(statement="select last_insert_id()",keyColumn = "id",keyProperty = "id",before =false,resultType =Integer.class)

int insert(User user);

④.修改

@Update("update user set username = #{username},birthday = #{birthday},sex = #{sex},address = #{address} where id = #{id}")
int update(User user);

⑤.删除

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

⑥.查询数量

@Select("select count(*) from user")
int findCount();

⑦.模糊查询

 @Select("select * from user where username like #{username}")
List<User> findByName(String username);

3. UserTest测试方法的编写

①.查询所有

@Test
    public void findAll() throws IOException {
        List<User> users = mapper.findAll();
        for (User user:users) {
            System.out.println(user.toString());
        }
    }

②.通过ID查询

 @Test
    public void findById(){
        User user = mapper.findById(4);
        System.out.println(user.toString());
    }

③.添加

 @Test
    public void insert(){
        User user = new User();
        user.setSex("女");
        user.setUsername("小美");
        user.setBirthday(new Date());
        user.setAddress("保定");
        int insert = mapper.insert(user);
        session.commit();
        System.out.println(insert);
    }

④.修改 

@Test
    public void update(){
        User user = new User();
        user.setId(22);
        user.setSex("女");
        user.setUsername("小美");
        user.setBirthday(new Date());
        user.setAddress("上海");
        int insert = mapper.update(user);
        session.commit();
        System.out.println(insert);
    }

⑤.删除 

 @Test
    public void delete(){
       int delete =  mapper.delete(22);
       session.commit();
        System.out.println(delete);
    }

⑥.查询数量 

 @Test
    public void findCount(){
        int count = mapper.findCount();
        session.commit();
        System.out.println(count);
    }

⑦.模糊查询 

@Test
    public void findByName(){
        List<User> list  = mapper.findByName("%a%");
        for (User user : list) {
            System.out.println(user);
        }
        session.close();
    }

二、多对一的注解查询

1.多对一立即加载查询

①.StudentDao接口的方法编写

@Select(" SELECT  student.*,teacher.Tname FROM student LEFT JOIN teacher  on student.t_id = teacher.id")
@Results(value = {
        @Result(property = "id",column = "id"),
        @Result(property = "Sname",column = "Sname"),
        @Result(property = "sex",column = "sex"),
        @Result(property = "age",column = "age"),
        @Result(property = "t_id",column = "t_id"),
        @Result(property = "teacher.Tname",column = "Tname")
})
List<Student> getStudent();

②.进行测试

@Test
public void getStudent(){
    List<Student> student = mapper.getStudent();
    for (Student student1:student) {
        System.out.println(student1.toString());
    }
}

2.多对一延迟加载查询

①.StudentDao接口的方法编写

@Select("select * from student")
@Results(value = {
        @Result(property = "id",column = "id"),
        @Result(property = "Sname",column = "Sname"),
        @Result(property = "sex",column = "sex"),
        @Result(property = "age",column = "age"),
        @Result(property = "teacher",column = "t_id",one=@One(select = "com.qcby.dao.TeacherDao.getTeacher",fetchType = FetchType.LAZY))
})
List<Student> getStudent();

②.TeacherDao接口的方法编写

@Select("select  * from  teacher where id = #{t_id}")
Teacher getTeacher(Integer id);

③.进行测试

@Test
public void getStudent(){
    List<Student> student = mapper.getStudent();
    for (Student student1:student) {
        System.out.println(student1.toString());
    }
}

三、一对多的注解查询

一对多查询,使用延迟加载的方式查询

①.TeacherDao接口的方法编写

//查询所有延迟加载
@Select("select * from Teacher")
@Results(value = {
        @Result(property = "id",column = "id"),
        @Result(property = "Tname",column = "Tname"),
        @Result(property = "students",column = "id",many =@Many(select = "com.qcby.dao.StudentDao.findByUid",fetchType = FetchType.LAZY))
})
List<Teacher> findAllLazy();

②.StudentDao接口的方法编写

@Select("select * from student where t_id = #{t_id}")
Student findByUid(int uid);

③.进行测试

@Test
public void findAllLazy(){
    List<Teacher> list =  mapper.findAllLazy();
    for (Teacher teacher: list) {
        System.out.println(teacher.toString());
    }
}


原文地址:https://blog.csdn.net/m0_75163045/article/details/144311158

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!