mybatis-mybatis-plus xml方式update语句、mybatis的基本工作流程(2.0)

mybatis-mybatis-plus xml方式update语句

    <update id="updateById">
        update collection_shop
        <trim prefix="set" suffixOverrides=",">
            <if test="merchantId != null">
                merchant_id = #{merchantId},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="name != null and name != ''">
                `name` = #{name},
            </if>
            <if test="type != null">
                `type` = #{type},
            </if>
            <if test="door != null and door != ''">
                door = #{door},
            </if>
            <if test="openStatus != null">
                open_status = #{openStatus},
            </if>
            <if test="openStatusDate != null and openStatusDate != ''">
                open_status_date = #{openStatusDate},
            </if>
            <if test="shopType != null">
                shop_type = #{shopType},
            </if>
            <if test="coverArea != null and coverArea != ''">
                cover_area = #{coverArea},
            </if>
            <if test="rentMonthAmount != null and rentMonthAmount != ''">
                rent_month_amount = #{rentMonthAmount},
            </if>
            <if test="longitude != null">
                longitude = #{longitude},
            </if>
            <if test="latitude != null">
                latitude = #{latitude},
            </if>
            <if test="provinceCode != null and provinceCode != 0">
                province_code = #{provinceCode},
            </if>
            <if test="province != null and province != ''">
                province = #{province},
            </if>
            <if test="cityCode != null and cityCode != 0">
                city_code = #{cityCode},
            </if>
            <if test="city != null and city != ''">
                city = #{city},
            </if>
            <if test="region != null and region != ''">
                region = #{region},
            </if>
            <if test="regionCode != null">
                region_code = #{regionCode},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="gmtModifiedUid != null">
                gmt_modified_uid = #{gmtModifiedUid},
            </if>
            <if test="gmtModifiedBy != null">
                gmt_modified_by = #{gmtModifiedBy},
            </if>
            <if test="gmtModified != null">
                gmt_modified = #{gmtModified},
            </if>
        </trim>
        WHERE id = #{id}
    </update>

mybatis的基本工作流程(2.0)※

mybatis的基本工作流程
1.读取配置文件,配置文件包含数据库连接信息和Mapper映射文件或者Mapper包路径。

2.有了这些信息就能创建SqlSessionFactory,SqlSessionFactory的生命周期是程序级,程序运行的时候建立起来,程序结束的时候消亡

3.SqlSessionFactory建立SqlSession,目的执行sql语句,SqlSession是过程级,一个方法中建立,方法结束应该关闭

4.当用户使用mapper.xml文件中配置的的方法时,mybatis首先会解析sql动态标签为对应数据库sql语句的形式,并将其封装进MapperStatement对象,然后通过executor将sql注入数据库执行,并返回结果。

5.将返回的结果通过映射,包装成java对象。

1、jdbc.properties,下面会调用

jdbc.username=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库

2、mapper配置

<?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.....IUserDao">

1 在mybatis中,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。
当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句

public interface IUserDao extends IBaseDao<User>{
    public void modifyPassword(User user);
}

对应xml有

<!--修改密码-->
<update id="modifyPassword" parameterType="com.qf.entity.User">
    UPDATE t_user set password=#{password} where id=#{id};
</update>

2 resultMap的使用

在mybatis中有一个resultMap标签,它是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与数据库表中的字段进行关联映射。

2.1数据库与实体类之间名称相同
前提要实体类和数据库字段名称一毛一样,实际一般不这样,在mapper中的查询方式:

2.2 数据库与实体类之间不相同

第一种:开启驼峰规则
mybatis中开启

<setting name="mapUnderscoreToCamelCase" value="true" />

<?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="menu.mysql">
    <select id="selectMenu" resultType="org.me.menu.Menu">
        select MENU_ID, MENU_NAME, PARENT_ID, URL, TITLE, LEAF, ORDER_SEQ
          from mysql.MENU
          order by ORDER_SEQ
    </select>
</mapper>

第二种:使用resultMap标签来映射

然后在mapper.xml中书写resultMap标签,使得数据库字段和实体类

