MyBatis(三) OGNL log4j

MyBatis查询与Log4j配置
本文介绍使用MyBatis进行复杂条件查询的方法,包括XML映射文件中的动态SQL应用,以及如何通过Log4j进行日志记录配置。


OGNL表达式

大小写敏感







Message.xml


可以直接在里面用方法


  <!-- id唯一 可通过namespace区分 即不同的namespace下可以有相同的id  -->
  <!-- parameterType只能传一个值,只写类名,不用写变量名  -->
  <!-- “&quot;“ 双引号          ”&amp;“ &    -->
  <select id="queryMessageList" parameterType="com.example.bean.Message" resultMap="MessageResult">
    select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1 = 1
    <if test="command != null and !"".equals(command.trim())"> 
    	<!-- and前面的空格都不用加 MyBatis会自动处理好 -->
    	and COMMAND=#{command}
    </if>
    <if test="description != null and !"".equals(description.trim())"> 
    	and DESCRIPTION like '%' #{description} '%'
    </if>
  </select>


MessageDao.java

	/**
	 * 根据查询条件查询消息列表
	 */
	public List<Message> queryMessageList(String command, String description) {
		DBAccess dbAccess = new DBAccess();
		List<Message> messageList = new ArrayList<Message>();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			Message message = new Message();
			message.setCommand(command);
			message.setDescription(description);
			// 通过sqlSession执行SQL语句      namespace.id
			// 只能传递一个变量
//			messageList = sqlSession.selectList("Message.queryMessageList", command);
			messageList = sqlSession.selectList("Message.queryMessageList", message);
		} catch (IOException e) { // 在Dao层处理异常
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
		return messageList;
	}



添加log4j



%d 时间 

%t 线程名

%p 级别

%-5p 占位并右对齐

%c 类的全名

%m 输出时附加的信息

%n 换行



log4j.properties


可直接放在src下


log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO


代码:点击打开链接










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值