九点钟☆方向

记录成长点滴

欢迎来到我的个人博客~


Mybatis笔记(十一) Mybatis注解开发

目录

mybatis 的常用注解说明

@Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与@Result 一起使用,封装多个结果集 @ResultMap:实现引用@Results 定义的封装 @One:实现一对一结果集封装 @Many:实现一对多结果集封装 @SelectProvider: 实现动态 SQL 映射 @CacheNamespace:实现注解二级缓存的使用

使用 Mybatis 注解实现基本 CRUD

  1. 引入依赖

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
       
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.16</version>
        </dependency>
    </dependencies>
    
  2. 编写实体类

    public class User implements Serializable {
        private Integer userId;
        private String userName;
        private Date userBirthday;
        private String userSex;
        private String userAddress;
        //生成set/get
    }
    
  3. 使用注解方式开发持久层接口

    public interface IUserMapper {
           
        /**
        * 查询所有用户
        * 当数据库字段名和属性名不一致时,可使用@Results封装
        */
        @Select("select * from user")
        @Results(id="userMap",
        value= {
            @Result(id=true,column="id",property="userId"),
            @Result(column="username",property="userName"),
            @Result(column="sex",property="userSex"),
            @Result(column="address",property="userAddress"),
            @Result(column="birthday",property="userBirthday")
        })
        List<User> findAll();
           
        /**
        * 根据 id 查询一个用户
        * @param userId
        * @return
        */
        @Select("select * from user where id = #{uid} ")
        //@ResultMap(value={"userMap"}) 如果只有value一个属性则value可省略,如果数组中只有一个元素则{}可省略,所以可以写成这样
        @ResultMap("userMap")
        User findById(Integer userId);
           
        /**
        * 保存操作
        * @param user
        * @return
        */
        @Insert("insert into user(username,sex,birthday,address) values(#{username},#{sex},#{birthday},#{address})")
        @SelectKey(keyColumn="id",keyProperty="id",resultType=Integer.class,
                   before =false,statement = { "select last_insert_id()" })
        int saveUser(User user);
        /**
        * 更新操作
        * @param user
        * @return
        */
        @Update("update user set
        username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id
        =#{id} ")
        int updateUser(User user);
        /**
        * 删除用户
        * @param userId
        * @return
        */
        @Delete("delete from user where id = #{uid} ")
        int deleteUser(Integer userId);
        /**
        * 查询使用聚合函数
        * @return
        */
        @Select("select count(*) from user ")
        int findTotal();
      	/**
        * 模糊查询
        * @param name
        * @return
        */
        @Select("select * from user where username like #{username} ") //使用时需要加%
        //@Select("select * from user where username like '%${value}%'") //使用时不需要加%
        List<User> findByName(String name);
    }
    通过注解方式我们就不需要再去编写 IUserMapper.xml 映射文件了
    
  4. JDBC的配置文件 jdbcConfig.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mybatis
    jdbc.username=root
    jdbc.password=root
    
  5. 编写 SqlMapConfig 配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!-- 配置 properties 文件的位置 -->
        <properties resource="jdbcConfig.properties"></properties>
           
        <!-- 配置别名的注册 -->
        <typeAliases>
        	<package name="com.mg.entity"/>
        </typeAliases>
           
        <!-- 配置环境 -->
        <environments default="mysql">
            <!-- 配置 mysql 的环境 -->
            <environment id="mysql">
                <!-- 配置事务的类型是 JDBC -->
                <transactionManager type="JDBC"></transactionManager>
                <!-- 配置数据源 -->
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
        </environments>
        <!-- 配置映射信息 -->
        <mappers>
            <!-- 配置 dao 接口的位置,它有两种方式
            第一种:使用 mapper 标签配置 class 属性
            第二种:使用 package 标签,直接指定 dao 接口所在的包
            -->
        	<package name="com.mg.dao"/>
        </mappers>
    </configuration>
    

使用注解实现复杂关系映射开发

复杂关系映射的注解说明

1. @Results 注解代替的是标签<resultMap>
该注解中可以使用单个@Result 注解也可以使用@Result 集合
@Results{@Result(), @Result() } @Results@Result())

2. @Result 注解代替了 <id>标签和<result>标签
@Result  属性介绍
id 是否是主键字段
column 数据库的列名
property 需要装配的属性名
one 需要使用的@One 注解@Resultone=@One)()))
many 需要使用的@Many 注解@Resultmany=@many)()))

3. @One 注解一对一
代替了<assocation>标签是多表查询的关键在注解中用来指定子查询返回单一对象
@One 注解属性介绍
select 指定用来多表查询的 sqlmapper
fetchType 代表加载方式延迟加载设置为LAZY立即加载为EAGER 会覆盖全局的配置lazyLoadingEnabled
使用格式
@Result(column=" ",property="",one=@One(select=""))
    
