ibatis学习笔记
对book表的CRUD操作
[color=red]Book.java[/color]
[color=red]SqlMapConfig.xml[/color]
[color=red]Book.xml[/color]
[color=red]BookDAO.java[/color]
[color=red]测试类BookTest.java[/color]
对book表的CRUD操作
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int(11) NOT NULL auto_increment,
`author` varchar(50) default NULL,
`title` varchar(50) default NULL,
`price` double NOT NULL default '0',
`yr` date default NULL,
`description` varchar(100) default NULL,
`saleAmount` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
[color=red]Book.java[/color]
package com.it.domain;
import java.util.Date;
public class Book implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String author;
private String title;
private Double price;
private Date yr;
private String description;
private Integer saleAmount;
public Book() {
}
public Book(Double price) {
this.price = price;
}
public Book(String author, String title, Double price, Date yr,
String description, Integer saleAmount) {
this.author = author;
this.title = title;
this.price = price;
this.yr = yr;
this.description = description;
this.saleAmount = saleAmount;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getYr() {
return this.yr;
}
public void setYr(Date yr) {
this.yr = yr;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getSaleAmount() {
return this.saleAmount;
}
public void setSaleAmount(Integer saleAmount) {
this.saleAmount = saleAmount;
}
}
[color=red]SqlMapConfig.xml[/color]
<?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>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://127.0.0.1:3306/bookstore"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="123456"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/it/data/Book.xml"/>
</sqlMapConfig>
[color=red]Book.xml[/color]
<?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">
<typeAlias alias="Book" type="com.it.domain.Book"/>
<resultMap id="BookResult" class="Book">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="author" column="author"/>
<result property="price" column="price"/>
<result property="yr" column="yr"/>
<result property="description" column="description"/>
<result property="saleAmount" column="saleAmount"/>
</resultMap>
<select id="selectAllBooks" resultMap="BookResult">
select * from Book
</select>
<select id="selectBookById" parameterClass="int" resultClass="Book">
select * from Book where id = #id#
</select>
<insert id="insertBook" parameterClass="Book">
insert into Book (
id,
title,
author,
price,
yr,
description,
saleAmount)
values (
#id#, #title#, #author#, #price#, #yr#, #description#, #saleAmount#
)
</insert>
<update id="updateBook" parameterClass="Book">
update Book set
title = #title#,
author = #author#,
price = #price#
where
id = #id#
</update>
<delete id="deleteBookById" parameterClass="int">
delete from Book where id = #id#
</delete>
<delete id="deleteAllBooks">
delete from Book
</delete>
</sqlMap>
[color=red]BookDAO.java[/color]
package com.it.dao;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.it.domain.Book;
public class BookDAO {
private static SqlMapClient sqlmap ;
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("com/it/data/SqlMapConfig.xml");
sqlmap = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertBook(Book book) throws SQLException {
sqlmap.insert("insertBook", book);
}
public void deleteBook(int id) throws SQLException {
sqlmap.delete("deleteBookById", id);
}
public void deleteAllBooks() throws SQLException {
sqlmap.delete("deleteAllBooks");
}
public void updateBook(Book book) throws SQLException {
sqlmap.update("updateBook", book);
}
@SuppressWarnings("unchecked")
public List getAllBooks() throws SQLException {
return sqlmap.queryForList("selectAllBooks");
}
public Book getBookById(int id) throws SQLException {
return (Book) sqlmap.queryForObject("selectBookById", id);
}
}
[color=red]测试类BookTest.java[/color]
package com.it.test;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import com.it.dao.BookDAO;
import com.it.domain.Book;
public class BookTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws SQLException {
BookDAO dao = new BookDAO();
List list = dao.getAllBooks();
for (Iterator it = list.iterator(); it.hasNext();) {
Book book = (Book) it.next();
System.out.println("id=" + book.getId());
System.out.println("title=" + book.getTitle());
System.out.println("author=" + book.getAuthor());
System.out.println("yr=" + book.getYr());
System.out.println("price=" + book.getPrice());
System.out.println("descrption=" + book.getDescription());
System.out.println("saleAmount=" + book.getSaleAmount());
System.out.println("------------------------------------------------------------------------");
}
}
}