论坛首页 Java企业应用论坛

Mybatis3.0查询,保存,更新,删除数据。

浏览 76915 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-02-15  
抛砖引玉!!
0 请登录后投票
   发表时间:2012-02-26  
SqlSessionFactory是应用范围的,而builder、sqlSession、sqlMapper都应该是方法范围的。注意sqlSession是线程不安全的。
0 请登录后投票
   发表时间:2012-02-27  
直线曲线 写道
SqlSessionFactory是应用范围的,而builder、sqlSession、sqlMapper都应该是方法范围的。注意sqlSession是线程不安全的。

采用threadLocal
0 请登录后投票
   发表时间:2012-03-08  
tomleader 写道
没写分页啊,这些都没什么用。

 

分页有拦截器,在mybatis的配置文件里面配置一下。然后在调用mybatis的selectList api时加一个new RowBounds()参数就可以了.

-----------------------------------------------------------------------------------------

	<plugins>
		<plugin interceptor="com.jacarri.common.filter.PaginationInterceptor" />
	</plugins>

 

 

package com.jacarri.common.filter;

import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.RowBounds;

@Intercepts( { @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class PaginationInterceptor implements Interceptor {
	private final static Log log = LogFactory
			.getLog(PaginationInterceptor.class);

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		log.debug("开始拦截");
		StatementHandler statementHandler = (StatementHandler) invocation
				.getTarget();
		BoundSql boundSql = statementHandler.getBoundSql();
		MetaObject metaStatementHandler = MetaObject
				.forObject(statementHandler);
		RowBounds rowBounds = (RowBounds) metaStatementHandler
				.getValue("delegate.rowBounds");
		if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
			log.info("没有指定行标。退出拦截");
			return invocation.proceed();
		}
		String originalSql = (String) metaStatementHandler
				.getValue("delegate.boundSql.sql");
		log.debug("originalSql\t" + originalSql);
		metaStatementHandler.setValue("delegate.boundSql.sql", OracleDialect
				.getLimitString(originalSql, rowBounds.getOffset(), rowBounds
						.getLimit()));
		metaStatementHandler.setValue("delegate.rowBounds.offset",
				RowBounds.NO_ROW_OFFSET);
		metaStatementHandler.setValue("delegate.rowBounds.limit",
				RowBounds.NO_ROW_LIMIT);
		log.debug("生成分页SQL : " + boundSql.getSql());
		log.debug("退出拦截");
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}

}

 

---------------

备注:该代码仅作参考。我怀疑这个拦截器把一些配置信息给"弄掉"了:感觉加上这个拦截器后,缓存的配置就失效了。

 

 

 

 

0 请登录后投票
   发表时间:2012-03-09  
Jacarri_Chan 写道
tomleader 写道
没写分页啊,这些都没什么用。

 

分页有拦截器,在mybatis的配置文件里面配置一下。然后在调用mybatis的selectList api时加一个new RowBounds()参数就可以了.

-----------------------------------------------------------------------------------------

	<plugins>
		<plugin interceptor="com.jacarri.common.filter.PaginationInterceptor" />
	</plugins>

 

 

package com.jacarri.common.filter;

import java.sql.Connection;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.RowBounds;

@Intercepts( { @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class PaginationInterceptor implements Interceptor {
	private final static Log log = LogFactory
			.getLog(PaginationInterceptor.class);

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		log.debug("开始拦截");
		StatementHandler statementHandler = (StatementHandler) invocation
				.getTarget();
		BoundSql boundSql = statementHandler.getBoundSql();
		MetaObject metaStatementHandler = MetaObject
				.forObject(statementHandler);
		RowBounds rowBounds = (RowBounds) metaStatementHandler
				.getValue("delegate.rowBounds");
		if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
			log.info("没有指定行标。退出拦截");
			return invocation.proceed();
		}
		String originalSql = (String) metaStatementHandler
				.getValue("delegate.boundSql.sql");
		log.debug("originalSql\t" + originalSql);
		metaStatementHandler.setValue("delegate.boundSql.sql", OracleDialect
				.getLimitString(originalSql, rowBounds.getOffset(), rowBounds
						.getLimit()));
		metaStatementHandler.setValue("delegate.rowBounds.offset",
				RowBounds.NO_ROW_OFFSET);
		metaStatementHandler.setValue("delegate.rowBounds.limit",
				RowBounds.NO_ROW_LIMIT);
		log.debug("生成分页SQL : " + boundSql.getSql());
		log.debug("退出拦截");
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}

}

 

