- 浏览: 1133897 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (138)
- JAVA基础 (22)
- Spring (6)
- 设计模式 (2)
- JDK源码 (3)
- java-功能组件 (4)
- 游戏项目 (2)
- linux (13)
- Oracle (2)
- struts (1)
- 字符集 (8)
- HTTP协议 (2)
- java-网络通信 (1)
- 工具软件推荐 (2)
- tomcat (1)
- java-容器框架 (2)
- java-IO框架 (2)
- java-多线程框架 (4)
- java-NIO框架 (0)
- jquery (2)
- 工具使用 (12)
- 加密解密 (1)
- redis (2)
- maven (2)
- svn (1)
- eclipse (1)
- mysql (11)
- 我的收藏 (1)
- JAVA进阶 (26)
- 运维 (3)
- protocol buffer (1)
- 优秀博主 (1)
- nginx (1)
- 算法 (2)
- 故障排查 (4)
- 粤语歌曲 (6)
- 生活总结 (6)
- 高并发 (4)
- 语言训练 (1)
- 读书笔记 (5)
- 诗歌 (1)
- tomcat源码学习 (1)
- 软件词汇 (1)
- git (1)
最新评论
-
ryuhi:
一个是来源source,一个是来源方序列号seq这两个数据要怎 ...
高并发的核心技术-幂等的实现方案 -
xuezhongyu01:
无量 写道Master-Gao 写道理论感觉还行,可以代码我还 ...
高并发的核心技术-幂等的实现方案 -
无量:
Master-Gao 写道理论感觉还行,可以代码我还是不会写。 ...
高并发的核心技术-幂等的实现方案 -
phil_jing:
@RequestParam 默认 true
SpringMVC注解@RequestParam全面解析 -
aguai0:
aguai0 写道第五条里的如果要获取任务执行结果,用Comp ...
JAVA进阶----ThreadPoolExecutor机制
一、越少的代码,越强悍的功能,xml里面应该6个sql语句就够用了,修改,维护成本很低,见下表
二、公共的查询条件和字段列表等抽出公共sql段,方便使用
三、一个mybatis.xml例子
英文名 | 方法名称 | 核心点 | 建议 |
insert | 1.新增数据 | 如果是自增主键,应该返回主键ID | |
deleteById | 2. 根据主键ID删除数据 | sql默认加limit 1,防止多删数据 | 此方法不建议有,建议逻辑删除 |
updateById | 3. 根据主键ID修改数据 | sql默认加limit 1,防止多修改数据 | |
selectById | 4. 根据主键查询数据 | 查询一条数据 | |
selectByIdForUpdate | 5. 根据主键加锁查询数据 | 加锁查询一条数据,事务处理用 | |
queryListByParam | 6. 根据输入条件查询数据列表 | 和7配合使用 | |
queryCountByParam | 7. 根据输入条件查询总数 | 和6配合使用 |
二、公共的查询条件和字段列表等抽出公共sql段,方便使用
英文名 | 方法名称 | 核心点 | 建议 |
_field_list | 1.字段列表 | 修改方便,方便字段排序 | |
_value_list | 2. 字段值列表 | 修改方便,方便字段值排序 | |
_common_where | 3. 通用查询条件 | 每个字段的等值判断 | |
_regin_where | 4. 通用范围区间条件 | 字段的时间区间,字段的金额区间等的判断 | |
_contain_where | 5. 包含字段值范围条件 | 字段的常量值包含判断,in ,not in | |
_common_sorts | 6. 通用排序条件 | order by |
三、一个mybatis.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="Assets"> <!-- 设置1分钟缓存,缓存大小1024,采用最近最少使用算法 --> <cache readOnly="true" flushInterval="60000" size="10" eviction="LRU" /> <resultMap type="Assets" id="AssetsResultMap"> <id property="id" column="id" /> <result property="userId" column="user_id" /> <result property="amount" column="amount" /> <result property="earning" column="earning" /> <result property="type" column="type" /> <result property="status" column="status" /> <result property="productId" column="product_id" /> <result property="productName" column="product_name" /> <result property="cardNo" column="card_no" /> <result property="bankCode" column="bank_code" /> <result property="orderId" column="order_id" /> <result property="effectiveDate" column="effective_date" /> <result property="redeemType" column="redeem_type"/> <result property="initAmount" column="init_amount"/> <result property="initEarning" column="init_earning"/> <result property="redeemingAmount" column="redeeming_amount"/> <result property="redeemingEarning" column="redeeming_earning"/> <result property="redeemedAmount" column="redeemed_amount"/> <result property="redeemedEarning" column="redeemed_earning"/> <result property="punishAmount" column="punish_amount"/> <result property="latestRedeemTime" column="latest_redeem_time"/> <result property="maturityDate" column="maturity_date"/> <result property="createTime" column="create_time" /> <result property="modifyTime" column="modify_time" /> <result property="remark" column="remark" /> </resultMap> <!-- 字段列表 --> <sql id="_field_list"> id, user_id, amount, earning, type, status, product_id, product_name, card_no, bank_code, order_id, effective_date, redeem_type, init_amount, init_earning, redeeming_amount, redeeming_earning, redeemed_amount, redeemed_earning, punish_amount, latest_redeem_time, maturity_date, create_time, modify_time, remark </sql> <!-- 字段值列表 --> <sql id="_value_list"> #{id}, #{userId}, #{amount}, #{earning}, #{type}, #{status}, #{productId}, #{productName}, #{cardNo}, #{bankCode}, #{orderId}, #{effectiveDate}, #{redeemType}, #{initAmount}, #{initEarning}, #{redeemingAmount}, #{redeemingEarning}, #{redeemedAmount}, #{redeemedEarning}, #{punishAmount}, #{latestRedeemTime}, #{maturityDate}, #{createTime}, #{modifyTime}, #{remark} </sql> <!-- 通用查询条件 不支持ID查询条件,ID的直接通过ID即可以查 --> <sql id="_common_where"> <if test="id != null"> AND id = #{id}</if> <if test="userId != null"> AND user_id = #{userId}</if> <if test="amount != null"> AND amount = #{amount}</if> <if test="earning != null"> AND earning = #{earning}</if> <if test="type != null"> AND type = #{type}</if> <if test="status != null"> AND status = #{status}</if> <if test="productId != null"> AND product_id = #{productId}</if> <if test="productName != null"> AND product_name = #{productName}</if> <if test="cardNo != null"> AND card_no = #{cardNo}</if> <if test="bankCode != null"> AND bank_code = #{bankCode}</if> <if test="orderId != null"> AND order_id = #{orderId}</if> <if test="effectiveDate != null"> AND effective_date = #{effectiveDate}</if> <if test="redeemType != null"> AND redeem_type = #{redeemType}</if> <if test="initAmount != null"> AND init_amount = #{initAmount}</if> <if test="initEarning != null"> AND init_earning = #{initEarning}</if> <if test="redeemingAmount != null"> AND redeeming_amount = #{redeemingAmount}</if> <if test="redeemingEarning != null"> AND redeeming_earning = #{redeemingEarning}</if> <if test="redeemedAmount != null"> AND redeemed_amount = #{redeemedAmount}</if> <if test="redeemedEarning != null"> AND redeemed_earning = #{redeemedEarning}</if> <if test="punishAmount != null"> AND punish_amount = #{punishAmount}</if> <if test="latestRedeemTime != null"> <![CDATA[ AND latest_redeem_time = #{latestRedeemTime, jdbcType=TIMESTAMP} ]]> </if> <if test="maturityDate != null"> <![CDATA[ AND maturity_date = #{maturityDate, jdbcType=TIMESTAMP} ]]> </if> <if test="createTime != null"> <![CDATA[ AND create_time = #{createTime, jdbcType=TIMESTAMP} ]]> </if> <if test="modifyTime != null"> <![CDATA[ AND modify_time = #{modifyTime, jdbcType=TIMESTAMP} ]]> </if> <if test="remark != null"> AND remark = #{remark}</if> </sql> <!-- 通用范围区间查询 --> <sql id="_regin_where"> <if test="egtCreateTime != null"> <![CDATA[ AND create_time >= #{egtCreateTime, jdbcType=TIMESTAMP} ]]> </if> <if test="ltCreateTime != null"> <![CDATA[ AND create_time < #{ltCreateTime, jdbcType=TIMESTAMP} ]]> </if> </sql> <!-- 通用排序处理 --> <sql id="_common_sorts"> <if test="sorts != null"> ORDER BY <foreach collection="sorts" item="item" separator=","> ${item.column.columnName} ${item.sortMode.mode} </foreach> </if> </sql> <!-- in 和 not in的通用查询where --> <sql id="_contain_where"> <if test="containStatusSet!=null"> AND status IN <foreach item="item" index="i" collection="containStatusSet" separator="," open="(" close=")" > #{item} </foreach> </if> </sql> <!-- 插入操作 --> <insert id="insert" parameterType="Assets"> INSERT INTO assets ( <include refid="_field_list"/>) VALUES ( <include refid="_value_list"/>) </insert> <!-- 根据ID主键进行删除,注意limit 1 --> <delete id="deleteById" parameterType="java.lang.String" > delete from assets where id = #{id} limit 1 </delete> <!-- 根据主键ID进行更新,注意limit 1 --> <update id="updateById" parameterType="Assets"> UPDATE assets <set> <if test="userId != null"> user_id = #{userId}, </if> <if test="amount != null"> amount = #{amount}, </if> <if test="earning != null"> earning = #{earning}, </if> <if test="type != null"> type = #{type}, </if> <if test="status != null"> status = #{status}, </if> <if test="productName != null"> product_name = #{productName}, </if> <if test="productId != null"> product_id = #{productId}, </if> <if test="cardNo != null"> card_no = #{cardNo}, </if> <if test="bankCode != null"> bank_code = #{bankCode}, </if> <if test="orderId != null"> order_id = #{orderId}, </if> <if test="effectiveDate != null"> effective_date = #{effectiveDate}, </if> <if test="redeemType != null"> redeem_type = #{redeemType}, </if> <if test="initAmount != null"> init_amount = #{initAmount}, </if> <if test="initEarning != null"> init_earning = #{initEarning}, </if> <if test="redeemingAmount != null"> redeeming_amount = #{redeemingAmount}, </if> <if test="redeemingEarning != null"> redeeming_earning = #{redeemingEarning}, </if> <if test="redeemedAmount != null"> redeemed_amount = #{redeemedAmount}, </if> <if test="redeemedEarning != null"> redeemed_earning = #{redeemedEarning}, </if> <if test="punishAmount != null"> punish_amount = #{punishAmount}, </if> <if test="latestRedeemTime != null"> latest_redeem_time = #{latestRedeemTime}, </if> <if test="maturityDate != null"> maturity_date = #{maturityDate}, </if> <if test="modifyTime != null"> modify_time = #{modifyTime}, </if> <if test="remark != null"> remark = #{remark}, </if> </set> <where> id = #{id} limit 1 </where> </update> <!-- 根据ID进行查询 --> <select id="selectById" resultMap="AssetsResultMap"> select * from assets where id = #{id} </select> <!-- 根据ID进行加行锁查询 --> <select id="selectByIdForUpdate" resultMap="AssetsResultMap"> select * from assets where id = #{id} for update </select> <!-- 根据查询条件查询数据和queryCountByParam方法配对使用 --> <select id="queryListByParam" parameterType="map" resultMap="AssetsResultMap"> SELECT <include refid="_field_list"/> FROM assets <where> 1 = 1 <include refid="_common_where"/> <include refid="_regin_where"/> <include refid="_contain_where"/> </where> <include refid="_common_sorts"/> <if test="offset != null and rows != null"> limit #{offset}, #{rows} </if> </select> <!-- 根据查询条件查询总数和queryListByParam方法配对使用 --> <select id="queryCountByParam" parameterType="map" resultType="java.lang.Integer"> SELECT count(1) FROM assets <where> 1 = 1 <include refid="_common_where"/> <include refid="_regin_where"/> <include refid="_contain_where"/> </where> </select> </mapper>
发表评论
-
区块链!每个人都要了解下--十分钟洞见区块链的前世今生
2018-04-27 12:32 738区块链!每个人都要了解下--十分钟洞见区块链的前世今生 ... -
区块链!每个人都要了解下--十分钟洞见区块链的前世今生
2018-04-27 12:09 0为啥要讲区块链呢,因为它太火了,火到什么程度呢 ... -
项目打包,报软件包、类不存在问题排查过程
2017-05-16 17:13 5893项目打包报,软件包、类不存在问题排查过程 一、背景 ... -
海量数据存储--分库分表策略详解
2017-04-12 19:59 7270海量数据存储--分库分表策略详解 一、背景: 系统刚 ... -
jstack详解
2017-02-17 11:15 1828jstack http://www.open-open.co ... -
jdk-源码中的一些坑
2017-02-13 15:17 1169jdk-源码中的一些坑 1. Runnable接口的命名简直 ... -
一次mysql死锁的排查过程
2016-11-21 10:04 11194一次mysql死锁的排查过程 一、背景 17号晚上要吃饭 ... -
JVM调优:选择合适的GC collector (三)
2016-11-15 20:51 1210CMS Collector 在很多地方,CMS Collec ... -
JVM调优:选择合适的GC collector (二)
2016-11-15 20:47 946http://blog.csdn.net/historya ... -
JVM调优:选择合适的GC collector (一)
2016-11-15 20:45 1226http://blog.csdn.net/historyas ... -
jstat查看gc情况
2016-11-10 10:11 2445jps(Java Virtual Machine Proces ... -
tomcat源码学习(一) eclipse导入tomcat源码
2016-10-31 20:05 13091. 到官网下载Tomcat源代码,这里用到的是apache- ... -
深入分析ClassLoader
2016-10-27 23:27 712转(原文http://blog.csdn.net/xya ... -
业务架构模板
2016-10-20 19:56 1815业务架构模板 默认一个高大上的业务系统需要具备的技术点和对应 ... -
如何写一个强壮的JOB任务
2016-10-18 15:00 2970如何写一个强壮的JOB任务 1. JOB跑一半断电了,不能产 ... -
数据库设计规范
2016-10-17 23:29 64981. 数据库设计基本规范 领域驱动表内容 ... -
全局主键生成器-支持单JVM1秒近1000万订单生成
2016-05-03 20:46 5606全局主键生成器 介绍: 相对于DB自增序列的全局主键生成器, ... -
解决并发下累计的问题
2016-04-25 11:58 2071package com.tongbanjie.trade.te ... -
系统开发中的坑
2016-03-15 15:39 2734系统开发中的坑 这个是在公司分享的一个ppt,整理下发到博客里 ... -
系统开发中的坑
2016-03-14 15:55 193系统开发的坑 一.幂等性 二.数据库 三.代码 ...
相关推荐
创建sql映射文件:接口名称.xml 9.编写测试类,测试接口方法:接口名称+方法+Test.class cn.edu.xxxx. util 存放工具类包 类/接口的格式:名称+Util pojo 存放实体类包 类/接口的格式:表名称 ...
<mapper namespace="com.tjjp.business.Notice.model.Notice(类的全路径)"> <resultMap id="BaseResultMap" type="com.tjjp.business.Notice.model.Notice(类的全路径)"> <!-- 对于的类型 jdbcType -->...
mybatis是一个持久层框架,用java编写的。 它封装了jdbc操作的很多细节,开发者只需要关注sql语句本身,无需关注注册驱动,创建连接等繁杂过程 它使用了ORM思想实现了结果集的封装。 ORM: Object ...
里面主要包含了数据库连接相关东西,还有 java 类所对应的别名,比如 <typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/> 这个别名非常重要,你在 具体的类的映射中,比如User.xml 中 resultType ...
基本思路 用户定义结构和接口 在接口的方法上定义 sql (可以在 xml 或 方法的注释中) 用工具生成接口的实现 创建接口的实例并使用它
4. Mybatis 是如何将 sql 执行结果封装为目标对象并返回的? 都有哪些映射形式? 5. Xml 映射文件中,除了常见的 select|insert|update|delete 标 签之外,还有哪些标签? 6. 简述 Mybatis 的插件运行原理,以及如何...
埃索姆介绍一个易于使用的Golang ORM工具,支持MyBatis-Like XML模板SQL特征功能齐全的ORM(几乎) MyBatis-Like XML模板SQL 开发人员友好入门 type RowType struct {Id intName string}type DAO struct {Id intName...
mybatis 一对多多对多案例详解 1、首先 准备sql 数据 user(用户表) role(角色...4、编写UserDao,RoleDao以及映射文件UserDaoMapping.xml ,RoleDaoMapping.xml和主配置文件SqlMapConfig.xml,下面上代码 5、编写测试类
mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码(mapper.java、mapper.xml、pojo…),可以让程序员将更多的精力放在繁杂的业务逻辑上。 企业实际开发...
mybatis是需要自己在mapper文件中编写sql,这样一来,如果表特别多的话,那么我们需要自己建很多类(pojo,mapper文件,sql语句等),会非常麻烦。为了解决这个问题,mybatis官方提供逆向工程 可以针对单表自动生成...
8.3 第三步:拷贝生成的mapper文件到工程中指定的目录中 73 8.3.1 Mapper.xml 73 8.3.2 Mapper.java 73 8.3.3 第四步Mapper接口测试 73 8.4 逆向工程注意事项 74 8.4.1 Mapper文件内容不覆盖而是追加 74 8.4.2 Table...
(3)MyBatis在XML文件中编写SQL语句,和程序逻辑代码分离,降低了耦合度。这样就不会对应用程序或数据库的现有设计有任何影响,便于统一管理和优化,也提高了代码的可重用性。 (4)MyBatis提供了XML元素,支持...
(3)通过xml 文件或注解的方式将要执行的各种 statement 配置起来,并通过java对象和 statement中sql的动态参数进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射为java对象并返回。...
使用原生的Mybatis编写持久层逻辑时,所需要的代码是比较繁琐的,需要定义Mapper接口和Mapper.xml文件,每一个方法都需要编写对应的sql语句,会存在很多大量的重复工作,使用MP之后,对通用的方法做了高度的抽取,...
这些 SQL 语句可以在 XML 映射文件中定义,也可以使用注解的方式直接写在接口方法上。 5. 编写测试代码 在测试类中,首先加载 MyBatis 的配置文件,然后获取 SqlSession。通过 SqlSession,你可以获取 Mapper 接口...
在实际生产中减少了80%以上的重复SQL编写工作,从而把关注力转移到模型本身的制定上。结合建表语句生成插件pngen,大部分场景只需编写一个模型类即可完成DAO层工作。 支持最主流的MyBatis框架,无学习成本 基于常见...
mybatis配置文件SqlMapConfig.xml*** mybatis核心: mybatis输入映射(掌握) mybatis输出映射(掌握) mybatis的动态sql(掌握) *****灵活 高级知识 订单商品数据模型分析 高级结果集映射(一对一、...
MyBatis使用XML描述符或注释将对象与存储过程或SQL语句耦合。相对于对象关系映射工具,简单性是MyBatis数据映射器的最大优势。 Mybaits的优点: 1、基于SQL语句编程,相当灵活,不会对应用程序或者数据库的现有设计...
mybatis支持自定义 SQL、存储过程以及高级映射,可以通过sql映射文件实现sql语句的编写,支持动态sql,用条件判断进行查询可以实现sql复用。 2、mybatis优势 通过参数映射方式,可以将参数灵活的配置在SQL语句中的...
其核心思想是将 SQL 语句从 Java 代码中分离出来,在 XML 或注解中进行配置,从而实现了 SQL 与 Java 代码的分离。 以下是 MyBatis 的一些主要特点和功能: SQL 映射配置:MyBatis 使用 XML 文件或注解来描述 SQL ...