mybatis-idea之注解方式配置

首先别忘了mybatis配置文件,在mybatis文件中配置`

<mappers>
        <!--<mapper resource="com/tao/dao/findMapper.xml"></mapper>-->
        <package name="com.tao.dao"></package>
    </mappers>

注解方式与xml文件配置相比,就不需要在resources文件夹下创建与相应接口同目录结构并且名称相同的xml文件。直接在接口中的方法上添加注解

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

    @Select("select * from orders")
    @Results(value = {
     
            @Result(property = "user_id",column = "user_id"),
            @Result(property = "user",column = "user_id",one = @One(select = "com.tao.dao.AnnotationMapper.selectUserById"
                    ,fetchType = FetchType.EAGER))
    })
    public List<Account> sselectAccountAndUser();

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

    @Select({
     "select *from orders where user_id=#{id}"})
    public List<Account> selectAccountByUId(int id);

    @Select("select * from User")
    @Results(value = {
     
            @Result(property = "accounts",column = "id",many = @Many(select = "com.tao.dao.AnnotationMapper.selectAccountByUId"
            ,fetchType = FetchType.LAZY
            ))
    })
    public List<User> selectUserAndAccount();

    @Select("select * from User where id=#{user1.id}")
    public User selectById(@Param("user1") User user);

创建测试类进行测试

SqlSession sqlSession=null;
    AnnotationMapper mapper=null;
    InputStream inputStream=null;

    @Before
    public void init() throws Exception{
     
        String resource= "SqlMapConfig.xml";
        inputStream = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        mapper = sqlSession.getMapper(AnnotationMapper.class);
    }

    @After
    public void close() throws IOException {
     
        sqlSession.commit();
        sqlSession.close();
        inputStream.close();
    }

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

    @Test
    public void selectUserById(){
     
        User user = mapper.selectUserById(24);
        System.out.println(user);
    }

    @Test
    public void sselectAccountAndUser(){
     
        List<Account> accounts = mapper.sselectAccountAndUser();
        for(Account account:accounts){
     
            System.out.println(account);
            System.out.println(account.getUser());
            System.out.println("-----------");
        }
    }

    @Test
    public void selectAccountByUId(){
     
        List<Account> accounts = mapper.selectAccountByUId(1);
        for(Account account:accounts){
     
            System.out.println(account);
        }
    }

    @Test
    public void selectUserAndAccount(){
     
        List<User> users = mapper.selectUserAndAccount();
        for(User user:users){
     
            System.out.println(user);
            System.out.println("账户信息");
            System.out.println(user.getAccounts());
        }
    }

注意
在使用xml配置文件时,参数最多一个,可以是基本类型,也可以是对象
而在进行注解时可以配置多个参数

@Select("select *from User where id=#{arg0} and username=#{arg1}")
    public User selectUser1(int id,String username);

注意,不是用#{参数名称},而是按顺序用#{arg0}

如果想直接使用参数名称,可以使用@param注解

@Select("select *from User where id=#{id} and username=#{username}")
    public User selectUser1(@Param("id") int id,@Param("username") String username);

你可能感兴趣的:(mybatis-idea之注解方式配置)