mybatis

作者: luckee | 来源:发表于2019-02-18 00:14 被阅读0次

    #{}${}的区别

    #{}${}的区别

    • #{}使用预编译机制,在传参数之前,对SQL语句进行预编译,#{}使用?代替,传参后,将#{}的值放在单引号中,取代?
      select * from user where name = #{name};-->select * from user where name = ?;
      如果name的值传进来是“jack”,那么最终变成select * from user where name = 'jack';
    • ${}在预编译之前,将${}的值不加单引号直接和SQL语句拼接后,再进行编译,会造成SQL注入的安全问题
      select * from ${tableName} where name = #{name}
      假如,我们的参数 tableName 为 user; delete user; --,那么 SQL 动态解析阶段之后,预编译之前的 sql 将变为:
      select * from user; delete user; -- where name = ?;
      -- 之后的语句将作为注释,不起作用,因此本来的一条查询语句偷偷的包含了一个删除表数据的 SQL。

    mapper.xml文件位置

    • 传统的和mapper接口放在一起
    <!-- 将sql映射注册到全局配置中-->
        <mappers>
    
            <!--
                mapper 单个注册(mapper如果多的话,不太可能用这种方式)
                    resource:引用类路径下的文件
                    url:引用磁盘路径下的资源
                    class,引用接口
                package 批量注册(基本上使用这种方式)
                    name:mapper接口与mapper.xml所在的包名
            -->
    
            <!-- 第一种:注册sql映射文件-->
            <mapper resource="com/spldeolin/mapper/UserMapper.xml" />
    
            <!-- 第二种:注册接口   sql映射文件必须与接口同名,并且放在同一目录下-->
            <!--<mapper class="com.spldeolin.mapper.UserMapper" />-->
    
            <!-- 第三种:注册基于注解的接口  基于注解   没有sql映射文件,所有的sql都是利用注解写在接口上-->
            <!--<mapper class="com.spldeolin.mapper.TeacherMapper" />-->
    
            <!-- 第四种:批量注册  需要将sql配置文件和接口放到同一目录下-->
            <package name="com.spldeolin.mapper" />
    
        </mappers>
    
    • 和mapper接口分离,和其他配置文件一样,放在resource文件夹
      在spring-dao.xml文件中配置sqlSessionFactory.mapperLocations指定mapper.xml的路径,mapperScannerConfigurer.basePackage指定mapper接口的包名。参考
      image.png
    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--DB配置文件-->
        <context:property-placeholder location="classpath:db.properties"
                ignore-unresolvable="true" />
    
        <!--数据源-->
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
    
        <!--qlSessionFactory-->
        <bean id="sqlSessionFactory"
                class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!--Mybatis配置文件-->
            <property name="configLocation"
                    value="classpath:mybatis-config.xml" />
            <!--mapper.xml所在位置-->
            <property name="mapperLocations" value="classpath:mapper/*Mapper.xml" />
            <!--指定需要使用别名的PO类所在的包-->
            <property name="typeAliasesPackage"
                    value="com.spldeolin.demoapp.po" />
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--mapper接口所在的包-->
            <property name="basePackage" value="com.spldeolin.demoapp.dao" />
        </bean>
    
    </beans>
    

    相关文章

      网友评论

          本文标题:mybatis

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