zoukankan      html  css  js  c++  java
  • Mybatis的注解开发

    一、使用注解单表CRUD操作

            1.环境搭建:

                    注解开发时不需要写映射配置文件,其余与xml配置的环境一致

                    参考xml的CRUD的环境搭建https://www.cnblogs.com/cqyp/p/12491801.html

              2.在相应接口里面要实现的方法上写注解sql

       例:

    public interface IUserDao {
        /**
         * 查询所有
         * @return
         */
        @Select("select * from user")
        List<User> findAll();
        /**
         * 保存用户
         */
        @Insert("insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})")
        void saveUser(User user);
    
        /**
         * 修改用户
         * @param user
         */
        @Update("update user set username=#{username},address=#{address} where id=#{id}")
        void updateUser(User user);
    
        /**
         * 删除用户
         *
         */
        @Delete("delete from user where id=#{id}")
        void deleteUser(Integer userId);
    
        /**
         * 根据id查询用户
         */
        @Select("select * from user where id=#{id}")
        User findById(Integer userId);
        /**
         * 根据name模糊查询用户
         */
        @Select("select * from user where username like #{username}")
        List<User> findUserByName(String username);
        /**
         * 查询总用户数量
         */
        @Select("select count(id) from user")
        int findTotalUser();
    }

    二、多表的注解开发

            多表查询XML和注解最终源码:https://github.com/Cqypyglz/mybatis

             1.一对一(查询出一个账户所对应一个用户)

                       注解开发时不需要写映射配置文件,其余与xml配置的环境一致

                   1)参考xml的多表查询的开发:https://www.cnblogs.com/cqyp/p/12493990.html

              2..在相应接口里面要实现的方法上写注解sql       

          /**
    * IAccountDao接口
    * 查询所有账户,并获得每个账户所属的用户信息 * * @Result(property = "user",column = "uid",one = @One(select="com.li.dao.IUserDao.findById",fetchType= FetchType.EAGER)) * property:要封装的属性名称 * column:通过uid去查找 * select:用户根据id查询时,所需要的参数值(全限定类名+方法名) * fetchType:EAGER立即加载
    */ @Select("select * from account") @Results(id = "accountMap", value = { @Result(id = true, property = "id", column = "id"), @Result(property = "uid",column = "uid"), @Result(property = "money",column = "money"), @Result(property = "user",column = "uid",one=@One(select="com.li.dao.IUserDao.findById",fetchType = FetchType.EAGER)), }) public List<Account> findAll();
       /**
         *  IUserdao的接口中
         * 根据id查询
         */
        @Select("select * from user where id=#{id}")
        public User findById(Integer id);
    /*
        别的方法通过id引用
    */
    @ResultMap("accountMap")
        public Account findByUid(Integer uid);

             2.一对多(查询出一个用户以及所对应的多个账户)

                   注解开发时不需要写映射配置文件,其余与xml配置的环境一致

                   1)参考xml的多表查询的开发:https://www.cnblogs.com/cqyp/p/12493990.html

              2..在相应接口里面要实现的方法上写注解sql             

     /**
    * IUserDao的接口中 * 查询用户所对应所有的账户 * 如果实体类的属性名和数据库的列名不一致时使用 * id:唯一标志,别的地方也可应用 * @Results(id="userMap",value={}) * 延迟加载 * FetchType.LAZY * */ @Select("select * from user") @Results(id="accountMap",value = { @Result(id=true,property = "id",column = "id"), @Result(property = "username",column = "username"), @Result(property = "sex",column = "sex"), @Result(property = "address",column = "address"), @Result(property = "birthday",column = "birthday"), @Result(property = "accounts",column = "id",many = @Many(select = "com.li.dao.IAccountDao.findByUid",fetchType = FetchType.LAZY)), }) public List<User> findAll();

    /**
         * 在IAccountDao的接口中
         * 通过uid查询
         */
        @Select("select * from account where uid=#{uid}")
        public Account findByUid(Integer uid);

    二、开启二级缓存的配置

          只需在接口上配置注解:            

    //开启二级缓存
    @CacheNamespace(blocking = true)

     

  • 相关阅读:
    深浅拷贝的区别
    vue中动态加载组件
    一些工具软件
    echarts 自传作品入口
    Echarts progress 设置背景颜色
    Echarts柱状图,柱子设置不一样的渐变色
    .net 控制台学习笔记 一、控制台程序的异常捕获
    IdentityServer4实战:自定义用户名和密码校验
    IdentityServer4实战:Token 中返回用户属性
    IdentityServer4实战:持久化 Resource
  • 原文地址:https://www.cnblogs.com/cqyp/p/12496729.html
Copyright © 2011-2022 走看看