核心配置文件详解
properties-引入驱动信息
在resources文件夹下创建jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=123456
在mybatis-config.xml核心配置文件中
<!--引入properties文件,此时就可以${属性名}的方式访问属性值--><properties resource="jdbc.properties"></properties><dataSource type="POOLED"><!--设置驱动类的全类名--><property name="driver" value="${jdbc.driver}"/><!--设置连接数据库的连接地址--><property name="url" value="${jdbc.url}"/><!--设置连接数据库的用户名--><property name="username" value="${jdbc.username}"/><!--设置连接数据库的密码--><property name="password" value="${jdbc.password}"/> </dataSource>
typeAliases-简化全限定名
1、将实体类一个一个进行注册
<typeAliases><!--typeAlias:设置某个具体的类型的别名type:需要设置别名的类型的全类名alias:设置此类型的别名,若不设置此属性,该类型拥有默认的别名,即类名且不区分大小写若设置此属性,此时该类型的别名只能使用alias所设置的值 --><typeAlias type="com.guang.mybatis.entity.User"></typeAlias>#默认别名为User<typeAlias type="com.guang.mybatis.entity.User" alias="abc"></typeAlias> #别名为abc</typeAliases>
2、将整个包下的实体类全部设置别名为默认
<typeAliases><!--以包为单位,设置改包下所有的类型都拥有默认的别名,即类名且不区分大小写--><package name="com.guang.mybatis.entity"/> </typeAliases><typeAliases>
此时的xml文件中全限定名即可省略
<insert id="insertUser" parameterType="User"></insert>
mapper-将配置文件注册
注意:此时xml文件与dao接口所在的包名必须一致
例如:com,guang.mybatis.UserDao与com/guang/mybatis/UserMapper.xml
<mappers><mapper resource="UserMapper.xml"/><!--以包为单位,将包下所有的映射文件引入核心配置文件注意:此方式必须保证mapper接口和mapper映射文件必须在相同的包下 --><package name="com.guang.mybatis.mapper"/> </mappers>
动态SQL
动态SQL-if
注意:当name==null或者name==’’时,sql语句会报错,所以得加上1=1的恒成立条件
<!--List<Emp> getEmpListByMoreTJ(Emp emp);--><select id="getEmpListByConditional" resultType="Emp">select * from t_emp where 1=1<if test="name != '' and name != null">and name = #{name}</if><if test="age != '' and age != null">and age = #{age}</if><if test="sex != '' and sex != null">and sex = #{sex}</if><if test="eamil != '' and eamil != null">and eamil = #{eamil}</if></select>
动态SQL-where
在where标签中,当条件不满足时,前面的的and或者or会被自动去掉,满足条件时,若条件前没有添加and,会自动加上and,所有条件都不满足时,where标签也会被自动去掉。
当and或者or在后面时,sql语句会报错。例如:age = #{age} and
<!--List<Emp> getEmpListByMoreTJ(Emp emp);--><select id="getEmpListByConditional" resultType="Emp">select * from t_emp<where><if test="name != '' and name != null">name = #{name}</if><if test="age != '' and age != null">and age = #{age}</if><if test="sex != '' and sex != null">and sex = #{sex}</if><if test="eamil != '' and eamil != null">and eamil = #{eamil}</if></where></select>
动态SQL-trim
疑问:添加上where关键字还是where标签?where标签
prefix、suffix ====》在前面或者后面添加。。。
prefixOverrides、suffixOverrides ====》在前面或者后面去掉。。。
例子:
在满足条件的if语句前加上where,去掉后面的and或者or
<!--List<Emp> getEmpListByMoreTJ(Emp emp);--><select id="getEmpListByConditional" resultType="Emp">select * from t_emp<trim prefix="where" suffixOverrides="and|or"><if test="name != '' and name != null">name = #{name}</if><if test="age != '' and age != null">age = #{age} and</if><if test="sex != '' and sex != null">sex = #{sex} and</if><if test="eamil != '' and eamil != null">eamil = #{eamil} and</if></trim></select>
动态SQL-choose、when、otherwise
choose、when、otherwise 对应java中的 if….else if…..else
when至少要有一个,otherwise最多只能有一个
