Más contenido relacionado
La actualidad más candente (20)
Similar a スキトラ Spring + mybatis (20)
スキトラ Spring + mybatis
- 15. XMLベース
例:
<bean id=“SampleController" class="jp.sh4.ooga.SampleController">
<property name="SampleService" ref="SampleService" />
</bean>
<bean id="SampleService" class="jp.sh4.ooga.SampleServiceImpl">
<property name="SampleDao" ref="SampleDataDao" />
</bean>
<bean id=“SampleDao" class="jp.sh4.ooga.SampleDao">
<property name="SampleMapper" ref="SampleMapper" />
</bean>
<bean id="SampleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionTemplate" ref="sqlSessionTempRefs" />
<property name="mapperInterface“ value="jp.sh4.ooga.SampleMapper" />
</bean>
- 16. XMLベース
例:
<bean id=“SampleController" class="jp.sh4.ooga.SampleController">
<property name="SampleService" ref="SampleService" />
</bean>
<bean id="SampleService" class="jp.sh4.ooga.SampleServiceImpl">
<property name="SampleDao" ref="SampleDataDao" />
</bean>
<bean id=“SampleDao" class="jp.sh4.ooga.SampleDao">
<property name="SampleMapper" ref="SampleMapper" />
</bean>
<bean id="SampleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionTemplate" ref="sqlSessionTempRefs" />
<property name="mapperInterface“ value="jp.sh4.ooga.SampleMapper" />
</bean>
これを機能ごとに一式書かないといけないから、
しんどい
- 20. アノテーションベース
良く使うアノテーション
@Controller : コントローラクラスに付ける
@Service : ビジネスロジッククラスに付ける
@Repository : データをかき集めるクラスに付ける(ドメインモデルにおいては、「≠DAO」)
@Component : 共通機能群などに付ける(居るの?って感じ)
@Transactional : トランザクション境界に付与(主にServiceクラスに付ける)
@Autowired : DIコンテナを使って外から設定させたいフィールドに付ける
@RequestMapping : HTTPリクエストを受け取る際のパスの設定
@Scope : DIはデフォルトシングルトンのため、問題が起きたときに付ける(prototypeとか)
- 25. SQLの書き方(2)
<select id=“findUser” resultMap=“UserMap”>
select user_id
, user_name
from user
where user_id = #{userId}
and invalid_flag = 0
</select>
<resultMap id=“UserMap” type=“jp.sh4.ooga.UserEntity” >
<result column=“user_id” property=“userId” jdbcType=“VARCHAR” />
<result column=“ user_name ” property=“ userName ” jdbcType=“VARCHAR” />
・・・省略・・・
</resultMap>
結果をResultMapに入れておき、
UserEntityに必要なものを書くこともできる
- 32. SpringとMyBatisの連携
<bean id=“SampleController" class="jp.sh4.ooga.SampleController">
<property name="SampleService" ref="SampleService" />
</bean>
<bean id="SampleService" class="jp.sh4.ooga.SampleServiceImpl">
<property name="SampleDao" ref="SampleDataDao" />
</bean>
<bean id=“SampleDao" class="jp.sh4.ooga.SampleDao">
<property name="SampleMapper" ref="SampleMapper" />
</bean>
<bean id="SampleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionTemplate" ref="sqlSessionTempRefs" />
<property name="mapperInterface“ value="jp.sh4.ooga.SampleMapper" />
</bean>
Daoの指定場所で、
Java側のMapperインタフェースを紐づける
XMLとJava側のマッピングは先述のとおり
- 34. SpringとMyBatisの連携
<bean id=“SampleMapper” class=“org.mybatis.spring.mapper.MapperFactoryBean”>
<property name=“sqlSessionTemplate” ref=“sqlSessionTemp” />
<property name=“mapperInterface“ value=”jp.sh4.ooga.SampleMapper” />
</bean>
<bean id=“sqlSessionTemp” class=“org.mybatis.spring.SqlSessionTemplate”>
<constructor-arg ref=“mySqlDaoRefs” />
</bean>
<bean id=“mySqlDao” class=“org.mybatis.spring.SqlSessionFactoryBean”>
・・・中略・・・
</bean>
アプリを作るときは、sqlSessionTemplateを必ず使用する。
書籍などでは、sqlSessionFactoryを使うことしか書かれていないが、これは必須。
sqlSessionFactoryはスレッドアンセーフなため、使うとよくないことが起きる。
- 47. ぐるぐる系でとにかく遅い
・全行取得 → データ加工&UPDATEなど実行 → コミット
以下のように変える
・全行取得 → データ加工 → バルククエリでまとめてUPDATEなど実行 → コミット
※全行取得もデータ件数によってはアンチパターンになる
ケースバイケースで他にも色々可能。
JDBCのメソッドやDB製品でどのようなSQLを書けるか知っているだけで、
色々工夫できる。