ibatis-分页
一、創建工程和數據庫
?? ?1.工程名:ibatisdemo1
?? ?數據庫名:ibatis
?? ??? ?創建表:student
?? ??? ?CREATE TABLE `student` (
?? ??? ?? `sid` int(11) NOT NULL,
?? ??? ?? `sname` varchar(30) DEFAULT NULL,
?? ??? ?? `major` varchar(30) DEFAULT NULL,
?? ??? ?? `birth` date DEFAULT NULL,
?? ??? ?? `score` decimal(10,0) DEFAULT NULL,
?? ??? ?? PRIMARY KEY (`sid`)
?? ??? ?) ENGINE=InnoDB DEFAULT CHARSET=utf8;
?? ??? ?添加測試數據
?? ??? ?insert? into
?? ??? ??? ?`student`(`sid`,`sname`,`major`,`birth`,`score`)
?? ??? ?values (1,'ss','ff','2014-03-06','22');
?? ??? ?insert? into
?? ??? ??? ?`student`(`sid`,`sname`,`major`,`birth`,`score`)
?? ??? ?values (2,'vv','ee','2014-03-05','33');
二、添加相關jar
?? ?1.在項目中創建lib目錄
?? ??? ?/lib
?? ?2.在lib目錄下添加jar包
?? ??? ?mysql-connector-java.jar
?? ??? ?ibatis-2.3.3.720.jar
?? ??? ?junit-4.4.jar
三、添加配置文件
?? ?1.在項目中創建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄添加屬性文件
?? ??? ?屬性文件名稱:SqlMap.properties
?? ??? ?內容:
?? ??? ?driver=com.mysql.jdbc.Driver
?? ??? ?url=jdbc:mysql://localhost:3306/ibatis
?? ??? ?username=root
?? ??? ?password=root
?? ?3.在conf目錄添加配置文件
?? ??? ?配置文件名稱:SqlMapConfig.xml
?? ??? ?內容:
?? ??? ?<?xml version="1.0" encoding="UTF-8" ?>? ?
?? ??? ?<!DOCTYPE sqlMapConfig??????? ?
?? ??? ???? PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"??????? ?
?? ??? ???? "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">? ?
?? ??? ? ?
?? ??? ?<sqlMapConfig> ?
?? ??? ??? ?<!-- 加載連接數據屬性文件 -->
?? ??? ? ??? ?<properties resource="SqlMap.properties"/>
?? ??? ? ??? ?<!-- 配置事務 -->
?? ??? ? ??? ?<transactionManager type="JDBC" commitRequired="false">
?? ??? ? ??? ?<!-- 配置數據源 -->? ?
?? ??? ??? ??? ?<dataSource type="SIMPLE">? ?
?? ??? ??? ?????? <property name="JDBC.Driver" value="${driver}"/>? ?
?? ??? ??? ?????? <property name="JDBC.ConnectionURL" value="${url}"/>? ?
?? ??? ??? ?????? <property name="JDBC.Username" value="${username}"/>? ?
?? ??? ??? ?????? <property name="JDBC.Password" value="${password}"/>? ?
?? ??? ??? ???? </dataSource>? ?
?? ??? ? ??? ?</transactionManager>? ?
?? ??? ?</sqlMapConfig>?????? ?
四、創建與數據庫表中相關的javabean和映射文件
?? ??? ??? ?1.在src下創建包
?? ??? ?cn.jbit.domain
?? ?2.在包下創建類
?? ??? ?類名:Student.java
?? ??? ?內容:
?? ??? ?public class Student {
?? ??? ??? ?private Integer sid;
?? ??? ??? ?private String sname;
?? ??? ??? ?private String major;//主修專業
?? ??? ??? ?private Date birth;
?? ??? ??? ?private float socre;
?? ??? ??? ?// get and set 省略
?? ??? ?}
?? ?3.在包下創建映射文件
?? ??? ?映射文件名:Student.xml
?? ??? ?內容:
?? ??? ??? ?<?xml version="1.0" encoding="UTF-8" ?>? ?
?? ??? ??? ?<!DOCTYPE sqlMap??????? ?
?? ??? ??? ???? PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"??????? ?
?? ??? ??? ???? "http://ibatis.apache.org/dtd/sql-map-2.dtd">? ?
?? ??? ??? ?<sqlMap>? ?
?? ??? ??? ?? <typeAlias alias="Student" type="cn.jbit.domain.Student"/>
?? ??? ??? ?? <!-- 分頁查詢 -->
?? ??? ??? ?? <select id="selectByPage" resultClass="Student" parameterClass="int">
?? ??? ??? ? ??? ?SELECT
?? ??? ??? ? ??? ??? ?*
?? ??? ??? ? ??? ?FROM
?? ??? ??? ? ??? ??? ?student
?? ??? ??? ? ??? ?LIMIT
?? ??? ??? ? ??? ??? ?#start#,2
?? ??? ??? ?? </select>
?? ??? ??? ?</sqlMap>
?? ?4.在核心配置文件中添加引用映射文件
?? ??? ?<!-- 加載映射文件 -->
?? ??? ?<sqlMap resource="cn/jbit/domain/Student.xml"/> ?
五、設計DAO層
?? ?接口:IStudentDao.java
?? ??? ?public interface IStudentDao {
?? ??? ??? ?/**
?? ??? ??? ? * 分頁查詢
?? ??? ??? ? * @param start
?? ??? ??? ? * @return
?? ??? ??? ? */
?? ??? ??? ?public List<Student> selectByPage(int start);
?? ??? ?}
?? ?實現類:StudentDaoImpl.java
?? ??? ?public class StudentDaoImpl implements IStudentDao{
?? ??? ??? ?private static SqlMapClient sqlMapClient;
?? ??? ??? ?static{
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?//加載配置文件
?? ??? ??? ??? ??? ?Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
?? ??? ??? ??? ??? ?//實例化SqlMapClient
?? ??? ??? ??? ??? ?sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
?? ??? ??? ??? ??? ?//關閉
?? ??? ??? ??? ??? ?reader.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?@Override
?? ??? ??? ?public List<Student> selectByPage(int start) {
?? ??? ??? ??? ?List<Student> students=null;
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?students = sqlMapClient.queryForList("selectByPage", start);
?? ??? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return students;
?? ??? ??? ?}
?? ??? ?}
六、設計SERVICE層
?? ?接口:IStudentService.java
?? ??? ?public interface IStudentService {
?? ??? ??? ?public List<Student> findPage(int start);
?? ??? ?}
?? ?實現類:StudentServiceImpl.java
?? ??? ?public class StudentServiceImpl implements IStudentService {
?? ??? ??? ?private IStudentDao studentDao = new StudentDaoImpl();
?? ??? ??? ?@Override
?? ??? ??? ?public List<Student> findPage(int start) {
?? ??? ??? ??? ?return studentDao.selectByPage(start);
?? ??? ??? ?}
?? ??? ?}
七、測試
?? ?public class StudentTest {
?? ??? ?IStudentService studentService = new StudentServiceImpl();
?? ??? ?/**
?? ??? ? * 查詢通過分頁
?? ??? ? */
?? ??? ?@Test
?? ??? ?public void testSelectByPage(){
?? ??? ??? ?List<Student> students =studentService.findPage(0);
?? ??? ??? ?for (Student student : students) {
?? ??? ??? ??? ?System.out.println(student.getSid());
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?
???
轉載于:https://blog.51cto.com/suyanzhu/1558748
總結
- 上一篇: javadoc时候乱码-编码 GBK 的
- 下一篇: 电线品牌