<?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.yhb.jsxn.mapper.FinanceProductsUsersMapper">
<!-- mybatis sql语句中的转义字符的书写:
1、在xml的sql语句中,不能直接用大于号、小于号要用转义字符
如果用小于号会报错误如下:
org.apache.ibatis.builder.BuilderException: Error creating document instance.
Cause: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.
转义字符
小于号 < &lt;
大于号 > &gt;
和 & &amp;
单引号 ' &apos;
双引号 " &quot;
2、使用<![CDATA[ 你的sql语句 ]]>(sql语句中的<where><if>等标签不会被解析)
如:
<![CDATA[
select * from
(select t.*, ROWNUM as rowno from tbl_user t where ROWNUM <= #{page.end,jdbcType=DECIMAL}) table_alias
where table_alias.rowno >#{page.start,jdbcType=DECIMAL}
]]>
--> <!-- Mybatis批量插入Oracle、MySQL --> <!-- mysql数据库的数据类型: -->
<!-- --> <!-- mysql修改字符编码
  X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConfig.exe
  重新启动设置,将默认编码设置为utf8.这样就能达到我们所要的效果了。 1、修改数据库字符编码 mysql> alter database mydb character set utf8 ; 2、创建数据库时,指定数据库的字符编码 mysql> create database mydb character set utf8 ; 3、查看mysql数据库的字符编码 mysql> show variables like 'character%'; //查询当前mysql数据库的所有属性的字符编码 +--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+ 4、修改mysql数据库的字符编码 修改字符编码必须要修改mysql的配置文件my.cnf,然后重启才能生效 通常需要修改my.cnf的如下几个地方: 【client】下面,加上default-character-set=utf8,或者character_set_client=utf8 【mysqld】下面,加上character_set_server = utf8 ; 因为以上配置,mysql默认是latin1,如果仅仅是通过命令行客户端,mysql重启之后就不起作用了。 如下是客户端命令行修改方式,不推荐使用 mysql> set character_set_client=utf8 ; mysql> set character_set_connection=utf8 ; mysql> set character_set_database=utf8 ; mysql> set character_set_database=utf8 ; mysql> set character_set_results=utf8 ; mysql> set character_set_server=utf8 ; mysql> set character_set_system=utf8 ; mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+ -> ;
+--------------------------+---------------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files (x86)\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+---------------------------------------------------------------+ 8 rows in set (0.00 sec)
8 rows in set (0.00 sec)
-->
<!-- 查询结果映射 -->
<!--解决数据库表字段列明和实体vo不匹配问题 -->
<resultMap id="BaseResultMap" type="com.yhb.jsxn.entity.FinanceProductsUsers">
<!-- 主键映射 -->
<id column="FPUID" property="FPUID" jdbcType="INTEGER" />
<result column="UserID" property="UserID" jdbcType="VARCHAR" />
<result column="FProductsRates" property="FProductsRates" jdbcType="FLOAT" />
<result column="FProductsBuyMoney" property="FProductsBuyMoney" jdbcType="DECIMAL" />
<result column="FProductsBuyTime" property="FProductsBuyTime" jdbcType="TIMESTAMP" />
<result column="FProductsRateInNum" property="FProductsRateInNum" jdbcType="INTEGER" /> </resultMap> <!-- select 语句
select 标签属性:
id: id编号
parameterType: 获取的参数值:
eg:
java.lang.Integer
map
resultMap:
eg:
返回的是一个映射结果集,对应一个实体vo类
想用ParameterType=Map传入多个参数构造SQL进行查询:
<select id="getBusList" resultMap="busListMap" parameterType="java.util.Map">
select bs.bus_id as bus_id,bs.arrive_time as up_time,b.start_station
as start_station_id,
b.end_station as end_station_id
from bus b , bus_station bs where b.bus_id = bs.bus_id and
bs.station_id=#{upStationId}
and is_up=1 and b.up_station_line like
#{upStationLineLike} and b.down_station_line
like
#{downStationLineLike}
and (b.daily=1 or b.weekly like #{weeklyLike} or b.run_day like
#{runDayLike} )
order by bs.arrive_time asc
</select>
调试时报 Parameter not found异常
解决方法,使用此方式传参,必须在对应的接口方法用@Param标签定义参数value才行: public List<Bus> getBusList(@Param(value = "upStationId") long upStationId,
@Param(value = "upStationLineLike") String upStationLineLike,
@Param(value = "downStationLineLike") String downStationLineLike,
@Param(value = "weeklyLike") String weeklyLike,
@Param(value = "runDayLike") String runDayLike
){} ;
resultType:
eg:
Integer
String
Decimal
int
-->
<select id="selectFinByFPUID" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select
*
from financeproducts_users
where FPUID = #{FPUID,jdbcType=INTEGER}
</select>
<select id="selectAllByFPid" resultMap="BaseResultMap" parameterType="map">
select
a.TrueName,a.UserName,b.FProductsBuyMoney,b.FProductsBuyTime from
accounts_users a, financeproducts_users b where a.UserID = b.UserID
and b.FPid=#{fpid} and FProductsCountNum &gt; FProductsRateInNum order
by b.FProductsBuyTime DESC limit
#{pageNo},#{size}
</select>
<select id="getFinancialUsers" resultType="String">
select distinct userid from financeproducts_users
</select>
<select id="selectNewProduct" parameterType="String" resultType="Integer">
select count(*) from financeproducts_users where UserID =#{userId} and
FPid in (140,141)
</select>
<select id="getUserFreeze" parameterType="map" resultType="Decimal">
select sum(FproductsBuyMoney-FProductsRateMoney) userFrezz from
financeproducts_users where UserID =#{userId} and FProductsEm_k2=1
</select>
<!-- <if test="array.length > 0">
<where>
<foreach collection="array" open="(" item="age" close=")" separator=",">
and age in (#{age})
</foreach>
</where>
</if> --> <select id="selectByUserIdById" resultMap="BaseResultMap" parameterType="Map">
select * from (
select
a.UserID,a.FProductsBuyMoney,a.FProductsName,b.ProfitMoney,b.UserId_Get,b.UserName_Give
from financeproducts_users a
left join
(SELECT
UserId_Give,UserId_Get,UserName_Give,ProfitMoney FROM
accounts_distributor_profit
where UserId_Get=#{Id} group by UserId_Give
)b
on a.UserId=b.UserId_Give
)a where a.UserId_Get is not null
</select>
<select id="getHistoricalBuyProductsByName" resultMap="BaseResultMap"
parameterType="hashMap">
select
FProductsName,FProductsRateInNum,FProductsImgs,FProductsCountRateMoney,FProductsBuyMoney,
FProductsBuyTime,FProductsCountNum
from financeproducts_users
where
<if test="UserID !=null">
UserID =#{UserID}
</if>
and FProductsCountNum<![CDATA[<=]]>FProductsRateInNum
<if test="startTime !=null">
and DATE_FORMAT(FProductsBuyTime,'%Y-%m-%d')<![CDATA[>=]]>#{startTime}
</if>
<if test="dayNum !=null">
and FProductsCountNum <![CDATA[<=]]>#{dayNum}
</if>
<if test="FProductsName !=null and FProductsName !=''">
<!-- mysql> select concat_ws(',','11','22','33'); +-------------------------------+
| concat_ws(',','11','22','33') |
+-------------------------------+
| 11,22,33 |
+-------------------------------+
oracle 可以使用||来连接
-->
and FProductsName like CONCAT('%', #{FProductsName}, '%')
</if>
</select>
<select id="selectAlreadyByUserId" resultMap="BaseResultMap"
parameterType="Map">
select
FProductsName,FProductsRateInNum,FProductsImgs,FProductsCountRateMoney,FProductsBuyMoney,
FProductsBuyTime,FProductsCountNum
from financeproducts_users
where
FProductsCountNum &gt; FProductsRateInNum and UserID =#{UserID}
order
by FProductsBuyTime
</select>
<!--根据查询条件获取历史购买产品并分页
用 ISNULL(), NVL(), IFNULL() and COALESCE() 函数替换空值
在数据库操作中,往往要对一些查询出来的空值进行替换,如函数SUM(),这个函数如果没有值会返回NULL,这是我们不希望看到的,
在MySQL中我们可以这样来写:
select IFNULL(sum(data),0) ...
在SQLSERVER中我们可以这样写:
select ISNULL(sum(data),0) ...
在ORACLE中我们可以这样写:
select NVL(sum(data),0) ...
对于所有数据库适用的方法可以这样写:
select COALESCE(sum(data),0) ...
COALESCE()用法:
COALESCE(value,...)
返回第一个不是null的值,如果参数列表全是null,则返回null
SELECT COALESCE(NULL,1);
-> 1
SELECT COALESCE(NULL,NULL,NULL);
-> NULL
-->
<select id="selectHistoricalByUserIdByPageName" resultMap="BaseResultMap"
parameterType="Map"> select b.UserID,coalesce(a.FProductsBuyMoney,b.FProductsRateMoney) as FProductsBuyMoney,coalesce(a.m,0) as sumProfit,b.FProductsRateMoney,b.FPUID,b.FProductsName,b.FProductsImgs from
( select * from financeproducts_users where FProductsEm_k2 = 0 and userid =#{UserID}) b
left join
(select userid,FProductsBuyMoney,FProductsRateEm_k1,sum(FProductsAsRateMoney) as m from financeproducts_rates
where userid =#{UserID}
group by FProductsRateEm_k1 ) a
on a.FProductsRateEm_k1=b.fpuid <if test="startTime !=null">
and DATE_FORMAT(b.FProductsBuyTime,'%Y-%m-%d')<![CDATA[>=]]>#{startTime}
</if>
<if test="productName !=null">
and b.FProductsName like CONCAT('%', #{productName}, '%')
</if>
</select> <!-- insert -->
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="FPUID" parameterType="com.yhb.jsxn.entity.FinanceProductsUsers">
insert into financeproducts_users
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="UserID != null">
UserID,
</if>
<if test="FPid != null">
FPid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="UserID != null">
#{UserID,jdbcType=VARCHAR},
</if>
<if test="FPid != null">
#{FPid,jdbcType=INTEGER},
</if>
<if test="FProductsName != null">
#{FProductsName,jdbcType=VARCHAR},
</if>
</trim>
</insert> <!-- update 语句 -->
<update id="updateByPrimaryKeySelective" parameterType="com.yhb.jsxn.entity.FinanceProductsUsers">
update financeproducts_users
<set>
<if test="FProductsEm_k2 != null">
FProductsEm_k2=#{FProductsEm_k2,jdbcType=VARCHAR},
</if>
<if test="FProductsEm_k3 != null">
FProductsEm_k3=#{FProductsEm_k3,jdbcType=VARCHAR},
</if>
</set>
where FPUID =#{FPUID,jdbcType=INTEGER}
</update> <!-- del 语句 -->
</mapper>

mybatis常用写法

【mybatis-记录】的更多相关文章

  1. MyBatis记录

    记录一下MyBatis的几个模块大纲,除去缓存以及集合映射两个部分 Mybatis架构 1. mybatis配置 SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了myb ...

  2. Mybatis 记录

    1. #{}, ${}两种传参数方式的区别 1) ${} 会将传入的参数完全拼接到sql语句中,也就是相当于一个拼接符号. 也就是,最后的处理方式就相当于 String sql = select * ...

  3. Mybatis框架基础支持层——日志模块(8)

    前言: java开发中常用的日志框架有Log4j,Log4j2,Apache Commons Log,java.util.logging,slf4j等,这些工具对外的接口不尽相同.为了统一这些工具的接 ...

  4. MyBatis源码解读之延迟加载

    1. 目的 本文主要解读MyBatis 延迟加载实现原理 2. 延迟加载如何使用 Setting 参数配置 设置参数 描述 有效值 默认值 lazyLoadingEnabled 延迟加载的全局开关.当 ...

  5. MyBatis 源码篇-日志模块1

    在 Java 开发中常用的日志框架有 Log4j.Log4j2.Apache Common Log.java.util.logging.slf4j 等,这些日志框架对外提供的接口各不相同.本章详细描述 ...

  6. mybatis源码解析-日志适配器

    1.为什么需要使用适配器?    集成第三方日志组件,屏蔽日志组件底层实现,统一提供写日志的接口. 2.什么是适配器模式 定义:将一个类的接口变成客户端所期待的另一种接口,从而使原本因接口不匹配而无法 ...

  7. MyBatis获取插入记录的自增长字段值

    在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名! <insert id=" ...

  8. mybatis 配置返回集合collection时只有一条记录

    查询语句配置如下: <select id="selectCustomerList" resultMap="CustomerDtoMap" paramete ...

  9. 备忘:mybatis 3的使用记录

    这是一篇记录.mybatis是一个部分模仿ORM的framework.是一个介于ORM和原始JDBC的框架.既可以提供ORM的操作对象的特性,又能从详细地控制最终的SQL.达到一个平衡.我们还是得写s ...

  10. Mybatis获取插入记录的自增长ID(转)

    1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <ins ...

随机推荐

  1. Zookeeper简单配置

    1.配置zookeeper 解压tar包后,在conf目录下,有一个zoo_sample.cfg,拷贝一份并命名为zoo.cfg cp zoo_sample.cfg zoo.cfg 编辑修改数据路径, ...

  2. jsp的9个内置对象

    Jsp提供了request.response.session.application.out.page.config.exception.pageContext9个内置对象. 1. Request R ...

  3. 如何设置minSdkVersion和targetSdkVersion

    转http://www.07net01.com/2015/07/878098.html minSdkversion和targetSdkVersion相信很多人都不太理解,我在网上也看了许多关于这两者区 ...

  4. Python习题-登录

    写一个登录的程序,失败次数最多为3次,输入账号.密码错误,提示账号/密码错误.失败三次程序退出,输入正确,提示欢迎xxx登录 i=0while (i<3): username = input(' ...

  5. Jmete基础使用

    1,jmeter下载与安装 Jmeter的运行需要JDK支持,所以需要先安装好jdk,并配置好环境变量: 下载地址:http://jmeter.apache.org/download_jmeter.c ...

  6. SQL的CASE表达式用法

    case 表达式从SQL-92标准开始引入,因此是不依赖于具体的数据库技术,可提高SQL代码的可移植性. case表达式注意事项: 1. 统一各个分支返回数据类型,并保证各个when字句的排他性,因为 ...

  7. git克隆某一个branch

    git clone -b <branch> <remote_repo> 例如: git clone -b 指定的分支名字

  8. python 3 serial module install

    /************************************************************************* * python 3 serial module ...

  9. jitter buffer QoS的解决方案

    本文主要介绍一种QoS的解决方案,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help QoS出现的背景: 而当网络发生拥塞的时候,所有的数据流都有 ...

  10. ACM学习历程—UVALive 7147 World Cup(分类讨论 && 贪心)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...