名称映射。(将实体类字段与数据库字段在标签中进行一一映射)

<resultMap id="BaseResultMap" type="com...entity.User" >
    <id column="id" property="id" jdbcType="INTEGER"/>
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="sex" property="sex" jdbcType="INTEGER" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="INTEGER" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="INTEGER" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="flag" property="flag" jdbcType="INTEGER" />
    <result column="png" property="png" jdbcType="LONGVARCHAR" />
</resultMap>

3、配置数据库

什么是数据源

JDBC2.0 提供了javax.sql.DataSource接口,它负责建立与数据库的连接,当在应用程序中访问数据库时
不必编写连接数据库的代码,直接引用DataSource获取数据库的连接对象即可。用于获取操作数据Connection对象。

<!-- 1.加载配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 2.dataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClass}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

4、spring-mybatis.xml核心文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">


    <!-- 包含配置文件进来-->
    <import resource="classpath:spring-datasource.xml"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com....entity"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor"></bean><!--这里用了pagehelper插件在mybtis的配置,就是分页的插件-->
            </array>
        </property>
    </bean>

    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="tx"/>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qf.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Mybatis-Plus可以使用注解的方式来代替XML文件,这样可以减少开发人员的工作量,提高开发效率。同时,Mybatis-Plus还提供了一些方便的工具类和方法,使得开发更加简单和快捷。因此,使用Mybatis-Plus可以让开发更加高效和便捷。 ### 回答2: mybatis-plus是基于mybatis的一个增强工具,其致力于简化mybatis的开发流程,其中之一的特点就是开发者可以不需要编写繁琐的XML文件即可完成CRUD操作。 mybatis开发中,我们需要按照数据表的结构编写对应的实体类、Mapper接口和XML文件,对于简单的查询可以直接在Mapper接口中通过注解实现,但对于大多数复杂的SQL操作,还是需要编写XML文件来实现。然而,这种方式会使得程序结构变得复杂,且XML文件的维护和调试也很麻烦,为此mybatis-plus提供了一种更加方便和简单的方式:使用注解代替XMLmybatis-plus的Mapper接口中提供了多种注解,包括@Insert、@Update、@Delete、@Select等等。使用这些注解可以直接在Mapper接口中完成CRUD操作,并且还可以使用@Param注解来传递参数。例如,一个简单的查询可以这样实现: @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(@Param("id") Long id); 使用这种方式,就可以避免编写XML文件,不仅可以简化开发流程,还可以使程序结构变得更加清晰,易于维护和调试。当然,如果需要执行较为复杂的SQL语句mybatis-plus也提供了一些高级用法,可以在不编写XML文件的情况下实现。 总之,mybatis-plus的不用写XML文件的特点使得开发变得更为简洁高效,提高了程序的可读性和可维护性,让开发者可以更加专注于业务逻辑的处理。 ### 回答3: mybatis-plus是基于mybatis的一个增强工具,可以极大地优化和简化mybatis的使用,其中之一便是省去了编写繁琐的xml文件。 目前,mybatis的使用方式一般是通过编写mapper接口和xml文件,其中mapper接口用于定义SQL语句,而xml文件则用于存放具体的SQL语句。这种方式需要我们手动编写大量的xml文件,而且xml文件的编写难免会遇到各种问题,容易出错。mybatis-plus则完全摆脱了这个麻烦。 mybatis-plus使用的方式是通过注解来实现,它提供了一系列的注解来帮助我们完成SQL语句的编写,这些注解包括@TableName、@TableId、@TableField等,在使用过程中,我们只需要在实体类中添加这些注解,然后调用mybatis-plus中提供的方法即可完成CRUD操作,非常简单方便。 除了简化了SQL语句的编写外,mybatis-plus还提供了很多其他的便利,如自动分页、代码生成器、性能分析等等,这些功能大大提高了开发效率和代码的可读性和可维护性。 总结起来,mybatis-plus的使用不需要编写繁琐的xml文件,省去了大量的力气,使开发更为简单快捷,同时提供了更多的实用功能,非常适合在项目中使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习微站公众平台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值