mybatis需要注意的几点

官方网站  http://www.mybatis.org/mybatis-3/zh/configuration.html#typeHandlers

对于mybatis中字段名和model中名不一致的问题:

1.select user_name as username from users;

2.<resultMap id=“userResultMap” type="user">

<id column="user_name" property="username"/>

</resultMap>

3.mybatis 对于字段user_name 到属性 username ,mybatis能自动识别装配 。

<setting name="mapUnderscoreToCamelCase" value="true"/> 
<!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认为false-->

@Param注解

int updateUserById (@Param("id") Integer id,@Param("name") String name);
<update id="updateUserById"> <!--注解之后不需要指定参数-->
  update
    users
  set
    name = #{name}
  where id = #{id}
</update>

int updateUser(Map<String,Object> params);
<update id="updateUser"  parameterTYpe="map"> 
  update
    users
  set
    name = #{name}
  where id = #{id}
</update>

4.<sql ></sql>

<sql id="selectBaseField">
  id as id,
  user_name as userName,
  password as password,
  age as age,
  create_time as createTime
</sql>

<select id = "selUser" resultType="User">
  select
  <include refid="selectBaseField"/>
  from
  users
</select>

5.动态sql

<select id="selectUser" resultType="user">
  select
  <include refid="selectFields"/>
  from
  user
  <where>
    <if test="user_name != null and user_name !='' ">
        and user_name = #{username}          <!--会自动去掉第一个符合条件的 and-->
    </if>
    <if test="age != null and age !='' ">
        and age = #{age}         
    </if>
  </where>
</select>


<update id="updateUser"  parameterTYpe="user"> 
  update
    users
  <set>
     <if test="age != null and age > 18 ">
        and age = #{age} ,        <!--会自动去掉最后一个 ,-->
    </if>
  </set>  
</update>


<insert id="addUser" parameterType="java.util.List">
    insert into users
    (
    user_name,
    age,
    password
    )values
    <foreach collection="list" item="item" separator=","> 
    <!-- <foreach collection="list" item="item" separator="," close=")" open="(">-->
        (
        #{item.username},
        #{item.age},
        #{item.password},
        )
    </foreach>
</insert>

6.关于逻辑分页和物理分页

逻辑分页是一次查询出所有结果,然后对resultSet 结果进行处理,设计算法去分页

物理分页是通过 查表的时候 limit 行数

7.<mvc:mapping path="/**"/> //这里注意,path需要配置成path="/**" , /**的意思是所有文件夹及里面的子文件夹 /*是所有文件夹,不含子文件夹 /是web项目的根目录

8.MyBatis 插入空值时,需要指定JdbcType.如#{name,jdbcType=VARCHAR}

在执行SQL时MyBatis会自动通过对象中的属性给SQL中参数赋值,它会自动将Java类型转换成数据库的类型。而一旦传入的是null它就无法准确判断这个类型应该是什么,就有可能将类型转换错误,从而报错。

      <if test="condition.createTimeTo != null">
        and create_time &lt; #{condition.createTimeTo,jdbcType=TIME}
      </if>

MyBatis 通过包含的jdbcType类型

BIT         FLOAT      CHAR           TIMESTAMP       OTHER       UNDEFINED

TINYINT     REAL       VARCHAR        BINARY          BLOB        NVARCHAR

SMALLINT    DOUBLE     LONGVARCHAR    VARBINARY       CLOB        NCHAR

INTEGER     NUMERIC    DATE           LONGVARBINARY   BOOLEAN     NCLOB

BIGINT      DECIMAL    TIME           NULL            CURSOR

### 整合Spring Boot与MyBatis所需Maven依赖 为了使Spring Boot项目成功整合MyBatis,在`pom.xml`文件中至少应包含如下几个核心依赖: #### MyBatis-Spring Boot Starter 此依赖项提供了用于简化MyBatis配置的支持,使得开发者可以更方便地在Spring应用程序上下文中使用MyBatis。 ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>版本号需根据实际情况填写</version> </dependency> ``` #### MySQL Connector Java 对于采用MySQL数据库的情况,还需加入对应的JDBC驱动程序以建立连接。注意实际应用时应当依据所使用的具体数据库调整该部分配置[^1]。 ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 以上两个是最基本的要求;除此之外,视项目需求可能还会涉及到其他辅助工具类库的选择,比如Lombok等可选组件可以帮助减少样板代码的编写工作量[^2]。 值得注意的是,在设置这些依赖的同时也要确保项目的构建路径以及IDE环境变量正确无误,这样才能有效避免诸如`BindingException`这样的运行期异常错误的发生[^3]。 最后提醒一点,当遇到类似于`Property &#39;sqlSessionFactory&#39; or &#39;sqlSessionTemplate&#39; are required`这类启动失败提示的时候,则可能是由于缺少必要的Bean定义或者属性注入不完全引起的,此时应该仔细检查相关配置文件中的设定是否恰当[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值