4. @Many 注解多对一
代替了<Collection>标签,是是多表查询的关键在注解中用来指定子查询返回对象集合
注意聚集元素用来处理一对多的关系需要指定映射的 Java 实体类的属性属性的 javaType
一般为 ArrayList但是注解中可以不定义
使用格式
@Result(property="",column="",many=@Many(select=""))

使用注解实现多对一(Mybatis会变成一对一)复杂关系映射及延迟加载

已账户和用户关系为例,多个账户归属于同一个用户

  1. 添加用户实体类User

    public class User implements Serializable {
        private Integer userId;
        private String userName;
        private Date userBirthday;
        private String userSex;
        private String userAddress;
        //生成set/get
    }
    
  2. 账户实体类 Account

    public class Account implements Serializable {
        private Integer id;
        private Integer uid;
        private Double money;
        //多对一关系映射:从表方应该包含一个主表方的对象引用
        private User user;
        //生成set/get
    }
    
  3. 添加账户的持久层接口并使用注解配置

    public interface IAccountMapper {
        /**
        * 查询所有账户,采用延迟加载的方式查询账户的所属用户
        * @return
        */
        @Select("select * from account")
        @Results(id="accountMap",
        value= {
            @Result(id=true,column="id",property="id"),
            @Result(column="uid",property="uid"),
            @Result(column="money",property="money"),
            @Result(column="uid",property="user",
                    one=@One(select="com.mg.dao.IUserMapper.findById",
                    fetchType=FetchType.LAZY)
                   )
        })
        List<Account> findAll();
    }
       
    fetchType 属性代表加载方式一般如果要延迟加载都设置为 LAZY 的值立即加载为 EAGER
    fetchType=FetchType.LAZY 延迟加载       fetchType=FetchType.EAGER 立即加载
    
  4. 添加用户的持久层接口并使用注解配置

    public interface IUserMapper {
        /**
        * 查询所有用户
        * @return
        */
        @Select("select * from user")
        @Results(id="userMap",
            value= {
                @Result(id=true,column="id",property="userId"),
                @Result(column="username",property="userName"),
                @Result(column="sex",property="userSex"),
                @Result(column="address",property="userAddress"),
                @Result(column="birthday",property="userBirthday")
            }
        )
        List<User> findAll();
           
        /**
        * 根据 id 查询一个用户
        * @param userId
        * @return
        */
        @Select("select * from user where id = #{uid} ")
        @ResultMap("userMap")
        User findById(Integer userId);
    }
    

使用注解实现一对多复杂关系映射

以用户和账户之间的关系。一个用户可以有多个账户

  1. 添加用户实体类User

    public class User implements Serializable {
        private Integer userId;
        private String userName;
        private Date userBirthday;
        private String userSex;
        private String userAddress;
        //一对多关系映射:主表方法应该包含一个从表方的集合引用
        private List<Account> accounts;
        //生成set/get
    }
    
  2. 编写用户的持久层接口并使用注解配置

    public interface IUserMapper {
        /**
        * 查询所有用户
        * @return
        */
        @Select("select * from user")
        @Results(id="userMap",
            value= {
                @Result(id=true,column="id",property="userId"),
                @Result(column="username",property="userName"),
                @Result(column="sex",property="userSex"),
                @Result(column="address",property="userAddress"),
                @Result(column="birthday",property="userBirthday"),
                @Result(column="id",property="accounts",
                        many=@Many(
                        select="com.mg.dao.IAccountMapper.findByUid",
                        fetchType=FetchType.LAZY
                        )
                )
        	}
        )
        List<User> findAll();
    }
       
    @Many:
    相当于<collection>的配置
    select 属性代表将要执行的 sql 语句
    fetchType 属性代表加载方式一般如果要延迟加载都设置为 LAZY 的值立即加载为 EAGER
    
  3. 账户实体类 Account

    public class Account implements Serializable {
        private Integer id;
        private Integer uid;
        private Double money;
        //生成set/get
    }
    
  4. 编写账户的持久层接口并使用注解配置

    public interface IAccountMapper {
        /**
        * 根据用户 id 查询用户下的所有账户
        * @param userId
        * @return
        */
        @Select("select * from account where uid = #{uid} ")
        List<Account> findByUid(Integer userId);
    }
    

mybatis 基于注解的二级缓存

  1. 在 SqlMapConfig 中开启二级缓存支持 (可省略,默认为true)

    <!-- 配置二级缓存 -->
    <settings>
        <!-- 开启二级缓存的支持 -->
        <setting name="cacheEnabled" value="true"/>
    </settings>
    
  2. 在持久层接口中使用注解配置二级缓存

    //mybatis 基于注解方式实现配置二级缓存
    @CacheNamespace(blocking=true)
    public interface IUserDao {     }
    

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