※2018/11/18更新:各ソフトのバージョンを最新にして再編集しました。
環境
・Eclipse 4.8
・SQL Server 2017 Express
■SQL Server 2017 Expressの準備
・「SQL Server 2017 構成マネージャー」から接続先ポートをTPCポート1433に固定にしておく。
・ユーザーを事前に作成し、JDBCでテーブルなどへ接続可能にしておく。
■Eclipseからプロジェクトを作成。ここでは「MyProject」とする。
以下のjarをプロジェクトのビルド・パスへ追加する。
(1)JDBCドライバ (サイト)
今回は「Microsoft JDBC Driver 7.0 for SQL Server 」のmssql-jdbc-7.0.0.jre8.jarを使用。
(2)my Batis (サイト)
mybatis-3.4.6.jar を公式サイトからダウンロード。
■接続先のデータベースには次のテーブルがあるとする。
(1)JDBCドライバ (サイト)
今回は「Microsoft JDBC Driver 7.0 for SQL Server 」のmssql-jdbc-7.0.0.jre8.jarを使用。
(2)my Batis (サイト)
mybatis-3.4.6.jar を公式サイトからダウンロード。
■接続先のデータベースには次のテーブルがあるとする。
CREATE TABLE UserTable(
userid int PRIMARY KEY,
name nvarchar(255) NULL,
address nvarchar(255) NULL,
birthday datetime2(7) NULL);
■Eclipse プラグインのインストール
ヘルプ>Eclipseマーケットプレース>「MyBatis Generator」で検索し、画面の指示に沿ってインストール。(MyBatis Generator 1.3.7)
■generatorConfig.xmlの自動生成
メニューバー>ファイル>新規>その他>MyBatis>「MyBatis Generator Configuration File」を選択。XMLファイルが生成される。
■generatorConfig.xmlの編集
サーバ名、DB名、プロジェクト名等を環境に合わせて書き換える。
(JDBCがSQL Serverの場合はjarファイルへのパスを正しく設定する。)
ヘルプ>Eclipseマーケットプレース>「MyBatis Generator」で検索し、画面の指示に沿ってインストール。(MyBatis Generator 1.3.7)
■generatorConfig.xmlの自動生成
メニューバー>ファイル>新規>その他>MyBatis>「MyBatis Generator Configuration File」を選択。XMLファイルが生成される。
■generatorConfig.xmlの編集
サーバ名、DB名、プロジェクト名等を環境に合わせて書き換える。
(JDBCがSQL Serverの場合はjarファイルへのパスを正しく設定する。)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="D:\(jarファイルへのパス)\mssql-jdbc-7.0.0.jre8.jar" />
<context id="context1">
<jdbcConnection connectionURL="jdbc:sqlserver://SERVERNAME\\SQLEXPRESS:1433;database=MYDATABASE;"
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
userId="???" password="???" />
<javaModelGenerator targetPackage="jp.example.entity"
targetProject="MyProject/src" />
<sqlMapGenerator targetPackage="jp.example.entity.dao"
targetProject="MyProject/src" />
<javaClientGenerator targetPackage="jp.example.entity.dao"
targetProject="MyProject/src" type="XMLMAPPER" />
<table schema="dbo" tableName="UserTable" />
</context>
</generatorConfiguration>
■generatorConfig.xmlを右クリックして、メニューから「実行」→「Run MyBatis Generator」を選択。
ファイルが自動生成される。
■ファイル階層
■ファイル階層
パッケージエクスプローラを右クリックし、「resource」という名前で「ソースフォルダ」を作る。
同様に「mapper」のソースフォルダを作る。以下のようなフォルダ構成にする。
プロジェクト名
├「src」フォルダ
│ ├ jp.example.entityパッケージ
│ │ │Usertable.java
│ │ └UsertableExample.java
│ └ jp.example.entity.daoパッケージ
│ └UsertableMapper.java
├「resource」フォルダ
│ ├generatorConfig.xml
│ └mybatis-config.xml //この後、自分で作るファイル
└「mapper」フォルダ
├UsertableMapper.xml //自動生成されたxmlファイルをここに移動
└VUsertableMapper.xml //(メソッドを自作する場合、既存のXMLに追加も可能だが、分かり易いようにファイルを分ける)
■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>
<property name="mapper.url" value="./mapper"/>
</properties>
<!--settings>
<setting name="logImpl" value="LOG4J"/>
</settings-->
<environments default="release">
<environment id="release">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://SERVERNAME\\SQLEXPRESS:1433;database=MYDATABASE;" />
<!--property name="username" value="${username}"/-->
<!--property name="password" value="${password}"/-->
<property name="username" value="???" />
<property name="password" value="???" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper url="file:${mapper.url}/UsertableMapper.xml" />
<!-- ここにXMLを追加 -->
<!-- mapper url="file:${mapper.url}/VUsertableMapper.xml" /-->
</mappers>
</configuration>
■自作のSQLの追加方法
UsertableMapper.javaにメッソド追加。VUsertableMapper.xmlに定義を書く。
UsertableMapper.javaにメッソド追加。VUsertableMapper.xmlに定義を書く。
public class MyClass {
public static void main(String... args) {
try (Reader r = Resources.getResourceAsReader("mybatis-config.xml")) {
Properties properties = new Properties();
// mybatis-config.xmlでプレースホルダにしておいて、ここで値を入れることも可能
// properties.setProperty("username", "???????");
// properties.setProperty("password", "???????");
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(r, properties);
try (SqlSession ss = ssf.openSession()) {
// Mapperインスタンス作成
UsertableMapper tmap = ss.getMapper(UsertableMapper.class);
// Exampleインスタンス作成
UsertableExample texp = new UsertableExample();
// ケース1:条件を指定してレコード取得
// 「name='青木'」または「address='大阪府'」のレコード抽出
texp.createCriteria().andNameEqualTo("青木");
texp.or().andAddressEqualTo("大阪府");
List<Usertable> tlist = tmap.selectByExample(texp);
for (Usertable cur : tlist) {
System.out.println(cur.getName());
}
// ケース2:レコード挿入・更新
int newUserId = 9999;
String newName = "新しい名前";
Usertable rec = new Usertable();
texp = new UsertableExample();
texp.createCriteria().andUseridEqualTo(newUserId);
// 既存レコードチェック
if (tmap.countByExample(texp) > 0) {
// アップデート
rec.setName(newName);
tmap.updateByExampleSelective(rec, texp);
// update〜SelectiveメソッドはUsertableに代入されたカラムのみアップデートする。
} else {
// インサート
rec.setUserid(newUserId);
rec.setName(newName);
tmap.insert(rec);
}
// コミット
ss.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}