系统原型结构描述(十)

Book.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 namespace="Book">

<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="Book" type="com.test.domain.Book"/>

<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly. -->
<resultMap id="BookResult" class="Book">
<result property="id" column="ID"/>
<result property="name" column="NAME"/>
<result property="author" column="AUTHOR"/>
<result property="summary" column="SUMMARY"/>
</resultMap>

<!-- Select with no parameters using the result map for Account class. -->
<select id="SelectAllBooks" resultMap="BookResult">
select * from BOOKS
</select>

<select id="GetBookTotal" resultClass="int">
select count(id) from BOOKS
</select>

<!-- A simpler select example without the result map. Note the
aliases to match the properties of the target result class. -->
<select id="SelectBookById" parameterClass="int" resultClass="Book">
select * from BOOKS
where ID = #id#
</select>

<select id="SelectBookByname" parameterClass="String" resultMap="BookResult">
select * from BOOKS
where NAME = #name#
</select>

<!-- Insert example, using the Account parameter class -->
<insert id="InsertBook" parameterClass="Book">
insert into BOOKS (
ID,
NAME,
AUTHOR,
SUMMARY)
values (
#id#, #name#, #author#, #summary#
)
</insert>

<!-- Update example, using the Account parameter class -->
<update id="UpdateBook" parameterClass="Book">
update BOOKS set
NAME = #name#,
AUTHOR = #author#,
SUMMARY = #summary#
where
ID = #id#
</update>

<!-- Delete example, using an integer as the parameter class -->
<delete id="DeleteBookById" parameterClass="int">
delete from BOOKS where ID = #id#
</delete>

</sqlMap>


Config.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>

<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="com/test/persistence/Book.xml"/>

</sqlMapConfig>


BookDao.java

package com.test.persistence.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.test.domain.Book;
import com.test.persistence.dao.IBookDao;

public class BookDao implements IBookDao {
private SqlMapClientTemplate sqlMapClientTemplate = null;

public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}

public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}

public List selectAllBooks(int top, int max) {
return this.sqlMapClientTemplate.queryForList("SelectAllBooks", top, max);
}

public int getBookTotal() {
return ((Integer)this.sqlMapClientTemplate.queryForObject

("GetBookTotal")).intValue();
}

public Book selectBookById(int id) {
return (Book) this.sqlMapClientTemplate.queryForObject("SelectBookById", new

Integer(id));
}

public List selectBookByName(String name) {
return this.sqlMapClientTemplate.queryForList("SelectBookByname", name);
}

public void insertBook(Book book) {
this.sqlMapClientTemplate.insert("InsertBook", book);
}

public void updateBook(Book book) {
this.sqlMapClientTemplate.update("UpdateBook", book);
}

public void deleteBookById(int id) {
this.sqlMapClientTemplate.delete("DeleteBookById", new Integer(id));
}
}

PageManager.java


package com.test.page;

public class PageManager {
private IPage page = null;
private IPageResult pageResult = null;

public IPage getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public IPageResult getPageResult() {
return pageResult;
}
public void setPageResult(PageResult pageResult) {
this.pageResult = pageResult;
}

public PageValue run(PageValue value) {
value.setRecords(this.pageResult.getTotal());
value = this.page.run(value);
value = this.pageResult.run(value);
return value;
}

}


PageResult.java

package com.test.page;

import java.util.List;

import org.apache.log4j.Logger;

import com.test.persistence.dao.impl.BookDao;

public class PageResult implements IPageResult {
static final Logger logger = Logger.getLogger(PageResult.class);
private BookDao bookDao = null;

public PageValue run(PageValue value) {
//value.setRecords(this.getTotal());
value.setResult(this.getResult(value.getTop(), value.getMax()));

logger.debug("Page : Total record " + value.getRecords());
logger.debug("Page : Result " + value.getResult());

return value;
}

public BookDao getBookDao() {
return bookDao;
}

public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}

public int getTotal() {
return this.bookDao.getBookTotal();
}

public List getResult(int top, int max){
return this.bookDao.selectAllBooks(top, max);
}

}


你可能感兴趣的:(DAO,apache,sql,xml,ibatis)