myBatisアプリケーション
最近のプロジェクトではmyBatisを使用していますので、今のところ、いくつかの問題や曖昧なところについてはここに表示してください.
まずmybaitsは「半自動化」というORMの枠組みです.
使用するjarバッグ:mybatis-3.4.2.jar(mybatisコアバッグ)、mybatis-spring-1.00.jar(springとjarバッグを結合する)これらのjarは公式サイトでダウンロードできます.
設定ファイル:
マップファイル:
appication.xmlファイルの変更
コードの実装
ユニットテストは文章を見てください.
まずmybaitsは「半自動化」というORMの枠組みです.
使用するjarバッグ:mybatis-3.4.2.jar(mybatisコアバッグ)、mybatis-spring-1.00.jar(springとjarバッグを結合する)これらのjarは公式サイトでダウンロードできます.
設定ファイル:
<?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>
<!-- cache / true|false true -->
<setting name="cacheEnabled" value="true"/>
<!-- 。 ‘ ', 。 -->
<setting name="lazyLoadingEnabled" value="false"/>
<!-- ‘ ’ , 。 , 。 -->
<setting name="aggressiveLazyLoading" value="true"/>
<!-- ( ) -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 。 。 -->
<setting name="useColumnLabel" value="true"/>
<!-- JDBC 。 . true, ,
。 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- MyBatis 。PARTIAL ,NONE 。
FULL 。 -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- ,SIMPLE 。REUSE preparedstatements ,BATCH 。 -->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!-- , . -->
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
<!-- -->
<typeAliases>
<typeAlias type="com.igit.storage.client.model.ClientRelation" alias="ClientRelation"/>
</typeAliases>
<!--
<plugins>
<plugin interceptor="com.igit.storage.util.DiclectStatementHandlerInterceptor"></plugin>
<plugin interceptor="com.igit.storage.util.DiclectResultSetHandlerInterceptor"></plugin>
</plugins>
-->
<!-- -->
<mappers>
<mapper resource="com/igit/storage/client/model/ClientRelation.xml"/>
</mappers>
</configuration>
マップファイル:
<?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="com.igit.storage.client.model.ClientRelation">
<resultMap type="ClientRelation" id="relationResult"><!-- -->
<association property="client" column="clientId" javaType="Client" select="com.igit.storage.client.model.Client.selectById">
</association>
</resultMap>
<insert id="insert" parameterType="ClientRelation" useGeneratedKeys="true" keyProperty="id">
insert into S_ClientRelation(relation,clientId)
values(#{relation},#{client.clientId})
</insert>
<delete id="delete" parameterType="long" flushCache="true">
delete from S_ClientRelation where relation=#{value}
</delete>
<!-- flushCache="true" -->
<delete id="remove" parameterType="long" flushCache="true">
delete from S_ClientRelation where id=#{id}
</delete>
<!-- , key array map-->
<delete id="removeMore" parameterType="map" flushCache="true">
delete from S_ClientRelation
where id in
<foreach collection="array" index="index" item="ids" open="(" separator="," close=")">
#{ids}
</foreach>
</delete>
<select id="selectById" resultMap="relationResult" parameterType="long">
select * from S_ClientRelation where id=#{value}
</select>
<select id="selectByRelation" resultMap="relationResult" parameterType="long">
select * from S_ClientRelation where relation=#{value}
</select>
<select id="selectByPage" resultMap="relationResult">
select * from S_ClientRelation
</select>
<select id="selectByCount" resultType="long">
select count(*) from S_ClientRelation
</select>
<select id="selectMaxRelation" resultType="long">
select max(relation) from s_clientRelation
</select>
</mapper>
appication.xmlファイルの変更
<!-- Hibernate TransactionManager JdbcTransactionManager, mybatis hibernate -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- mybatis sqlSessionFactory datasource -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatisconfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- mybatis hibernateTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
コードの実装
package com.igit.storage.client.service.imp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.util.Assert;
import org.springside.core.dao.Page;
import com.igit.exception.BusinessException;
import com.igit.storage.client.model.Client;
import com.igit.storage.client.model.ClientRelation;
import com.igit.storage.client.service.ClientRelationService;
public class ClientRelationServiceImp implements ClientRelationService {
private SqlSessionTemplate sqlSessionTemplate;
/**
*
*/
public void addRelationByClients(Long relation, List<Client> clients) {
// id
if(!isValidRelation(relation)){
throw new BusinessException(" !");
}
//
for(Client client : clients){
ClientRelation cr = new ClientRelation();
cr.setRelation(relation);
cr.setClient(client);
Assert.notNull(client, " Null");
sqlSessionTemplate.insert(ClientRelation.class.getName()+".insert", cr);
}
}
/**
*
*/
public void cancelRelation(Long relation) {
sqlSessionTemplate.delete(ClientRelation.class.getName()+".delete", relation);
}
/**
*
*/
public void createRelation(List<Client> clients) {
//
if(clients.size()<=1){
throw new BusinessException(" , 2 !");
}
ClientRelation cr = new ClientRelation();
cr.setRelation(getMaxRelation());
//
for(Client client : clients){
cr.setClient(client);
Assert.notNull(client, " Null");
sqlSessionTemplate.insert(ClientRelation.class.getName()+".insert", cr);
}
}
/**
* ID
* @return
*/
private Long getMaxRelation(){
Long relation = 1L;
Long maxRelation = (Long) sqlSessionTemplate.selectOne(ClientRelation.class.getName()+".selectMaxRelation");
if(maxRelation==null){
return relation;
}
return maxRelation+1;
}
/**
*
*/
public Page getClientRelationByPage(int startIndex, int pageSize) {
List<ClientRelation> list = sqlSessionTemplate.selectList(ClientRelation.class.getName()+".selectByPage", new RowBounds(startIndex,pageSize));
Long totalCount = (Long) sqlSessionTemplate.selectOne(ClientRelation.class.getName()+".selectByCount");
return new Page(list,totalCount,pageSize,startIndex);
}
/**
*
*/
public Page getNoRelationClients(int startIndex,int pageSize) {
List<Client> list = sqlSessionTemplate.selectList(Client.class.getName()+".selectSingleClient", new RowBounds(startIndex,pageSize));
Long totalCount = (Long) sqlSessionTemplate.selectOne(Client.class.getName()+".selectSingleCount");
return new Page(list,totalCount,pageSize,startIndex);
}
/**
*
*/
public void removeRelationByClients(Long relation, List<Client> clients) {
//1.
List<ClientRelation> list = getRelationClients(relation);
if((list.size()-clients.size())<=1){
throw new BusinessException(" , 2 !");
}
//2.
for(Client c : clients){
Map<String,Long> params = new HashMap<String,Long>();
params.put("relation", relation);
params.put("clientId", c.getClientId());
sqlSessionTemplate.delete(ClientRelation.class.getName()+".remove", params);
}
}
private boolean isValidRelation(Long relation) {
List<ClientRelation> list = getRelationClients(relation);
if(list.size()<=1){
return false;
}
return true;
}
private List<ClientRelation> getRelationClients(Long relation) {
List<ClientRelation> list = sqlSessionTemplate.selectList(ClientRelation.class.getName()+".selectByRelation", relation);
return list;
}
public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
}
ユニットテストは文章を見てください.