概要
MyBatisの使用例を備忘録として残しておきます。
アプリケーション構成
ソース
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>
<properties resource="resources/hoge.properties">
<property name="hoge" value="property init value"/>
<property name="fuga" value="property init value"/>
<property name="uwagaki" value="property init value"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@//WinPro:1521/PDB" />
<property name="username" value="SAMPLEUSR1" />
<property name="password" value="xxxxxxxx" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="resources/sample_mapper.xml" />
</mappers>
</configuration>
hoge.properties
fuga=property file naiyo1
zoyo=property file naiyo2
sample_mapper.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="sample.mybatis">
<select id="selectTest" resultType="map">
select * from PROPERTY
</select>
</mapper>
OracleJDBCExample.java
package sample.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleJDBCExample {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@//WinPro:1521/pdb", "SAMPLEUSR1", "xxxxxxxx");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
Mytest1.java
package sample.mybatis;
import java.io.InputStream;
import java.util.Properties;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Mytest1 {
public static void main(String[] args) throws Exception {
try (InputStream in = Mytest1.class.getResourceAsStream("/mybatis-config.xml")) {
Properties prop = new Properties();
prop.put("uwagaki", "Class de uwagaki");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in, prop);
Properties properties = factory.getConfiguration().getVariables();
properties.forEach((key, value) -> {
System.out.printf("%s=%s%n", key, value);
});
}
}
}
Mytest2.java
package sample.mybatis;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Mytest2 {
public static void main(String[] args) {
// 設定ファイルを読み込む
try (InputStream in = Mytest2.class.getResourceAsStream("/mybatis-config.xml")) {
// 設定ファイルを元に、 SqlSessionFactory を作成する
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// SqlSessionFactory から SqlSession を生成する
try (SqlSession session = factory.openSession()) {
// SqlSession を使って SQL を実行する
List