iBatis (現 MyBatis 前身) 是輕量級持久層框架,強調 SQL 手寫控制,適合複雜查詢場景。本篇建立新聞資料表、POJO、SQL Map 映射、SqlMapConfig、ArticleManager 業務邏輯與 JUnit 測試。
CREATE DATABASE simpledb;
CREATE TABLE article (
ID INT AUTO_INCREMENT PRIMARY KEY,
TITLE VARCHAR(25),
AUTHOR VARCHAR(25),
CONTENT TEXT,
PUBTIME DATE
);
public class Article {
private int id;
private String title, author, content;
private Date pubtime;
// getter/setter 省略
}
<sqlMap namespace="Article">
<typeAlias alias="Article" type="cn.simple.pojo.Article"/>
<resultMap id="ArticleResult" class="Article">
<result property="id" column="ID"/>
<result property="title" column="TITLE"/>
<result property="author" column="AUTHOR"/>
<result property="content" column="CONTENT"/>
<result property="pubtime" column="PUBTIME"/>
</resultMap>
<select id="selectAllArticles" resultMap="ArticleResult">SELECT * FROM article</select>
<select id="selectArticleById" parameterClass="int" resultClass="Article">
SELECT ID as id, TITLE as title, AUTHOR as author, CONTENT as content, PUBTIME as pubtime
FROM article WHERE ID=#id#
</select>
<insert id="insertArticle" parameterClass="Article">
INSERT INTO article (TITLE, AUTHOR, CONTENT, PUBTIME) VALUES (#title#, #author#, #content#, #pubtime#)
</insert>
<update id="updateArticle" parameterClass="Article">
UPDATE article SET TITLE=#title#, AUTHOR=#author#, CONTENT=#content#, PUBTIME=#pubtime# WHERE ID=#id#
</update>
<delete id="deleteArticleById" parameterClass="int">DELETE FROM article WHERE ID=#id#</delete>
</sqlMap>
<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://localhost:3306/simpledb"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="cn/simple/pojo/Article.xml"/>
</sqlMapConfig>
public class ArticleManager {
private static SqlMapClient sqlMapper;
static {
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) { throw new RuntimeException("SqlMapClient建立失敗", e); }
}
public static List<Article> selectAllArticles() throws SQLException {
return sqlMapper.queryForList("selectAllArticles");
}
public static void insertArticle(Article article) throws SQLException {
sqlMapper.insert("insertArticle", article);
}
public static void updateArticle(Article article) throws SQLException {
sqlMapper.update("updateArticle", article);
}
public static void deleteArticle(int id) throws SQLException {
sqlMapper.delete("deleteArticleById", id);
}
public static Article queryArticleById(int id) throws SQLException {
return (Article) sqlMapper.queryForObject("selectArticleById", id);
}
}
public class ArticleManagerTest {
@Test public void testSelectAllArticles() throws SQLException {
for (Article a : ArticleManager.selectAllArticles())
System.out.println(a.getTitle() + a.getAuthor() + a.getContent() + a.getPubtime());
}
@Test public void testInsertArticle() throws SQLException {
for (int i=0; i<10; i++) {
Article a = new Article();
a.setTitle("title-" + i); a.setAuthor("author-" + i);
a.setContent("content-" + i); a.setPubtime(new Date());
ArticleManager.insertArticle(a);
}
}
@Test public void testUpdateArticle() throws SQLException {
Article a = new Article(); a.setId(3); a.setTitle("title-title"); a.setAuthor("author-author");
ArticleManager.updateArticle(a);
}
@Test public void testDeleteArticle() throws SQLException { ArticleManager.deleteArticle(5); }
}
src/
cn/simple/pojo/Article.java
cn/simple/pojo/Article.xml
cn/simple/manager/ArticleManager.java
cn/simple/manager/ArticleManagerTest.java
SqlMapConfig.xml
WEB-INF/lib/ibatis-2.3.4.726.jar, mysql-connector-java.jar
simple_example 範例專案,最快上手路徑