Mybatis構成
4947 ワード
Mybatis構成
MyBatisプロファイル要素
Mybatisの構成順序を逆さまにすることはできません
Propertyサブエレメント
propertiesファイルの使用
jdbc.properties
プログラム伝達方式を使用してパラメータを伝達する
実際の生産環境では、データベースのユーザーパスワードは秘密にされており、メンテナンス担当者は一般的にユーザーとパスワードを暗号化して暗号化して暗号化します.
まずResourcesオブジェクトを使用してjdbc.propertiesプロファイルを読み込み、元の構成のユーザー名とパスワードを取得して復号してリセットし、最後にSqlSessionFactoryBuilderのbuildメソッドを使用して複数のpropertiesパラメータを渡して完了します.これにより、以前に構成された暗号文が上書きされます.
settings設定
全量settingsの構成例
typeAliases別名
カスタムエイリアス:
多くのクラスで別名を定義する必要がある場合は、Mybatisは別名のスキャンもサポートします.
システム定義typeHandler
MybatisでtypeHandlerはインタフェースを実現する必要があるorg.apache.ibatis.type.TypeHandler.
TypeHandler.javaTは汎用型でjavaTypeを指す.たとえばStringが必要な場合、実装クラスはimplements Type Handlerと書くことができる. setParameterメソッドは、typeHandlerを使用してPreparedStatementオブジェクトを介してSQLパラメータを設定する際に使用される具体的なメソッドであり、iはパラメータがSQLの下付き、parameterがパラメータ、jdbcTypeがデータベースタイプである. JDBC結果セットからデータを取得して変換するか、カラム名(columnName)を使用するか、下付き(columnIndex)を使用してデータベースのデータを取得するかの3つのgetResultメソッドがあり、最後のgetResultメソッドはストレージ・プロシージャ専用です.
MyBatisプロファイル要素
xml version="1.0" encoding="UTF-8"?>
Mybatisの構成順序を逆さまにすることはできません
Propertyサブエレメント
propertiesファイルの使用
jdbc.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/ssm
databaes.username=root
database.password=123456
プログラム伝達方式を使用してパラメータを伝達する
実際の生産環境では、データベースのユーザーパスワードは秘密にされており、メンテナンス担当者は一般的にユーザーとパスワードを暗号化して暗号化して暗号化します.
String resource = "mybatis-config.xml";
InputStream inputStream;
InputStream in = Reources.getResourceAsStream("jdbc.properties");
Properties props = new Properties();
props.load(in);
String username = props.getProperty("database.username");
String password = props.getProperty("database.password");
// ,
props.put("database.username",CodeUtils.decode(username));
props.put("database.password",CodeUtils.decode(password));
inputStream = Resources.getResourceAsStream(resource);
// properties
SqlSessionFactroy = new SqlSessionFactoryBuilder().build(inputStream,props);
まずResourcesオブジェクトを使用してjdbc.propertiesプロファイルを読み込み、元の構成のユーザー名とパスワードを取得して復号してリセットし、最後にSqlSessionFactoryBuilderのbuildメソッドを使用して複数のpropertiesパラメータを渡して完了します.これにより、以前に構成された暗号文が上書きされます.
settings設定
全量settingsの構成例
typeAliases別名
カスタムエイリアス:
多くのクラスで別名を定義する必要がある場合は、Mybatisは別名のスキャンもサポートします.
システム定義typeHandler
MybatisでtypeHandlerはインタフェースを実現する必要があるorg.apache.ibatis.type.TypeHandler.
TypeHandler.java
public interface TypeHandler{
void setParameter(PreparedStatement ps, int i, T parameter, jdbcType jdbcType) throws SQLException;
T getResult(Result rs, String columnName) throws SQLException;
T getResult(Result rs, int columnIndex) throws SQLException;
T getResult(Result rs, int columnIndex) throws SQLxception;
T getResult(CallableStatement cs, int columnIndex) throws SQLException;
}