笔记---Mybatis

本文详细解读了Mybatis的基础配置,包括mybatis-config.xml的设置、Mapper.xml的编写,以及如何利用动态SQL进行条件查询。介绍了事务管理、参数传递和自动生成映射文件的方法,还涉及到了环境配置的优化和动态SQL标签的使用技巧。
摘要由CSDN通过智能技术生成

Mybatis

一、配置

1.1 配置文件

  1. 生成配置文件: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>
  <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>
  • <transactionManager 事务管理

1.2 Mapper.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="com.example.eat.dao.mapper.ClientMapper">
  <resultMap id="BaseResultMap" type="com.example.eat.dao.pojo.Client">
    <id column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="phonenumber" jdbcType="VARCHAR" property="phonenumber" />
    <result column="head_portrait" jdbcType="VARCHAR" property="headPortrait" />
    <result column="reputation" jdbcType="VARCHAR" property="reputation" />
  </resultMap>
  <select id="selectByUserId" parameterType="com.example.eat.dao.pojo.Grope" resultMap="GroupResultMap">
    SELECT * FROM food join menu on food.menu_id = menu.menu_id where user_id = #{userId,jdbcType=INTEGER}
  </select>
  • mapper接口的路径
  • result:
    • resultType:返回单条
    • resultMap: 返回集合
  1. select:

    • id: 接口方法名

    • resultType:sql语句执行的返回值

    • parameterType: 参数类型

      • SELECT * FROM food join menu on food.menu_id = menu.menu_id where user_id = #{userId,jdbcType=INTEGER}
        
  2. insert:

    • 没有返回值

1.3 万能Map

map当参数

1.4 模糊查询

参数传递通配符%李%

1.5 generator

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--数据库驱动地址-->
    <classPathEntry
            location="G:\working\Maven\localRepo\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar"/>

    <context id="DB2Tables" targetRuntime="Mybatis3">
        <commentGenerator>
            <property name="suppressData" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--驱动、数据库、账号‘密码-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/eat-what"
                        userId="root"
                        password="haorui1996">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成的包名、要生成包所在的位置-->
        <!--模型-->
        <javaModelGenerator targetPackage="pojo" targetProject="src/main/java/com/example/eat/dao"/>
        <!--映射-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <!--dao类位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject="src/main/java/com/example/eat/dao"/>
        <!--生成的表及其对应的类名-->
        <table tableName="client" domainObjectName="Client"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               enableUpdateByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="menu" domainObjectName="Menu"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               enableUpdateByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="food" domainObjectName="Food"
               enableCountByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               enableUpdateByExample="false"
               selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

二、配置优化

2.1 配置环境

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ropkrm8-1652711597965)(C:\Users\70402\AppData\Roaming\Typora\typora-user-images\image-20220117232058918.png)]

三、动态SQL

  1. if

    //查询user表,如果查询title不为空则按照给定title查询,否则查询所有
    <select id="queryBlogIF" parameterType="map" resultType="blog">
    	select * form user where 1=1
        <if test="title !=null">
            title = #{title}
    </select>
    
  2. where

    //sql语句where后面不能直接跟and 所以使用where标签,当匹配到第二个时自动删除and
    <select id="selectIf" parameterType="map" resultType="map">
        select * from customer
        <where>
            <if test="id != null">
                id = #{id}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
        </where>
    </select>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值