• mybatis注解版延迟加载、立即加载、一级缓存、二级缓存


    1.延迟加载和立即加载

    public class AccountTest {
        private InputStream in;
        private SqlSessionFactory factory;
        private SqlSession session;
        private IAccountDao accountDao;
    
        @Before
        public  void init()throws Exception{
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            factory = new SqlSessionFactoryBuilder().build(in);
            session = factory.openSession();
            accountDao = session.getMapper(IAccountDao.class);
        }
    
        @After
        public  void destroy()throws  Exception{
            session.commit();
            session.close();
            in.close();
        }
    
        @Test
        public  void  testFindAll(){
            List<Account> accounts = accountDao.findAll();
            for(Account account : accounts){
                System.out.println("----每个账户的信息-----");
                System.out.println(account);
                System.out.println(account.getUser());
            }
        }
    public class AnnotationCRUDTest {
        private InputStream in;
        private SqlSessionFactory factory;
        private SqlSession session;
        private IUserDao userDao;
    
        @Before
        public  void init()throws Exception{
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            factory = new SqlSessionFactoryBuilder().build(in);
            session = factory.openSession();
            userDao = session.getMapper(IUserDao.class);
        }
    
        @After
        public  void destroy()throws  Exception{
            session.commit();
            session.close();
            in.close();
        }
    
        @Test
        public  void  testFindAll(){
            List<User> users = userDao.findAll();
            for(User user : users){
                System.out.println("---每个用户的信息----");
                System.out.println(user);
                System.out.println(user.getAccounts());
            }
        }
    
        @Test
        public void testFindOne(){
            User user = userDao.findById(57);
            System.out.println(user);
        }
    
    
        @Test
        public  void testFindByName(){
            List<User> users = userDao.findUserByName("%mybatis%");
            for(User user : users){
                System.out.println(user);
            }
        }
    
    
    }
    public class Account implements Serializable {
    
        private Integer id;
        private Integer uid;
        private Double money;
    
        //多对一(mybatis中称之为一对一)的映射:一个账户只能属于一个用户
        private User user;
    
    }
    
    public class User implements Serializable{
    
        private Integer userId;
        private String userName;
        private String userAddress;
        private String userSex;
        private Date userBirthday;
    
        //一对多关系映射:一个用户对应多个账户
        private List<Account> accounts;
    }
    public interface IAccountDao {
    
        /**
         * 查询所有账户,并且获取每个账户所属的用户信息
         * @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(property = "user",column = "uid",one=@One(select="com.itheima.dao.IUserDao.findById",fetchType= FetchType.EAGER))
        })
        List<Account> findAll();
    
        /**
         * 根据用户id查询账户信息
         * @param userId
         * @return
         */
        @Select("select * from account where uid = #{userId}")
        List<Account> findAccountByUid(Integer userId);
    }
    @CacheNamespace(blocking = true)
    public interface IUserDao {
    
        /**
         * 查询所有用户
         * @return
         */
        @Select("select * from user")
        @Results(id="userMap",value={
                @Result(id=true,column = "id",property = "userId"),
                @Result(column = "username",property = "userName"),
                @Result(column = "address",property = "userAddress"),
                @Result(column = "sex",property = "userSex"),
                @Result(column = "birthday",property = "userBirthday"),
                @Result(property = "accounts",column = "id",
                        many = @Many(select = "com.itheima.dao.IAccountDao.findAccountByUid",
                                    fetchType = FetchType.LAZY))
        })
        List<User> findAll();
    
        /**
         * 根据id查询用户
         * @param userId
         * @return
         */
        @Select("select * from user  where id=#{id} ")
        @ResultMap("userMap")
        User findById(Integer userId);
    
        /**
         * 根据用户名称模糊查询
         * @param username
         * @return
         */
        @Select("select * from user where username like #{username} ")
        @ResultMap("userMap")
        List<User> findUserByName(String username);
    
    
    }
    public class SecondLevelCatchTest {
    
        private InputStream in;
        private SqlSessionFactory factory;
    
    
        @Before
        public  void init()throws Exception{
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            factory = new SqlSessionFactoryBuilder().build(in);
    
        }
    
        @After
        public  void destroy()throws  Exception{
    
            in.close();
        }
    
        @Test
        public void testFindOne(){
            SqlSession session = factory.openSession();
            IUserDao userDao = session.getMapper(IUserDao.class);
            User user = userDao.findById(49);
            System.out.println(user);
    
            session.close();//释放一级缓存
    
            SqlSession session1 = factory.openSession();//再次打开session
            IUserDao userDao1 = session1.getMapper(IUserDao.class);
            User user1 = userDao1.findById(49);
            System.out.println(user1);
    
    
            session1.close();
    
        }
    }
  • 相关阅读:
    UE如何区分PDSCH中传输的DCCH和DTCH呢?
    LTE RLC 子层
    LTE的两篇微文
    TD-LTE中S1接口不支持的功能有哪些
    LTE系统消息SIB
    【转载】LTE – TTI Bundling技术
    关于lte上行闭环功控,下面哪个信道不能进行闭环功控
    lte每个小区有多少个可用的随机接入前导码
    LTE的物理小区标识(PCI)
    (转)MVC一个页面多个submit
  • 原文地址:https://www.cnblogs.com/liuyi13535496566/p/13601532.html
Copyright © 2020-2023  润新知