使用mybatis 链接oracle 设置数据分页

使用mybatis 链接oracle 设置数据分页;


1.oracle数据驱动包:
//本地驱动包依赖配置;

            com.oracle
            jdbc6
            11.2.0.1.0

           



            ${basedir}/src/lib/jdbc6.jar


            system
       



2,链接oracle 数据库:
resources目录中:
db.properties文件加;
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@localhost:1521:FZ
db.username=system
db.password=fz

3.实体类;
@Data
public class Student {
    private int id;
    private String name;
    private BigDecimal money;
}


4.mapper 中设置: 
public interface StudentMapper {
    @Insert("insert into student values(stuid.nextval,#{name},#{money})")
    public int add(@Param("name") String name, @Param("money")BigDecimal money);

    public Student queryById(int id);

    @Select("select * from student")
    public List query();

    @Select("select * from student")
    public List page(RowBounds r); 
    public List pages(@Param("start") int start,@Param("end") int end);
}
XX.xml设置:


        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


   

   

   


5.测试:
@Before  和 @After 略;

@Test
    public void jdbc() throws SQLException {
        Connection conn = this.session.getConnection();
        PreparedStatement pst = conn.prepareStatement("delete from student");
        pst.execute();
        pst.close();
        conn.commit();
        conn.close();
    }


    @Test
    public void add(){
        StudentMapper sdao = this.session.getMapper(StudentMapper.class);
        sdao.add("李四",new BigDecimal(809));
        sdao.add("王一",new BigDecimal(680)); 
    }


    @Test
    public void query() {
        StudentMapper sdao = this.session.getMapper(StudentMapper.class);
        //Student st = sdao.queryById(12);
        //System.out.println(st);
        System.out.println(sdao.query());


        //RowBounds r = new RowBounds(2,2);
        //RowBounds r = new RowBounds(0,3);
        //System.out.println(sdao.page(r));


        int currpage = 1;
        int pagesize = 3;
        //System.out.println(sdao.pages(currpage*pagesize,currpage*pagesize-pagesize));
        System.out.println(sdao.pages(currpage,pagesize));

    }

注:oracle 数据库中有表student,没有创建;
#建立学生信息表:
create table student(id number,name varchar2(30),money number(8,2),primary key(id));

#建立自增序列对象: 
  create sequence sid;

以上内容适合初学者参考,仅供参考。。。。

你可能感兴趣的:(java-mybatis)