MyBatis paging Implementation (MySQL)

Source: Internet
Author: User
Tags connection pooling

Beginner MyBatis paged query; includes unconditional pagination and conditional paging

Student.java

Package Cn.buaa.mybatis.app3;public class Student {private Integer id;private String name;private Double sal;public stude NT (Integer ID, String name, Double Sal) {super (); this.id = Id;this.name = Name;this.sal = sal;} Public Student () {super ();} Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Double Getsal () {return sal;} public void Setsal (Double sal) {this.sal = sal;} @Overridepublic String toString () {return "Student [id=" + ID + ", name=" + name + ", sal=" + sal + "]";}}
Studentdao.java

Package Cn.buaa.mybatis.app3;import Java.util.linkedhashmap;import Java.util.list;import java.util.Map;import Org.apache.ibatis.session.sqlsession;import cn.buaa.mybatis.util.mybatisutil;/** * Persistent layer * * @author sycamore under the yin * */public clas  s Studentdao {/** * add student */public void Add (Student Student) throws Exception {sqlsession sqlsession = null;try {sqlsession = Mybatisutil.getsqlsession (); Sqlsession.insert (Student.class.getName () + ". Add", Student);//Things submitted Sqlsession.commit ();} catch (Exception e) {e.printstacktrace (); Sqlsession.rollback (); throw e;} finally {mybatisutil.closesqlsession ()}} /** * Unconditional Paging */public list<student> findallwithfy (int start,int size) throws Exception {sqlsession sqlsession = null; try {sqlsession = Mybatisutil.getsqlsession (); map<string,object> map = new linkedhashmap<string,object> (), Map.put ("Pstart", start), Map.put ("Psize", size); list<student> studentlist = sqlsession.selectlist (Student.class.getName () + ". Findallwithfy", map); return Studentlist;} catch (Exception e) {e.printstacktrace (); throw e;} finally {mybatisutil.closesqlsession ();}}  /** * Conditional Paging */public list<student> findallbynamewithfy (String name,int start,int size) throws Exception {sqlsession Sqlsession = null;try {sqlsession = Mybatisutil.getsqlsession (); map<string,object> map = new linkedhashmap<string,object> (), Map.put ("pname", "%" +name+ "%"), Map.put (" Pstart ", start); Map.put (" psize ", size); list<student> studentlist = sqlsession.selectlist (Student.class.getName () + ". Findallbynamewithfy", map); return studentlist;} catch (Exception e) {e.printstacktrace (); throw e;} finally {mybatisutil.closesqlsession ();}} Test public static void main (string[] args) throws Exception {Studentdao dao = new Studentdao ();/*for (int i = 1; i < 1 1; i++) {Dao.add (new Student (I, "hehe", 7000D));} */system.out.println ("-----------------------------first page"); list<student> studentlist = dao.findallbynamewithfy ("Ah", 0, 3); for (Student student:studentlist) { System.out.println (student.tostring ());} System.out.println ("---------------second page--------------"); list<student> studentList2 = dao.findallbynamewithfy ("Ah", 3, 3); for (Student Student:studentlist2) { System.out.println (Student.tostring ());} System.out.println ("---------------The third page--------------"); list<student> studentlist3= dao.findallbynamewithfy ("Ah", 6, 3); for (Student Student:studentlist3) { System.out.println (Student.tostring ());} System.out.println ("---------------on page Fourth--------------"); list<student> studentlist4= dao.findallbynamewithfy ("Ah", 9, 3); for (Student Student:studentlist4) { System.out.println (Student.tostring ());}}}
Studentmapper.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >< Mapper namespace= "Cn.buaa.mybatis.app3.Student" > <resultmap type= "cn.buaa.mybatis.app3.Student" id= "Studentm AP "> <id property=" id "column=" t_id "/> <result property=" name "column=" T_name "/> <r Esult property= "Sal" column= "T_sal"/> </resultMap> <insert id= "Add" parametertype= "Cn.buaa.mybat Is.app3.Student "> INSERT into Students (T_id,t_name,t_sal) VALUES (#{id},#{name},#{sal}) </inse Rt> <!--unconditional Inquiry-<select id= "findallwithfy" parametertype= "map" resultmap= "Studentmap" &G                T   Select T_id,t_name,t_sal from students limit #{pstart},#{psize} </select> <!-- Conditional query--<select id= "findallbynamewithfy" parametertype= "map" resultmap= "studEntmap "> select T_id,t_name,t_sal from students where t_name like #{pname} Limit #{pstart},#{psize} </select></mapper>

Mybatis.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" >  <configuration> <!--properties file under load Classpath--<properties resource= "db.properties"/> <!-- Setting type aliases--<typealiases > <typealias type= "cn.buaa.mybatis.app1.Student" alias= "Student "/> </typeAliases> <!--set the default connection environment information--<environments default=" Mysql_developer "&G               T <!--connect the environment with a unique name--<environment id= "Mysql_developer" > <!                        --MyBatis using JDBC things management--<transactionmanager type= "JDBC" ></transactionManager>                                <!--MyBatis Use connection pooling to get connected--<datasource type= "Pooled" > <property name= "Driver" value= "Com.mysql.jdbc.Driver"/>                               <property name= "url" value= "Jdbc:mysql://localhost:3306/emp"/> <property name= "username" value= "root"/> <property name= "password" value=                                "123456"/> </dataSource> </environment> <!--connect the environment with a unique name--<environment id= "Oracle_developer" > &L t;!                        --MyBatis using JDBC things management--<transactionmanager type= "JDBC" ></transactionManager>                                <!--MyBatis Use connection pooling to get connected--<datasource type= "Pooled" >                                <property name= "Driver" value= "Oracle.jdbc.driver.OracleDriver"/> <property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521/orcl"/> <prope Rty name= "username" VAlue= "Zhangdong"/> <property name= "password" value= "123456"/>        </dataSource> </environment> </environments> <!--load map files-- <mappers> <mapper resource= "Cn/buaa/mybatis/app1/studentmapper.xml"/> <ma Pper resource= "Cn/buaa/mybatis/app2/studentmapper.xml"/> <mapper resource= "Cn/buaa/mybatis/app3/stude Ntmapper.xml "/> </mappers></configuration>

Db.properties

mysql.driver=com.mysql.jdbc.drivermysql.url=jdbc:mysql://127.0.0.1:3306/empmysql.username=rootmysql.password= 123456oracle.driver=oracle.jdbc.driver.oracledriveroracle.url=jdbc:oracle:thin:@127.0.0.1:1521:o rcloracle.username=rootoracle.password=123456




MyBatis paging Implementation (MySQL)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.