浏览 1904 次
锁定老帖子 主题:ibatis学习
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-08-10
对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; Book.java 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; } } 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> <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> 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"> <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> BookDAO.java 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); } } 测试类BookTest.java 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("------------------------------------------------------------------------"); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-08-10
其实这个帖子完全没有必要发到论坛来
仅是个人意见 |
|
返回顶楼 | |
发表时间:2009-08-10
书上拿来用一下,这是笔记?
|
|
返回顶楼 | |