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>