Mybatis学习(三)分页的实现,使用注解开发,使用注解实现CURD

分页的实现此处有两种思路:

1.使用SQL的 limit ?,?

2.使用limit分页

方式一 (步骤:):

(1).dao层中UserMapper接口的编写:

 //分页的sql实现
  List<User> getUserByLimit(Map<String,Integer> map);

(2).dao层中UserMapper接口的userMapper.xml编写

<select id="getUserByLimit" parameterType="map" resultType="study.com.pojo.User">
        select * from mybatis.user limit #{startIndex},#{pageSize}
</select>

(3).测试

@org.junit.Test
    public void test4(){
    
    
    SqlSession sqlSession = MybatisUtils.getsqlsession();
    UserDao userDao = sqlSession.getMapper(UserDao.class);

    HashMap<String, Integer> map = new HashMap<String, Integer>();
    //从该索引处开始检索
    map.put("startIndex",1);
    //检索长度为2,也就是只展示两条数据
    map.put("pageSize",2);
    List<User> userList = userDao.getUserByLimit(map);
    for (User user : userList) {
    
    
        System.out.println(user);
    }
    sqlSession.close();
}

使用注解开发

(1) 注解在接口上实现

@Select("select * from user")
List<User> getUsers();

(2)需要在核心配置文件中绑定接口

<!--绑定接口-->
<mappers>
    <mapper class="com.kuang.dao.UserMapper"/>
</mappers>

(3)测试

使用注解实现CURD

(1).dao层的UserMapper的编写:

public interface UserMapper {
    
    

    @Select("select * from user")
    List<User> getAllUser();

    //增
    @Insert("insert into user (id,name,pwd) values (#{id},#{name},#{pwd})")
    int addUser(User u);

    //删
    @Delete("delete from user where id=#{id}")
    int delete(int id);

    //改
    @Update("update user set pwd=#{pwd},name=#{name} where id=#{id}")
    int update(User u);

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

(2).mybatis核心文件的编写(映射器):

 <mappers>
       <mapper class="com.kuang.dao.UserMapper"></mapper>
 </mappers>

(3).实现事务的自动提交:

public static SqlSessionFactory getSqlSessionFactory() {
    
    
        return sqlSessionFactory;
    }

public static SqlSession getSqlSession() {
    
    
        //开启事物的自动提交
        return sqlSessionFactory.openSession(true);
 }

(4).测试类的编写:

public class UserTest {
    
    


    @Test
    public void getAllUser(){
    
    

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getAllUser();
        System.out.println(users);
        sqlSession.close();
    }

    @Test
    public void addUser(){
    
    

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User u=new User(5,"劳务","567");
        int i = mapper.addUser(u);
        System.out.println(i);
    }

    @Test
    public void delete(){
    
    

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        User u=new User(5,"劳务","567");
        int i = mapper.delete(5);
        System.out.println(i);
    }

    @Test
    public void update(){
    
    

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       User u=new User(5,"老五","666");
        int i = mapper.update(u);
        System.out.println(i);
    }

    @Test
    public void select(){
    
    

        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User i = mapper.select(5);
        System.out.println(i);
    }
}

猜你喜欢

转载自blog.csdn.net/jingli456/article/details/114228832
今日推荐