Mybatis全局配置

 

                                         Mybatis全局配置   

项目的测试环境

编译器:IDEA

框架:SpringMVC 只是创建了这个工程没有结合Spring联合配置参数

数据库:mysql

数据库框架:Mybatis

文件结构

mabatis-config.xml中的参数全局配置

其下有许多标签参数,参数有先后顺序。最好按照这个来

"(properties?,settings?,typeAliases?,typeHandlers?,
objectFactory?,objectWrapperFactory?,reflectorFactory?,
plugins?,environments?,databaseIdProvider?,mappers?)"


1.properties

我们这里是配置了链接了数据库的参数

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=1234
  <!--配置数据库信息 resource是数据库路径-->
    <properties resource="db.properties"></properties>
   
  
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!-- 配置数据库连接信息 -->
            <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>
        </environment>
    </environments>

2.settings
设置mabatis的一些内容如:debug级别的日志都是很多
设置日志,二级缓存,延迟加载
在控制台打印
<settings>
    <setting name="logImpl" value="STDOUT_LOGING">
</settings>


3.typeAliases别名

取别名都是为了简化工作,我们写一个类需要写一大堆的包名全路径比较麻烦我们去取别名简化,名字不要乱取,最好都是类名首字母的大小写如User或user

有两种取别名的方式:

<1>单个别名

相应的类给出全路径,在<mapper>中直接引用product即可,参数或返回值类型都可以应用

 <!--设置别名-->
    <typeAliases>
        <!--单个取别名对应mapper中的参数 ,其实mybatis有许多默认的别名-->
      <typeAlias type="com.wlxy.pojo.Product" alias="product" />-
    </typeAliases>

在mapper-config.xml文件中别名作为参数或返回值类型引用,


 <select id="findByIdAndName" parameterType="product" resultType="product" >
        select *from t_user  where pid=#{pid} and  pname=#{pname};
    </select>

<2>批量取别名

<typeAliases>
     
        <!--批量取别名,则该类下面的所有类名首字母大小写均可-->
        <package name="com.wlxy.pojo"/>
    </typeAliases>

包下的类名mybatis默认是类的首字母大或小写均可如Product或product

mybatis默认别名如下表

                                                        mybatis默认别名

别名

映射的类型

_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

 

 4.typeHandlers类型处理器
mabatis就是通过这个处理器完成java类型和数据库类型的转换,mabatis
内部已经定义了足够多的的类型供我们使用,不需要自己定义。


5.plugins插件 使用插件需要配置


6.environments 底层运行环境

 

7、mapper 关联接口实现类 

有三种关联方式

1、关联单个类

<mappers >
        <!--<mapper resource="com/wlxy/mapper/userMapper.xml"/>-->
    </mappers>

2、使用类加载器

使用这个方式需要满足两个条件

(1)实现了接口文件和实现了接口的xml文件在同一目录

(2接口文件和xml文件的名称相同

此时是使用类加载器跟单个的引用文件不同这里使用的是class=“接口路径”

<mappers>
        <mapper class="com.wlxy.mapper.userMapper"/>
    </mappers>

 3、批量关联

使用批量关联的前提是满足类加载的两个条件,此时引入包时name="需要加载的类的包路径"

    <mappers>
        <package name="com.wlxy.mapper"/>
    </mappers>

 

输入参数和输出参数(Mapper-config.xml

后面说一个在Mapper-config.xml中 mapper当中输入参数和输出参数的处理

简单类型:int,string,double
        几个参数类型是一样的时候,通过下标#{0},#{1}

         多个参数类型不一样时:

如:

//这是原代码
public Product findByIdAndName(int pid,String pname);
//我们可以引入@Param("")
public Product findByIdAndName(@Param("pid")int pid, @Param("pname")String pname);

//在mapper中引用,不在需要写参数类型
 <select id="findByIdAndName"  resultType="product" >
        select *from t_user  where pid=#{pid} and  pname=#{pname};
    </select>

其实这种方式很不好,传递两个参数太麻烦了,我们直接传递一个对象更加的方便。

 

    实体类:直接写路径,

 <select id="findById" parameterType="java.lang.Integer" resultMap="com.wlxy.pojo.Product">
        select  pid,pname,price from  t_user where pid=#{0};
    </select>

当然我们也是可以使用mybatis默认的别名和自己引入别名

 <select id="findById" parameterType="int" resultMap="product">
        select  pid,pname,price from  t_user where pid=#{0};
    </select>

 包装类:这里使用Product作为参数封装User对象,使用user的属性Product的参数时,

直接使用属性名.被封装对象属性名
<!--这里使用Product作为参数封装User对象,使用user的属性Product的参数时,直接使用属性名.被封装对象属性名-->
<select id="findUser" parameterType="user" resultType="product">
    select *from  t_user where  pid=#{product.pid} and pname=#{product.pname};
</select>

 

parameterMap:不推荐使用

输出映射

 <select id="findById" paramemter="int" resulttype="product">
     select pid,pname, price from t_user where pid=#{0};
    </select>

 

   此时我们是查询结果需要得到pid、pname、price这三个属性,查询条件是pid,
    查询出来的列名必须和类(Product)属性名一致,如果不一致,映射不一致的那个映射失败,
    只要存在一个属性名一致都会创建对象,返回存在存在映射的结果,映射对不上的返回类的默认值,如果全部不一致,对象都不创建,直接传回null
  
 

关系映射

当Product的类属性名和数据库列名不一致时我们需要使用关系映射

public class Product {
    private int id;
    private String name;
    private int price;
}


   此时数据库查询结果的类型和积累类型不一致,导致在返回结果时无法完成关系映射。

 <resultMap type="product" id="resultProduct">
    <!--id表示主键-->
    <id column="pid" property="id"/>
    <result column="pname" property="name"/>
    <result column="pid" property="id"/>
</resultMap>
    <!--此时输出结果的类型为pid,pname,price需与输出类型Product的属性匹配,才可以正确映射-->
    <!--两边参数名在不相同的情况下我们需要手动映射两者的关系-->
    <select id="findById" parameterType="int" resultMap="resultProduct">
        select  pid,pname,price from  t_user where pid=#{0};
    </select>

在<resultMap>中

type="product":映射的基类 (前面使用了别名product也就是Product类)
id="resultProduct"  :用于后面的resultMap引用 

<id column="pid" property="id"/>   id:表示数据库中的主键
<result column="pname" property="name"/>   result:普通标签

下面是查询语句引用,需要使用resultMap作为返回值类型,resultMap中的id作为映射关系的引用

 <select id="findById" parameterType="int" resultMap="resultProduct">
        select  pid,pname,price from  t_user where pid=#{0};
    </select>

 

写的很乱。。。。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值