iBATIS(mybatis) †
iBATISとは †SQLを利用する事に注力したO/Rマッピングツール インストール †https://github.com/mybatis/mybatis-3 から目的のものを取得し、mybatis-X.X.X.jar にCLASSPATH を通す。 テーブル・データの準備 †CREATE TABLE `books` ( `id` int(11) NOT NULL AUTO_INCREMENT, `isbn` varchar(255) DEFAULT NULL, `title` varchar(255) DEFAULT NULL, `price` int(11) DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into books(isbn,title,price,created_at,updated_at) values('978-4822280536', 'デッドライン', 2376, NOW(), NOW()) ,('978-4873114798', 'プログラマが知るべき97のこと', 2052, NOW(), NOW()) ,('978-4873115658', 'リーダブルコード', 2592, NOW(), NOW()); 設定ファイルの作成 †mybatis-config.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> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> <!-- アンダースコアとキャメルケースの変換をするかどうか --> </settings> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/example_db?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="user" /> <property name="password" value="pass" /> </dataSource> </environment> </environments> <mappers> <mapper resource="example/mapper/BookMapper.xml" /> </mappers> </configuration> エンティティクラスの作成 †package example.entity; import java.io.Serializable; public class Book extends EntityBase implements Serializable { private static final long serialVersionUID = 1L; private int id; private String isbn; private String title; private int price; private java.util.Date createdAt; private java.util.Date updatedAt; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public java.util.Date getCreatedAt() { return createdAt; } public void setCreatedAt(java.util.Date createdAt) { this.createdAt = createdAt; } public java.util.Date getUpdatedAt() { return updatedAt; } public void setUpdatedAt(java.util.Date updatedAt) { this.updatedAt = updatedAt; } }
処理の作成(SQLをXMLファイルに定義する場合) †マッパー定義 †example/mapper/BookMapper.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="example.mapper.BookMapper"> <resultMap id="bookMap" type="example.entity.Book" /> <!-- DBの列名とEntityクラスのフィールド名が異なる場合は、以下のように紐付けも可能 <resultMap id="bookMap" type="example.entity.Book" /> <id property="id" column="id" /> <result property="book_title" column="title" /> <result property="book_isbn" column="isbn" /> <result property="book_price" column="price" /> <result property="book_created_at" column="createdAt" /> <result property="book_updated_at" column="updatedAt" /> </resultMap> --> <select id="selectById" resultType="example.entity.Book"> select * from books where id = #{id} </select> <select id="selectAll" resultMap="bookMap"> select * from books order by id </select> <insert id="insert" parameterType="example.entity.Book"> insert into books (isbn,title,price,created_at,updated_at) values(#{isbn},#{title},#{price},now(),now()) <!-- MySQLの場合、以下で自動採番(AUTO_INCREMENT)されたIDをオブジェクトにセットできる --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select @@IDENTITY <!-- SELECT LAST_INSERT_ID() でも可 --> </selectKey> </insert> <update id="update" parameterType="example.entity.Book"> update books set title = #{title}, price = #{price}, updated_at = now() where id = #{id} </update> <delete id="delete" parameterType="example.entity.Book"> delete from books where id = #{id} </delete> <delete id="deleteById" parameterType="int"> delete from books where id = #{id} </delete> </mapper> 処理の作成 †package example.service; import java.util.List; import org.apache.ibatis.session.SqlSession; import example.entity.Book; /** * XMLを使用したSQLマッピングによる検索/登録/更新/削除.<br /> */ public class SampleAll extends SampleBase { public static void main(String[] args) throws Exception { new SampleAll().execute(); } /** * メイン処理.<br /> * @throws Exception */ public void execute() throws Exception{ SqlSession session = openSqlSession(); try { // 登録 Book newBook = new Book(); newBook.setIsbn("TEST1"); newBook.setTitle("タイトル1"); newBook.setPrice(1080); session.insert("example.mapper.BookMapper.insert", newBook); System.out.println("id : " + newBook.getId()); // 1件取得 Book book1 = session.selectOne("example.mapper.BookMapper.selectById", newBook.getId()); System.out.println("id : " + book1.getId()); System.out.println("isbn : " + book1.getIsbn()); System.out.println("title : " + book1.getTitle()); System.out.println("price : " + book1.getPrice()); System.out.println("created : " + book1.getCreatedAt()); System.out.println("updated : " + book1.getUpdatedAt()); System.out.println("------------------------------"); // 全件取得 List<Book> list = session.selectList("example.mapper.BookMapper.selectAll"); for (int i = 0; i < list.size(); i++) { Book book = (Book) list.get(i); System.out.println("id : " + book.getId()); System.out.println("isbn : " + book.getIsbn()); System.out.println("title : " + book.getTitle()); System.out.println("price : " + book.getPrice()); System.out.println("created : " + book.getCreatedAt()); System.out.println("updated : " + book.getUpdatedAt()); System.out.println("------------------------------"); } // 更新 Book updBook = session.selectOne("example.mapper.BookMapper.selectById", newBook.getId()); updBook.setTitle("タイトル変更"); updBook.setPrice(1080); session.update("example.mapper.BookMapper.update", updBook); // 引数にEntityオブジェクトを指定して1件削除 Book delBook = session.selectOne("example.mapper.BookMapper.selectById", newBook.getId()); session.delete("example.mapper.BookMapper.delete", delBook); // 引数にidを指定して1件削除 session.delete("example.mapper.BookMapper.deleteById", 20); session.commit(); } finally { session.close(); } } public SqlSession openSqlSession() throws Exception{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); } } 処理の作成(SQLをアノテーションに記述する場合) †マッパー定義 †example/mapper/BookMapper.java package example.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.SelectKey; import org.apache.ibatis.annotations.Update; import example.entity.Book; public interface BookMapper { /** * 引数のidを条件にして1件取得.<br /> * @param id * @return Bookオブジェクト */ @Select("select * from books where id = #{id}") Book selectById(int id); /** * 全件取得.<br /> * @return Bookオブジェクトのリスト */ @Select("select * from books order by id") List<Book> selectAll(); /** * 登録.<br /> * @param book Bookオブジェクト */ @Insert("insert into books (isbn,title,price,created_at,updated_at) values (#{isbn},#{title},#{price},now(),now())") @SelectKey(statement="select LAST_INSERT_ID()", keyProperty="id", before=false, resultType=int.class) void insert(Book book); /** * 更新.<br /> * @param book Bookオブジェクト */ @Update("update books set title = #{title}, price = #{price}, updated_at = now() where id = #{id}") void update(Book book); /** * 引数のidを条件にして削除.<br /> * @param id */ @Delete("delete from books where id = #{id}") void deleteById(int id); /** * 引数のBookオブジェクトに設定されたidを条件にして削除.<br /> * @param id */ @Delete("delete from books where id = #{id}") void delete(Book book); } 処理の作成 †package example.service; import java.util.List; import org.apache.ibatis.session.SqlSession; import example.entity.Book; import example.mapper.BookMapper; /** * XMLを使用したSQLマッピングによる検索/登録/更新/削除.<br /> */ public class SampleAll extends SampleBase { public static void main(String[] args) throws Exception { new SampleAll().execute(); } /** * メイン処理.<br /> * @throws Exception */ public void execute() throws Exception{ SqlSession session = openSqlSession(); try { BookMapper mapper = session.getMapper(BookMapper.class); // 登録 Book newBook = new Book(); newBook.setIsbn("TEST1"); newBook.setTitle("タイトル1"); newBook.setPrice(1080); mapper.insert(newBook); System.out.println("id : " + newBook.getId()); // 1件取得 Book book1 = mapper.selectById(newBook.getId()); System.out.println("id : " + book1.getId()); System.out.println("isbn : " + book1.getIsbn()); System.out.println("title : " + book1.getTitle()); System.out.println("price : " + book1.getPrice()); System.out.println("created : " + book1.getCreatedAt()); System.out.println("updated : " + book1.getUpdatedAt()); System.out.println("------------------------------"); // 全件取得 List<Book> list = mapper.selectAll(); for (int i = 0; i < list.size(); i++) { Book book = (Book) list.get(i); System.out.println("id : " + book.getId()); System.out.println("isbn : " + book.getIsbn()); System.out.println("title : " + book.getTitle()); System.out.println("price : " + book.getPrice()); System.out.println("created : " + book.getCreatedAt()); System.out.println("updated : " + book.getUpdatedAt()); System.out.println("------------------------------"); } // 更新 Book updBook = mapper.selectById(newBook.getId()); updBook.setTitle("タイトル変更"); updBook.setPrice(1080); mapper.update(updBook); // 引数にEntityオブジェクトを指定して1件削除 Book delBook = mapper.selectById(newBook.getId()); mapper.delete(delBook); // 引数にidを指定して1件削除 mapper.deleteById(20); session.commit(); } finally { session.close(); } } public SqlSession openSqlSession() throws Exception{ Properties properties = new Properties(); // DB接続設定 properties.setProperty("driverClassName", "com.mysql.jdbc.Driver"); properties.setProperty("url" , "jdbc:mysql://127.0.0.1:3306/example_db?useUnicode=true&characterEncoding=UTF-8"); properties.setProperty("username" , "user"); properties.setProperty("password" , "pass"); properties.setProperty("initialSize" , "30"); properties.setProperty("maxActive" , "100"); properties.setProperty("maxIdle" , "30"); properties.setProperty("maxWait" , "5000"); properties.setProperty("validationQuery", "select 1"); // プロパティファイルから読む場合 //InputStream is = ClassLoader.getSystemResourceAsStream("mybatis-config.properties"); //properties.load(is); DataSource dataSource = BasicDataSourceFactory.createDataSource(properties); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); // アンダースコアとキャメルケースを変換する configuration.setMapUnderscoreToCamelCase(true); // Mapperの指定 configuration.addMappers("example.mapper"); // パッケージ指定 //configuration.addMapper(example.mapper.BookMapper.class); // クラス指定 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); return sqlSessionFactory.openSession(); } } |