Mybatis配置相关

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 ,见上例














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值