MyBatis And Spring Demo

咻pur慢 2010-10-26

Spring相关配置

//数据源
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
		<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=ibatisDB"></property>
		<property name="username" value="sa"></property>
		<property name="password" value="zhaoyu"></property>
	</bean>	
	//MyBatis支持
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="dao" class="com.dao.impl.DaoImpl">
		//注入
		<property name="sqlMapClient" ref="sqlMapClient"></property>
	</bean>
	
	//事务管理器
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
  		<property name="dataSource"> 
  		 <ref local="dataSource"/> 
    	</property> 
 	</bean>	
 	
	//通过引用bean方式配置事物(不是更加方便的设置切入点方式)
 	<bean id="DaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
 		 <property name="transactionManager"> 
  			 <ref bean="transactionManager"/> 
  		</property> 
  		<property name="target"> 
  			 <ref local="dao"/> 
  		</property> 
  		<property name="transactionAttributes"> 
 			  <props> 
  				<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop> 
  				<prop key="*">PROPAGATION_REQUIRED</prop> 
  			 </props> 
  		</property> 
 	</bean>

MyBatis相关配置

<?xml version="1.0" encoding="utf-8" ?> 
<!DOCTYPE sqlMapConfig 
PUBLIC "-//ibatis.apache.org//DTD sql Map Config 2.0//EN" 
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 

<sqlMapConfig> 	<!-- 都写在spring配置文件中,这里只需要配置映射文件 -->
	<sqlMap resource="com/entity/User.xml"/> 
</sqlMapConfig>

实体类

package com.entity;
public class User {
	private int id;
	private String name;
	private int age;
	//省略get set
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "编号:"+id+" 姓名:"+name+" 年龄:"+age;
	}	
}

配置SQL

<?xml version="1.0" encoding="utf-8" ?> 
<!DOCTYPE sqlMap 
PUBLIC "-//ibatis.apache.org//DTD sql Map 2.0//EN" 
"http://ibatis.apache.org/dtd/sql-map-2.dtd"> 

<sqlMap namespace="user">
	<typeAlias alias="user" type="com.entity.User"/>  //alias别名 type类型

	<select id="getUsers" resultClass="user" >
		<![CDATA[select * from t_user]]>
	</select>
	
	<insert id="insert" parameterClass="user">
		insert into t_user(name,age) values(#name#,#age#)
	</insert>
	
	<update id="update" parameterClass="java.util.Map">
		<![CDATA[update t_user set name=#name#,age=#age# where id = #id#]]>
	</update>
	
	<delete id="delete" parameterClass="int">
		<![CDATA[delete from t_user where id = #id#]]>
	</delete>
</sqlMap>

DAO代码

package com.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.dao.Dao;
import com.entity.User;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class DaoImpl extends SqlMapClientDaoSupport implements Dao {

	public void save(User user) {
		getSqlMapClientTemplate().insert("insert", user);
	}
	
	public void delete(int id) {
		getSqlMapClientTemplate().delete("delete",id);
	}

	public void update(String name, int age, int id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", name);
		map.put("age", age);
		map.put("id", id);
		getSqlMapClientTemplate().update("update",map);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> searchUsers() {
		return getSqlMapClientTemplate().queryForList("getUsers");
	}
	
	/**
	 * 类似hibernate的回调接口,在此是一个demo
	 * @return
	 */
	public List<User> demo(){
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {		
			public Object doInSqlMapClient(SqlMapExecutor sqlMap) throws SQLException {
				//sqlMap.queryForObject(arg0)
				return null;
			}
		});
		return null;
	}
}

相关调用代码

package com;

import java.util.Iterator;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dao.Dao;
import com.entity.User;

public class Demo {
	
	public static void main(String[] args) {	
		save();
		showUser();
		update();
		delete();
	}
	
	//保存
	public static void save(){
		Dao dao = daoFartory();
		User user = new User("zhaoliang228",24);
		dao.save(user);
	}
	
	//修改
	public static void update(){
		Dao dao = daoFartory();
		dao.update("zl", 100, 5);
	}
	
	public static void delete(){
		Dao dao = daoFartory();
		dao.delete(6);
	}
	
	//查询
	public static void showUser(){
		Dao dao = daoFartory();
		List<User> users = dao.searchUsers();
		Iterator<User> i = users.iterator();
		while(i.hasNext()){
			System.out.println(i.next());
		}
	}
	
	public static Dao daoFartory(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		Dao dao = (Dao) ctx.getBean("dao");
		return dao;
	}
}

相关推荐

kyle00 / 0评论 2020-05-07
zhangdy0 / 0评论 2020-05-01
melonjj / 0评论 2020-02-18