1. mybatis自动开启驼峰命名
添加mybatis-config.xml文件,并且配置在sqlSessionFactoryBean下
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/family/core/dao/sqlMapper/*.xml"></property> z
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
mybatis-config.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
2. mybatis 动态sql
where if 语句
<select id="getListOfSummary" resultType="BookEntity" parameterType="BookEntity" >
select
ID,
TITLE,
AUTHOR,
CREATED_TIME,
UPDATED_TIME,
USER,
CHANNEL,
SYSTEM,
STATUS,
CATEGORY,
SEQ
from
book
<where>
<if test="title!=null and title !=''">
and title like concat(concat("%",#{title}),"%")
</if>
<if test="author!=null and author !=''">
and author like concat(concat("%",#{author}),"%")
</if>
<if test="category!=null and category !=''">
and category = #{category}
</if>
</where>
<if test="sort !=null and order !=null">
order by ${sort} ${order}
</if>
<if test="offset!=null and limit!=null and limit!=''">
limit #{offset},#{limit}
</if>
</select>
3. 别名问题
mybatis自带别名,左侧是别名,右侧为对应的parameterType类型,基本类型及包装类注意区分
_byte byte
_long long
_short short
_int int
_integer int
_double double
_float float
_boolean boolean
string String
byte Byte
long Long
short Short
int Integer
integer Integer
double Double
float Float
boolean Boolean
date Date
decimal BigDecimal
bigdecimal BigDecimal
自定义别名,在mybatis-config文件中定义
<typeAliases>
<!--
单个别名的定义
alias:别名,type:别名映射的类型 -->
<typeAlias type="cn.itcast.mybatis.po.User" alias="user"/>
<!-- 批量别名定义
指定包路径,自动扫描包下边的pojo,定义别名,别名默认为类名(首字母小写或大写)
-->
<package name="cn.itcast.mybatis.po"/>
</typeAliases>
4. order by 问题 $与#的区别
# 会自动转成字符串,加上双引号,类似于preparedStatement
$不转义,传原始值,在用 order by 语句时候需要用 ${sort} ${order} statement ,见上例