mybatis ...
http://code.google.com/p/mybatis/
http://code.google.com/p/mybatis/downloads/list?can=2
http://code.google.com/p/mybatis/downloads/detail?name=mybatis-3.0.4-bundle.zip&can=2&q=
http://code.google.com/p/mybatis/downloads/detail?name=MyBatis-3-User-Guide.pdf&can=3&q=Docs
使ってみる。
●てきとうなテーブル
CREATE TABLE BLOG(
ID int8,
NAME VARCHAR(100),
STATUS CHAR(1)
);
●てきとうに設定XMLを作る。
test/mybatis/Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://xxx/yyy"/>
<property name="username" value="postgres"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="test/mybatis/SqlMapper.xml"/>
</mappers>
</configuration>
●てきとうにマッピングのXMLを作る。
test/mybatis/SqlMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.mybatis.mapper.select.BlogMapper"><select id="selectAllCount" resultType="int">
select count(*) from Blog
</select><select id="selectBlog" parameterType="int" resultType="test.mybatis.mapper.dto.Blog">
select * from Blog where id = #{id}
</select></mapper>
●マッピングに対するインターフェース。
package test.mybatis.mapper.select;import test.mybatis.mapper.dto.Blog;
public interface BlogMapper {
Blog selectBlog(int id);
int selectAllCount();
}
●結果用bean
package test.mybatis.mapper.dto;public class Blog {
private long id;
private String name;
private String status;public long getId() {
return id;
}public void setId(long id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public String getStatus() {
return status;
}public void setStatus(String status) {
this.status = status;
}
}
●てきとうに動かす。
package test;import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;import test.mybatis.mapper.dto.Blog;
import test.mybatis.mapper.select.BlogMapper;public class Test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブLogger log = Logger.getLogger ( Test1.class.getName() );
log.debug("START");String resource = "test/mybatis/Configuration.xml";
Reader reader;
SqlSession session = null;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);Blog blog = null;
session = sqlMapper.openSession();BlogMapper mapper = session.getMapper(BlogMapper.class);
log.debug(mapper.selectAllCount());
blog = mapper.selectBlog(1);blog = (Blog) session.selectOne(
"test.mybatis.mapper.select.BlogMapper.selectBlog", 2 );System.out.print("");
} catch (Exception e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();} finally {
if( session != null ){
session.close();
}
}
log.debug("END");
System.exit(0);
}}
●これでログが出るらしい。
log4j.logger.java.sql.PreparedStatement=DEBUG
・・・DIコンテナと連携するとトランザクションを自分でやらずに勝手に任せられるようなことがちらほらと出ている。
・・・マッピングさせるインターフェースに@selectアノテーション(org.apache.ibatis.annotations.Select)をくっつけると。マッピングのXMLに定義がいらない(あると既にある的なExceptionが発生する[java.lang.IllegalArgumentException: Mapped Statements collection already contains value for...] )。
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
・
・
・