标签归档:ibatis

ibatis 打印sql出来 log4j.xml与log4j.properties两种方式

第一种  log4j.xml  也是与spring集成常用的方式

[xml]<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/ ">

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p – %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>

<appender name="Access" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/access.log" />
<param name="Append" value="false" />
<param name="MaxFileSize" value="1024" />
<param name="MaxBackupIndex" value="2" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p – %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="WARN" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>

<appender name="Error" class="org.apache.log4j.RollingFileAppender">
<param name="Append" value="false" />
<param name="File" value="C:/error.log" />
<param name="MaxFileSize" value="1024" />
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p – %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>

<root>
<appender-ref ref="STDOUT" />
<appender-ref ref="Access" />
<appender-ref ref="Error" />
</root>

<appender name="IBatis" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x – %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>

<logger name="com.ibatis" additivity="true">
<level value="DEBUG" />
</logger>

<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG" />
</logger>

<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG" />
</logger>

<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG" />
<appender-ref ref="IBatis" />
</logger>

</log4j:configuration>[/xml]

第二种是log4j.properties

[xml]
#
log4j.rootLogger=DEBUG, stdout, fileout
#log4j.logger.test=info
#log4j.logger.org.apache.jasper = DEBUG
#log4j.logger.org.apache.catalina.startup.TldConfig = DEBUG
#log4j.logger.org.apache.catalina.session.ManagerBase = DEBUG

log4j.logger.com.fiscal = DEBUG
log4j.logger.com.system = DEBUG

log4j.logger.com.ibatis = DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG
log4j.logger.java.sql.Connection = DEBUG
#log4j.logger.java.sql.Statement = DEBUG
#log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet = DEBUG

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=C:\\ibatis.log
log4j.appender.fileout.MaxFileSize=10000KB

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} :%m%n
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n

#log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

# log4j.logger.org=info
#——————————–

[/xml]

 

两种方式都来源于网络

xml:

http://wuguowei1314.iteye.com/blog/1435164

log4j.properties:

http://www.iteye.com/topic/309147

 

IBatis中使用enum(IBatis sqlMal Enum)

很多朋友可能会说大可以使用Mybatis,但是目前项目决定,所以还得使用IBaits进行开发。过程中涉及到一个业务类型,原本使用数据库表进行存储,但是就为几个值而多了一个连表查询时很不值得的,所以果断
采用Enum来作为一个属性进行存储,数据库表存储int型的code值。展示时可以展示为对应的标题。接下来直接说一下步骤吧。

1、引入jar包,我是用的是maven

[java]<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.0</version>
</dependency>
[/java]

2、接下来看一下我的Enum对象:

[java]public enum BusinessTypeEnum {

BOTH(0, "两者都是"),
SUPPLIER(1, "供应商"),
PURCHASER(2, "采购商"),
MERCHANTS(3, "招商");

private int code = 0;

private String name = "";

private BusinessTypeEnum(int code, String name) {
this.code = code;
this.name = name;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}[/java]

3、自定义BusinessTypeEnum的处理类:

[java]import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

import java.sql.SQLException;
import java.sql.Types;

/**
* Created by jian on 14-8-7.
*/
public class BusinessTypeEnumHandler implements TypeHandlerCallback {
/**
* Performs processing on a value before it is used to set
* the parameter of a PreparedStatement.
*
* @param setter The interface for setting the value on the PreparedStatement.
* @param parameter The value to be set.
* @throws java.sql.SQLException If any error occurs.
*/
public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
if(parameter==null){
setter.setInt(Types.INTEGER);
}else
{
BusinessTypeEnum businessTypeEnum=(BusinessTypeEnum)parameter;
setter.setInt(businessTypeEnum.getCode());
}
}

/**
* Performs processing on a value before after it has been retrieved
* from a ResultSet.
*
* @param getter The interface for getting the value from the ResultSet.
* @return The processed value.
* @throws java.sql.SQLException If any error occurs.
*/
public Object getResult(ResultGetter getter) throws SQLException {
BusinessTypeEnum businessTypeEnum=null;
if(!getter.wasNull() && getter.getObject()!=null){
for (BusinessTypeEnum item: BusinessTypeEnum.values())
{
if(item.getCode()==Integer.parseInt(getter.getObject().toString()))
{
businessTypeEnum=item;
}
}
}
return businessTypeEnum;
}

/**
* Casts the string representation of a value into a type recognized by
* this type handler. This method is used to translate nullValue values
* into types that can be appropriately compared. If your custom type handler
* cannot support nullValues, or if there is no reasonable string representation
* for this type (e.g. File type), you can simply return the String representation
* as it was passed in. It is not recommended to return null, unless null was passed
* in.
*
* @param s A string representation of a valid value for this type.
* @return One of the following:
* <ol>
* <li>the casted repersentation of the String value,</li>
* <li>the string as is,</li>
* <li>null, only if null was passed in.</li>
* </ol>
*/
public Object valueOf(String s) {
return s;
}
}[/java]

4、配置处理类:
这里需要说明一下,配置方式分两种,一种是全局设置,另外一种则是单独设置,相比较来说全局设置比较方便一些:

[java]<sqlMapConfig>
<typeHandler javaType="com.yneit.www.common.BusinessTypeEnum" callback="com.yneit.www.common.BusinessTypeEnumHandler"/>
</sqlMapConfig>[/java]

关于单独设置:
首先需要在<sqlMap>中设置parameterMap,在设置parameter时指定typeHandler对应处理类,接下来需要设置resultMap,对应的属性也需要添加typeHandler

PS:MyBatis在处理Enum时较IBatis要方便,可以使用泛型。本人也刚刚开始接触,如果有不正确的地方欢迎指正交流。

ibatis的$与#区别

在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型匹配,而$不进行数据类型匹配,例如:
[sql]
select * from table where id = #id# [/sql]
其中如果字段id为字符型,那么#id#表示的就是’id’类型,如果id为整型,那么#id#就是id类型。
[sql]
select * from table where id = $id$ [/sql]
如果字段id为整型,Sql语句就不会出错,但是如果字段id为字符型,那么Sql语句应该写成
[sql]
select * from table where id = ‘$id$’
[/sql]
在做in,like 操作时候要特别注意
总结如下:
1.#是把传入的数据当作字符串,如#field#传入的是id,则sql语句生成是这样,order by “id”,这当然会报错..
2.$传入的数据直接生成在sql里,如#field#传入的是id,则sql语句生成是这样,order by id, 这就对了.
3.#方式能够很大程度防止sql注入.
4.$方式一般用于传入数据库对象.例如传入表名.
5.一般能用#的就别用$.
6.#使用的是PreparedStatement机制,生成的SQL字符串中含有很多?,这些?会被动态的添加参数进去查询$中的变量好比字符串直接替换。
PS:
#变量名# 会转化为 jdbc 的 ?,比如
[sql] select * from user where name=#name# [/sql]
会转化为jdbc的
[sql] select * from user where name=?[/sql],
把?参数设置为name的值,而$变量名$就直接把 $name$替换为 name的内容,也就是由可能
[sql]select * from user where name=$name$ [/sql]
如果name为 “‘ ‘ or 1 = 1”, 那么这样就有可能导致sql注入,所以ibatis用#比$好,不会造成sql注入。