log4j 显示mybatis日志sql语句
1.在根目录(src或resources)下创建 log4j.properties 文件
2.log4j.properties文件内容
#DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n
另一种配置方式: 可以屏蔽掉其他的框架信息
#DEBUG < INFO < WARN < ERROR < FATAL
### set log levels ###
log4j.rootLogger=DEBUG, stdout
###显示mybatis的SQL语句部分,类似于hibernate在控制台打印sql语句那部分
log4j.logger.org.apache=ERROR
#log4j.logger.org.hibernate=ERROR #没用到,屏蔽
log4j.logger.org.springframework=ERROR
log4j.logger.org.mybatis=DEBUG
log4j.logger.org.apache.http=ERROR
log4j.logger.com.netflix=ERROR
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
3.在pom.xml中引入 log4j 的jar包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
4.添加配置信息到mybatis-config.xml:
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
xml中的特殊字符
-
xml中的特殊字符 如 小于 “ < ” 为 xml 中标签符号,所以需要转译
-
另一种写法:不需要转译
<![CDATA[
sql语句
]]>
Mapper.xml使用
<!--properties配置文件导入-->
<properties resource="properties/db.properties"></properties>
<!--使用直接:${properties文件中对应参数名}-->
<!--resultMap 支持继承,减少代码的冗余度,提高可重用性和可维护性-->
<resultMap id="随便命名" type="com.gxa.entity.User" extends="继承的mapper的Id名">
<id property="实体属性名" column="数据库字段名"></id>
<result property="属性名" column="数据库字段名"></id>
<result property="属性名" column="数据库字段名"></id>
</resultMap>
<!--resultType
resultType:设置的是单条的记录类型
resultType:可以直接映射基本数据类型
resultType:默认要求数据库中的字段名和对象属性名一致,才能自动映射
-->
<select id="接口方法名" resultType="com.gxa.entity.User">
<!--sql语句-->
select * from t_user where userId=#{id}
</select>
<select id="接口方法名" resultMap="配置的mapper值">
<!--sql语句-->
select * from t_user where userId=#{id}
</select>
<select id="接口方法名" parameterType="参数类型" resultMap="配置的mapper值">
<!--
${value}采用的是statement执行方式,使用的是sql拼接技术。value不能修改为其他名字。
#{name} 采用的是preparedStatement的执行方式,使用的是占位符的方式
-->
select * from t_user where userName like '%${value}%'
</select>
<!--insert语句执行后的主键回显-->
<insert id="insert" parameterType="Course" useGeneratedKeys="true" keyProperty="courseId">
insert语句
</insert>
<insert id="方法名" paramterType="参数类型">
<selectKey keyProperty="返回到那个属性上" resultType="返回值类型(int)" order="AFTER/BEFORE">
select @@identity
</selectKey>
insert语句
</insert>
<!--定义一个全局sql片段-->
<sql id="sql片段命名" >
sql语句
</sql>
<!--引用sql片段,放在select之类的标签之中-->
<include refid="应用的sql片段id名"></include>
<!--配置在mybatis-config.xml中-->
<typeAliases>
<!--单个取别名-->
<typeAlias type="com.gxa.entity.User" alias="User"></typeAlias>
<!--对包中所有的类都支持别名-->
<package type="com.gxa.entity"/>
</typeAliases>
where 元素
- where 条件由mybatis 自行添加。
特别适合多条件筛选的拼接
当 where 标签中,有字符串时,才会在 sql 语句后 添加 where 的关键字
而且,如果有字符串,还会判断该条件是否是第一个条件,如果是第一个条件 会删除前面的 连接符号(and 、 or)
<select id="selectUsersByMap2" resultType="User">
select * from t_user
<where>
<if test="username != null and username != '' ">
and username = #{username}
</if>
<if test="password != null and password != '' ">
and password = #{password}
</if>
<if test="age != null and age > 0 ">
and age = #{age}
</if>
</where>
</select>
一对一关系配置
<resultMap type="对象类型全路径com.entity.Role" id="resultMap命名">
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
<association property="属性名" javaType="属性对应实体类全路径com.entity.User">
<!-- Role 对应的 t_role 表中的主键字段 -->
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
</association>
</resultMap>
注意:双向关联中,toString 所以会调用 关联 对象的 toString。如果 关联对象 中的 toString 也在调用 对方实体对象的 toString 死循环了,堆栈溢出 , 让其中一方不要调用对方的 toString
一对多关系配置
<!--方法一-->
<resultMap type="对象类型全路径com.entity.Role" id="resultMap命名">
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
<!-- 1 对 多 关系配置 -->
<collection property="属性名" ofType="属性对应实体类全路径com.entity.User">
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
</collection>
</resultMap>
<!--方法二-->
<resultMap type="对象类型全路径com.entity.Role" id="resultMap命名">
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
<!-- 1 对 多 关系配置 -->
<collection property="属性名" resultMap="引用的resultMap的ID userMap"></collection>
</resultMap>
<resultMap type="属性对应实体类全路径com.entity.User" id="resultMap命名 userMap">
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
</resultMap>
配置懒加载
<settings>
<!--开启懒加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--按需积极加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--指定哪个对象的方法触发一次延迟加载,即开始加载。value为空则没有方法会触发加载-->
<setting name="lazyLoadTriggerMethods" value=""/>
</setting>
<!--应用mapper.xml文件中:-->
<!--关联查询,延迟加载(懒加载)-->
<select id="selectUserByCountry" resultType="对象类型全路径com.entity.User">
select userId,userName from t_user where countryId=#{xxx}
</select>
<resultMap type="对象类型全路径com.entity.User" id="resultMap命名 countryMapper">
<id column="主键列" property="主键列对应实体类属性名"/>
<result column="列名" property="列对应实体类属性名"/>
<!-- 1 对 多 关系配置 -->
<collection property="属性名"
ofType="属性对应实体类全路径com.entity.Country"
select="调用的查询语句id"
column="查询作为的传入参数"
></collection>
</resultMap>
<!--主查询,直接加载-->
<select id="selectCountryById" resultMap="引用的resultMap的ID countryMapper">
select cid,cname from t_country where cid=#{temp}
</select>
二级缓存(默认mybatis中开启了二级缓存)=>不推荐使用
<!--二级缓存是跨session的,使用同一个缓存空间在session之间共享数据-->
<settings>
<setting name="cacheEnabled" value="true"></setting>
</settings>
<!--在mapper.xml中配置-->
<cache/><!--开启此mapper操作的二级缓存-->
<!--entities中的类需要实现Serializable接口,让对象支持序列化-->