当前位置:   article > 正文

Mybatis框架注解开发_mybatis框架的注解有哪些

mybatis框架的注解有哪些

1 MyBatis的常用注解

@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2 MyBatis的增删改查

2.1 使用xml实现curd

public interface UserMapper {
    public void save(User user);

    public void update(User user);

    public void delete(int id);

    public User findById(int id);

    public List<User> findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="lifeilin.mapper.UserMapper">
    <insert id="save" parameterType="lifeilin.domain.User">
        insert into user
        values (#{id}, #{username}, #{password}, #{birthday})
    </insert>

    <update id="update" parameterType="lifeilin.domain.User">
        update user
        set username = #{username},
            password = #{password}
        where id = #{id}
    </update>

    <delete id="delete" parameterType="int">
        delete
        from user
        where id = #{id}
    </delete>

    <select id="findById" parameterType="int" resultType="lifeilin.domain.User">
        select *
        from user
        where id = #{id}
    </select>

    <select id="findAll" resultType="lifeilin.domain.User">
        select *
        from user
    </select>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
import lifeilin.domain.User;
import lifeilin.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MapperTest {
    private UserMapper mapper;

    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        mapper = sqlSession.getMapper(UserMapper.class);
    }

    @Test
    public void testSave() {
        User user = new User();
        user.setId(11);
        user.setUsername("观音");
        user.setPassword("78999");
        user.setBirthday(new Date());
        mapper.save(user);
    }

    @Test
    public void testUpdate() {
        User user = new User();
        user.setId(10);
        user.setUsername("观音");
        user.setPassword("78999");
        user.setBirthday(new Date());
        mapper.update(user);
    }

    @Test
    public void testDelete() {
        mapper.delete(11);
    }

    @Test
    public void testFindById() {
        User u = mapper.findById(9);
        System.out.println(u);
    }

    @Test
    public void testFindAll() {
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

2.2 使用注解实现curd

删除映射关系文件(UserMapper.xml),在核心配置文件(sqlMapConfig.xml)种删除加载映射文件,添加加载映射关系。

<!--    &lt;!&ndash;    加载映射文件&ndash;&gt;-->
<!--    <mappers>-->
<!--        <mapper resource="lifeilin.mapper\UserMapper.xml"></mapper>-->
<!--    </mappers>-->

<!--    加载映射关系 -->
<mappers>
    <!--        指定接口所在的包-->
    <package name="lifeilin.mapper"/>
</mappers>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
public interface UserMapper {
    @Insert("insert into user values (#{id}, #{username}, #{password}, #{birthday})")
    public void save(User user);

    @Update("update user set username = #{username}, password = #{password} where id = #{id}")
    public void update(User user);

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

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

    @Select("select * from user")
    public List<User> findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3 MyBatis的注解实现复杂映射开发

在这里插入图片描述
在这里插入图片描述

3.1 一对一的注解开发

一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户
在这里插入图片描述
在接口中使用注解配置:
  第一种配置方法:

public interface OrderMapper {
    @Select("SELECT *,`order`.id oid FROM `order` ,`user` WHERE `order`.uid = `user`.id;")
    @Results({
            @Result(column = "oid", property = "id"),
            @Result(column = "ordertime", property = "ordertime"),
            @Result(column = "total", property = "total"),
            @Result(column = "uid", property = "user.id"),
            @Result(column = "username", property = "user.username"),
            @Result(column = "password", property = "user.password")
    })
    public List<Order> findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

  第二种配置方法:
在这里插入图片描述

public interface OrderMapper {
    @Select("SELECT * FROM `order`")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "ordertime", property = "ordertime"),
            @Result(column = "total", property = "total"),
            @Result(
                    property = "user",  //要封装的属性名称
                    column = "uid",     //根据哪个字段去查询user表
                    javaType = User.class,      //要封装的实体类型
                    //select属性 代表查询哪个接口的方法获得数据
                    one = @One(select = "lifeilin.mapper.UserMapper.findById")
            )
    })
    public List<Order> findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

测试:
在这里插入图片描述

3.2 一对多的注解开发

一对多查询的需求:查询一个用户,与此同时查询出该用户具有的订单
在这里插入图片描述

@Select("select * from user")
@Results({
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "birthday", column = "birthday"),
        @Result(property = "orders", column = "id",
                javaType = List.class,
                many = @Many(select =
                        "lifeilin.mapper.OrderMapper.findByUid"))
})
List<User> findAllUserAndOrder();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

3.3 多对多的注解开发

多对多查询的需求:查询用户同时查询出该用户的所有角色
在这里插入图片描述
在这里插入图片描述

@Select("select * from user")
@Results({
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "username", column = "username"),
        @Result(property = "password", column = "password"),
        @Result(property = "roleList", column = "id",
                javaType = List.class,
                many = @Many(select =
                        "lifeilin.mapper.RoleMapper.findByUid")
        )
})
List<User> findUserAndRole();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/922032
推荐阅读
相关标签
  

闽ICP备14008679号