Mybatis注解总结

Mybatis注解

Posted by AnAn on August 4, 2018

Mybatis注解

  • 第一步:建工程,导入坐标
  • 第二步:实体类、接口
  • 第三步:核心配置文件
    • SqlMapConfig.xml
        <mappers>
        		<!-- <mapper class="cn.flyingd.dao.IUserDao" /> -->
        		<package name="cn.flyingd.dao"></package>
        	</mappers>
      
  • 第四步:完善接口上的注解
      public interface IUserDao {
          @Select(value="select * from user")
          List<User> findAll();
    		
          @Select("select * from user where id= #{id}")
          User findById(Integer id);
    		
          @Insert("insert into user(username,sex,birthday,address) values(#{userName},#{userSex},#{userBirthday},#{userAddress})")
          void insert(User user);
    	
          @Update("update user set username = #{userName},sex=#{userSex},birthday= #{userBirthday},address=#{userAddress} where id=#{id}")
          void update(User user);
    		
          @Delete("delete from user where id=#{id}")
          void deleteById(Integer id);
      }
    
  • 第五步:测试
    • 测试类的方法
        public class MybatisTest {
        public static void main(String[] args) {
        		//1.读取配置文件
        		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        		//2.解析核心配置文件(解析注解),创建SqlSessionFactory对象
        		SqlSessionFactoryBuilder  builder = new SqlSessionFactoryBuilder();
        		SqlSessionFactory sqlSessionFactory = builder.build(in);
        		//3.创建SqlSession对象
        		SqlSession sqlSession = sqlSessionFactory.openSession();
        		//4.创建代理对象
        		IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        		//5.调用方法
        		List<User> userList = userDao.findAll();
        		userDao.insert(user);
        		sqlSession.commit();
        		//6.释放资源
        		sqlSession.close();
        		in.close();
        	}
      }
      
  • 多对一,一对一
      public interface IAccountDao{
    		
          @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"),
                  @Reulst(property="user",column="uid",
                      one=@One(select="cn.flyingd.dao.IUserDao.findById",fetchType= FetchType.LAZY))
              }
          )
          List<Account> findAll();
    		
          @Select("select * from account where uid= #{uid}")
          List<Account> findAccountByUid(Integer uid);
      }
    
  • 一对多,多对多
      public interface IUserDao {
          @Select(value="select * from user")
          @Results(
              id="userMap",
              value={
                  @Result(id=true,column="id",property="id"),
                  @Result(column="address",property="address"),
                  @Result(column="username",property="username"),
                  @Result(property="accountList",column="id",
                       many = @Many(select = "cn.flyingd.dao.IAccountDao.findAccountByUid",fetchType = FetchType.LAZY))
              }
          )
          List<User> findAll();
    		
          @Select("select * from user where id= #{id}")
          User findById(Integer id);
      }