MyBatis的注解开发

目录

常用注解 

 开发步骤

(一)编写接口

(二)加载映射关系

(三)测试


常用注解 

MyBatis的注解开发_第1张图片

MyBatis的注解开发_第2张图片

MyBatis的注解开发_第3张图片

 开发步骤

(一)编写接口

public interface UserMapper {
    @Select("select * from user")
    public List findAll();
    @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
    public void save(User user);
    @Update("update  user set username=#{username},password=#{password} where id=#{id}")
    public void update(User user);
    @Delete("delete from user where id=#{id}")
    public void delete(int id);
    @Select("select * from user where id=#{id}")
    public User findById(int id);


//一对多查询
    @Select("select * from user")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "birthday",property = "birthday"),
            @Result(
                    property = "orderList",//封装的属性名
                    column = "id",//根据用户的id 去查订单表
                    javaType = List.class,//List集合类型
                    many = @Many(select = "com.example.mapper.OrderMapper.findByUid")//订单表的查询方法引用
            )
    })
    public List findUserAndOrderAll();


//多对多查询
    @Select("select * from user")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "birthday",property = "birthday"),
            @Result(
                    property = "roleList",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.example.mapper.RoleMapper.findByUid")
            )
    })
    public  List findUserAndRoleAll();
}

 (二)加载映射关系

在MyBatis核心文件中配置


    
        
        
    

(三)测试

public class MybatisTest {
    private UserMapper mapper;
    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        mapper = sqlSession.getMapper(UserMapper.class);

    }
    
    //多对多查询测试
    @Test
    public  void findUserAndRoleAll(){
        List userList =  mapper.findUserAndRoleAll();
        for (User user : userList) {
            System.out.println(user.toString());
        }
    }
    //一对多查询测试
    @Test
    public  void findUserAndOrderAll(){
       List userList =  mapper.findUserAndOrderAll();
        for (User user : userList) {
            System.out.println(user.toString());
        }
    }
    
//-------------------------------下--简单查询---------------------------------------------------
    @Test
    public  void testSave(){
        User user = new User();
        user.setUsername("费力不");
        user.setPassword("12312");
        user.setBirthday(new Date());
        mapper.save(user);
    }
    @Test
    public  void testUpdate(){
        User user = new User();
        user.setId(4);
        user.setUsername("服了");
        user.setPassword("1111111111");
        user.setBirthday(new Date());
        mapper.update(user);
    }
    @Test
    public  void testDelete(){
        mapper.delete(13);
    }
    @Test
    public  void testFindAll(){

        List userList = mapper.findAll();
        for (User user : userList) {
            System.out.println(user.toString());
        }
    }
    @Test
    public  void testFindById(){
        User user = mapper.findById(1);
        System.out.println(user.toString());
    }
}

MyBatis的注解开发_第4张图片

MyBatis的注解开发_第5张图片

 MyBatis的注解开发_第6张图片

MyBatis的注解开发_第7张图片

但遇到相关pom.xml坐标的配置的错误时,最后还是没有解决,不妨用maven构建工具,清除下缓存坐标,重新构建,再重新运行项目,可能会有惊喜!

MyBatis的注解开发_第8张图片

 下面是因为c3p0版本过低导致的,改成下面版本即可


    com.mchange
    c3p0
    0.9.5.2

MyBatis的注解开发_第9张图片

你可能感兴趣的:(MyBatis,mybatis,java,mysql)