---------------

备注:该代码仅作参考。我怀疑这个拦截器把一些配置信息给"弄掉"了:感觉加上这个拦截器后,缓存的配置就失效了。

 

 

 

 

拦截器我用上了,缓存你可以整合oscache试试。。不过还是memcache好一点。。。

0 请登录后投票
   发表时间:2012-03-09  

能把你的拦截器代码弄上来么?  学习学习。嘿嘿……

 

能不能把你整合二级缓存的代码分享分享啊。这个我还没有配置过。

0 请登录后投票
   发表时间:2012-03-13  
qq123zhz 写道
zjfcyefeng 写道
private static SqlSession sqlSession = null;
public static SqlSession getSession() { 
    if(sqlSession == null){ 
        sqlSession = factory.openSession(); 
    } 
    return sqlSession; 


高并发怎么处理?


他那个有问题,session不能静态的。我提供一个吧

 

/**session 模板类
	 * @author zhz
	 *
	 */
	public static class SqlSessionTemplate {
		SqlSessionFactory sqlSessionFactory;
		
		public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
			this.sqlSessionFactory = sqlSessionFactory;
			
		}

		/**执行sql语句的方法
		 * @param action
		 * @return
		 */
		public Object execute(SqlSessionCallback action)  {
			SqlSession session = null;
			try {
				session = sqlSessionFactory.openSession();
				//设置事务手动提交,如果省略,事务会自动提交,则spring配置文件声明式事务不生效
				session.getConnection().setAutoCommit(false);
//				============================================
        		Object result = action.doInSession(session);
				return result;
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				if(session != null) session.close();
			}
			return null;
		}
		
		/**查询一条记录功能
		 * @param statement sql语句
		 * @param parameter 参数
		 * @return
		 */
		public Object selectOne(final String statement,final Object parameter) {
			return execute(new SqlSessionCallback() {
				public Object doInSession(SqlSession session) {
					return session.selectOne(statement, parameter);
				}
			});
		}
		
		/**查询多条记录功能
		 * @param statement  sql语句
		 * @param parameter 查询的对象
		 * @param offset list的起点
		 * @param limit   记录条数
		 * @return
		 */
		public List selectList(final String statement,final Object parameter,final int offset,final int limit) {
			return (List)execute(new SqlSessionCallback() {
				public Object doInSession(SqlSession session) {
					return session.selectList(statement, parameter, new RowBounds(offset,limit));
				}
			});
		}
		
		/**查询多条记录功能
		 * @param statement sql语句
		 * @param parameter 查询的对象
		 * @return
		 */
		public List selectList(final String statement,final Object parameter) {
			return (List)execute(new SqlSessionCallback() {
				public Object doInSession(SqlSession session) {
					return session.selectList(statement, parameter);
				}
			});
		}
		/** 删除
		 * @param statement sql语句
		 * @param parameter 删除的对象
		 * @return
		 */
		public int delete(final String statement,final Object parameter) {
			return (Integer)execute(new SqlSessionCallback() {
				public Object doInSession(SqlSession session) {
					return session.delete(statement, parameter);
				}
			});
		}
		
		/**更新
		 * @param statement 更新sql语句
		 * @param parameter
		 * @return
		 */
		public int update(final String statement,final Object parameter) {
			return (Integer)execute(new SqlSessionCallback() {
				public Object doInSession(SqlSession session) {
					return session.update(statement, parameter);
				}
			});
		}
		
		/**插入
		 * @param statement 插入sql语句
		 * @param parameter
		 * @return
		 */
		public int insert(final String statement,final Object parameter) {
			return (Integer)execute(new SqlSessionCallback() {
				public Object doInSession(SqlSession session) {
					try {
						session.getConnection().setAutoCommit(false);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return session.insert(statement, parameter);
				}
			});
		}
	}

  其实spring也提供了sqlSessionTemplate类。mybatis没有ibatis的daoSupport支持了。。。

弱弱的问一句:session不能为static,但是sessionFactory 能为static吧?

 

0 请登录后投票
   发表时间:2012-06-01  
增加 不需要手动回滚吗?
0 请登录后投票
   发表时间:2012-08-23  
貌似和ibatis的代码差不多
0 请登录后投票
   发表时间:2012-08-23  
sunliao_first 写道
貌似和ibatis的代码差不多


要仔细体会
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics