過去做過一些基於spring
後來做別的系統時候考慮過這樣的框架
首先spring和ibatis具體下載和安裝就不多說了
Spring框架下的ibatis應用
部署如下
首先spring配置文件
Spring_base
<?xml version=
<!DOCTYPE beans PUBLIC
<beans default
<!
<bean id=
<property name=
<value>net
</property>
<property name=
<value>jdbc:jtds:sqlserver://localhost:
</property>
<property name=
<value>sa</value>
</property>
<property name=
<value>sa</value>
</property>
<property name=
<value>
</property>
<property name=
<value>
</property>
<property name=
<value>
</property>
</bean>
/////////////////// dataSource
<bean id=
class=
<property name=
<value>SqlMap_config
</property>
<property name=
<ref bean=
</property>
</bean>
//////////////////// sqlMapClient
<!
<bean id=
<property name=
<ref local=
</property>
</bean>
///////////////// transactionManager:配置事務管理
<!
<import resource=
////////////把用戶自定義Bean與基本bean分開
</beans>
以上是spring 把一些ibatis相關配置集成到自己的配置文件裡面
Spring_other
<?xml version=
<!DOCTYPE beans PUBLIC
<beans>
<bean id=
<property name=
<ref bean=
</property>
<property name=
<ref local=
</property>
</bean>
////////////////////////使用service管理所有用戶自定義bean和Dao操作
<bean id=
<property name=
</bean>
///////////////用戶自定義Dao操作
////////<property name=
</beans>
Spring_other
SqlMap_config
<?xml version=
<!DOCTYPE sqlMapConfig
PUBLIC
<sqlMapConfig>
<settings lazyLoadingEnabled=
useStatementNamespaces=
enhancementEnabled=
errorTracingEnabled=
/>
/////////////定義ibatis相關操作參數
<sqlMap resource=
//////////////包含用戶的相關操作xml文件
</sqlMapConfig>
User
<?xml version=
<!DOCTYPE sqlMap
PUBLIC
<sqlMap namespace=
<typeAlias alias=
<!
<select id=
parameterClass=
resultClass=
<![CDATA[
select
id
name
sex
from
t_user
where name like #name#
]]>
</select>
<!
<update id=
<![CDATA[
update t_user
set
name=#name#
sex=#sex#
where id = #id#
]]>
</update>
<insert id=
<![CDATA[
insert into t_user(
id
name
sex)
values(
#id#
#name#
#sex#
)
]]>
</insert>
<delete id=
<![CDATA[
delete from t_user
where id = #value#
]]>
</delete>
<select id=
<![CDATA[
select * from t_user order by id desc
]]>
</select>
</sqlMap>
該配置文件屬性就不多了
針對spring_other
UserDao
package com
import java
import com
public interface UserDao {
public User getUser(String name);
public void updateUser(User user);
public List selectUser();
public void insertUser(User user);
}
UserDaoImpl
package com
import java
import org
import com
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao {
//private static Logger log = Logger
public User getUser(String name) {
return (User) this
}
public void updateUser(User user) {
this
}
public List selectUser() {
return this
}
public void insertUser(User user) {
this
}
}
現在大家也許看到這裡覺得就差不多了
Spring提供兩種方式的編程式事務管理
ⅰ
所以我們下面我們要再封裝一層以實現線程是安全的
baseService
package com
import org
/**
* 工廠的基礎類.
* @author 劉玉華
* @time
*/
public class BaseService extends TransactionTemplate{
private static final long serialVersionUID =
}
serviceFactory
package com
import org
import orgntext
import com
/**
* 數據操作工廠
* @author 劉玉華
* @time
*/
public class ServiceFactory {
private static BeanFactory factory = null;
static {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {
factory = (BeanFactory) context;
}
/**
* 獲得用戶服務類
* @return 用戶服務
*/
public static UserDao getUserService(){
return (UserDao) factory
}
}
我們所需要做的就是繼承Baseservice
UserService
package com
import java
public interface UserService {
public User getUser(final String name);
public void updateUser(final User user);
public List selectUser();
public void insertUser(final User user);
}
UserServiceImpl
package com
import java
import org
import org
import com
import com
public class UserServiceImpl extends BaseService implements UserDao {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this
}
public List selectUser() {
Object obj = execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
return userDao
}
});
return (List)obj;
}
public User getUser(final String name){
Object obj = execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
// TODO Auto
return userDao
}
});
return (User) obj;
}
public void insertUser(final User user) {
Object obj = execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status) {
userDao
return null;
}
});
}
public void updateUser(final User user) {
Object obj = execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus arg
userDao
return null;
}
});
}
}
這樣我們就把相關操作實現事務控制了
數據表建立
create table t_user(
)
這樣我們在以後調用方式為
測試類
package com
import java
import com
import com
import com
import common
public class UserTest {
private static Logger log = Logger
public static void main(String args[]){
UserDao service = ServiceFactory
User user=null;
int i =
switch (i) {
case
user
user
user
service
System
break;
case
try {
user = service
} catch (Exception e) {
log
}
System
case
List<User> ls = service
for (int j =
System
}
case
List<User> ls
for (int j =
user = ls
System
}
for (int j =
user
service
}
default:
break;
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28732.html