MyBatis

1、在pom.xml配置中配置mybbatis相关依赖 以及对应的mysql

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

2、resources->创建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>
<!--   编写多个环境,指定运行时的环境-->
    <!--
     可以点击configuration查看
   一般配置顺序:properties、settings、typeAliases、plugins(插件)、environments、mappers
 -->
<!--    导入db.properties 里面配置环境数据-->
    <properties resource="db.properties"/>
    <typeAliases>
<!--        在mapper.xml中可以不用使用全路径-而是映射entity下对应的对象-->
        <package name="com.wsj.entity"/>
    </typeAliases>
    <plugins>
<!--     拦截器 对目标代码进行拦截,并进行修改 这里配置一个分页拦截器 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
    <environments default="MySQL">
<!--     编写一个环境,由id作为唯一标识  -->
        <environment id="MySQL">
<!--   配置事务管理器, type是指定事务的管理方式  JDBC自己管-->
            <transactionManager type="JDBC"></transactionManager>
<!--   Druid 连接池-->
            <dataSource type="com.wsj.util.MyDruidDataSourceFactory">
                <property name="driverClass" value="${driver}"/>
                <property name="jdbcUrl" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
<!--        核心配置文件要引入映射文件-->
        <package name="com.wsj.mapper"/>
    </mappers>
</configuration>

3、配置db.properties->driver\url\username\password

      Mapper.xml要路径要对应相应的mapper

      配置好了推荐安装插件free 极大提高我们要执行mapper的速度

4、log4j的配置(日志查看)

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

resources->log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

5、mapper.xml相关语句参数传递方法

i:序号传参->查询多个,id=#{param1}  id2=#{param2}

ii:map传参->传递map集合中的key       id= #{key1}    #{key2}

iii:注解传参 ->

List<Student> selectByGenderAndAddress(@Param("gen") String stuGender, @Param("add") String stuAddress);

id=#{gen} #{add}

i4:模糊传递时传参 ${}是整合编译sql语句,可以被注入攻击

afhk or 1=1; #

#{}是 预编译sql语句,用?进行代替,防止注入攻击

${}是预编译sql 语句

like '%${value}%'

like concat('%',#{stuName},'%'};

i5:实体类传参

#{实体类属性1} #{实体类属性2} #{实体类属性3}

6、动态sql

1、sql + include

sql中设置命名id以及要重复的sql语句(以后常用的sql语句进行拼接)

2、where + if 实现动态拼接

<where></where>标签进行sql where选择,可以判断是否使用or\and\where

<if ></if>判断属性是否需要动态拼接

    <select id="selectByCondition" resultMap="StudentGradeMap">
        <include refid="StudentSelect"/>
        <where>
            <if test="stuGender!=null">
                stuGender=#{stuGender}
            </if>
            <if test="stuAddress!=null">
                and stuAddress=#{stuAddress}
            </if>
        </where>
    </select>

3、set + if 动态拼接sql 进行set的语句

<set></set> 标签可以对sql set实现动态拼接,可以判断是否使用 ,

<if ></if>判断属性是否需要动态拼接

    <update id="updateBySet">
        update student
        <set>
            stuId=#{stuId},
            <if test="stuName!=null and stuName !=''">
                stuName=#{stuName},
            </if>
            <if test="stuGender!=null">
                stuGender=#{stuGender},
            </if>
        </set>
        where stuId=#{stuId}
    </update>

4 foreach 实现 sql in(实现枚举效果)

    <select id="selectByIn" resultMap="StudentGradeMap">
        <include refid="StudentSelect"/>
        where stuId in
--         集合类型 开始符合,结束符合 分开方法 每个元素命名id
        <foreach collection="array" open="(" close=")" separator="," item="stuId">
--             对应array数组中单个属性名中的属性
            #{stuId}
        </foreach>
    </select>

5、

trim + if:where和set实现原理

        suffix在末尾添加元素

        prefix在前缀添加元素,prefixOverrides进行要忽略的部分

  • trim:prefix,suffix,prefixOverrides,suffixOverrides

  •     <select id="selectByCondition" resultMap="StudentGradeMap">
            <include refid="StudentSelect"/>
            <trim prefix="where" prefixOverrides="and|or">
                <if test="stuGender!=null">
                    stuGender=#{stuGender}
                </if>
                <if test="stuAddress!=null">
                    and stuAddress=#{stuAddress}
                </if>
            </trim>
        </select>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值