??

作者: 别叫我小新 | 来源:发表于2018-09-07 08:53 被阅读0次

    问题

    resultType 和resultMap的差别

    "#和$的区别

    mybatis使用,先导包,,在配置环境 mybatis是在java环境下运行

    在src下面建xml文件,命名为mybatis.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>
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <property name="driver" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
    </configuration>
    
    那种$的写法感觉很麻烦,还得配个properties文件, image.png

    再在mybatis.xml中进行扫描,
    <properties resource="jdbc.properties"></properties>
    感觉有点麻烦了,还是下面这样的直接写的比较省事吧

    image.png

    在mybatis中还可以起别名

    <!-- 设置类的别名 -->
        <typeAliases>
            <typeAlias type="com.qianfeng.demo1.Person" alias="Person"/>
        </typeAliases>
    

    这样mybatis.xml就配置完了

    配置PersonMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="org.mybatis.example.BlogMapper">
    <select id="selectBlog" parameterType="int" resultType="Blog">
    select * from Blog where id = #{id}
    </select>
    </mapper>
    
    <!-- 
    对于mybatis,传过来的参数本质上都封装为map
    如果是基础类型,key值随便写
    如果是实体类对象,key值是对象的属性(本质上是get方法中get后面的内容)
    如果是数组,key值是array
    如果是List,key值是list
    如果是map,key/value是程序设置的值
    
    如果传过来的参数是数组,映射文件中使用map类型,key值是array -->
    

    相关文章

      网友评论

          本文标题:??

          本文链接:https://www.haomeiwen.com/subject/xvyxgftx.html