一个由库表到POJO的myBatis配置文件建立的例子

2023-12-02 06:58

本文主要是介绍一个由库表到POJO的myBatis配置文件建立的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在Java web项目中,使用 myBatis 来作为DB持久化ORM框架时,其中免不了要进行从数据库表到Java 的POJO对象的映射XML文件的编写,本例假设以MS SQL SERVER 2008作为数据库,对应的DB表定义和XML映射文件分别如下:

1,DB表 productCategory  定义,

create table productCategory(
recId int identity(1,1) not null,
parentCategoryId int not null default 0,
productCategoryId int not null default 0, //value created by trigger
productCategoryName varchar(100) not null default '',
description varchar(200) not null default '',
showFlag varchar(1) not null default '1', //1=show,0=hide
showIndex int not null default 0,
addTime date not null default getDate(),
primary key(recId)
)

2,myBatis 对应库表到POJO的映射XML文件如下,(接口这里省去了,根据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="com.xxx.service.ProductCategoryService"><resultMap id="productCategoryList" type="ProductCategory"><id column="recId" property="recId" /><result column="parentCategoryId" property="parentCategoryId" /><result column="productCategoryId" property="productCategoryId" /><result column="productCategoryName" property="productCategoryName" /><result column="description" property="description" /><result column="showFlag" property="showFlag" /><result column="showIndex" property="showIndex" /><result column="addTime" property="addTime" javaType="java.util.Date" jdbcType="TIMESTAMP" /><result column="remark" property="remark" /></resultMap><select id="getByRecId" parameterType="Integer" resultType="ProductCategory">select * from [ProductCategory] where [recId] = #{recId}</select><select id="getByCategoryId" parameterType="Integer" resultType="ProductCategory">select * from [productCategory] where [productCategoryId] = #{productCategoryId}</select><select id="getByProductCategoryName" parameterType="String" resultType="ProductCategory">select top 1 * from [productCategory] where productCategoryName = #{productCategoryName}</select><!-- 方法一:大于或小于号可用用对应实体代替,大于号用 > 小于号用 < 方法二:用<![CDATA[]]>指令包含SQL语句,如:<![CDATA[用户自定义SQL语句]]>-->    <select id="queryByProductCategoryName" parameterType="String" resultMap="productCategoryList">select * from productCategory where charIndex(#{productCategoryName},productCategoryName) > 0 order by showIndex asc </select><!-- queryByXXXCount是为查询帮 queryByXXX求出总记录数,是配对的,名称前者在后者基础上增加Count --><!-- row_number() over(order by xx asc|desc)方式 ,support ms sql2005+ --><select id="queryByProductCategory" parameterType="ProductCategoryParam" resultMap="ProductCategoryList">select top ${rows} recId ,parentCategoryId ,productCategoryId ,productCategoryName ,description ,showFlag ,showIndex ,addTime ,remarkfrom (select *,ROW_NUMBER() over(order by showIndex asc) rowNo from productCategory where 1=1<if test="adParentCategoryId != null and adParentCategoryId >= 0">and parentCategoryId = #{parentCategoryId}  </if> <if test="productCategoryId != null and productCategoryId >= 0">and productCategoryId = #{productCategoryId} </if> <if test="productCategoryName != null and productCategoryName != ''">and charIndex(#{productCategoryName},productCategoryName) > 0 </if><if test="showFlag != null and showFlag != ''">and showFlag = #{showFlag}</if>) tempProductCategory where rowNo >= ${offsetRecord} </select><select id="queryByProductCategoryCount" parameterType="ProductCategoryParam" resultType="Long">select count(1) as totalRecord from productCategory where 1=1 <if test="adParentCategoryId != null and adParentCategoryId >= 0">and parentCategoryId = #{parentCategoryId}  </if> <if test="productCategoryId != null and productCategoryId >= 0">and productCategoryId = #{productCategoryId} </if> <if test="productCategoryName != null and productCategoryName != ''">and charIndex(#{productCategoryName},productCategoryName) > 0 </if><if test="showFlag != null and showFlag != ''">and showFlag = #{showFlag}</if></select><!-- useGeneratedKeys属性设置为true表明通知MyBatis获取由数据库自动生成的主键值;keyColumn="GENERATED_KEY" 表示从DB查询得到最新的主键值放到myBatis系统的字段GENERATED_KEY中;keyProperty="XXX"指定把查询获取到的主键值注入到对象的XXX属性中;最新的主键(此为recId)值的获取需要位于insert元素内部的selectKey元素人工去DB中查询返回得到最新值;因selectKey元素位于insert元素内部,故selectKey的参数可以引用insert元素的参数值;其中selectKey查询的顺序可以是插入语句的执行之前或之后,由order=BEFORE|AFTER指定,如本例;order = BEFORE(在插入语句前面)|AFTER(在后面); 参考http://www.iteye.com/problems/86864-->	<insert id="insert" useGeneratedKeys="true" keyProperty="recId" parameterType="ProductCategory"><!-- recId,productCategoryId 由数据库自动生成/created by db auto --><![CDATA[insert into productCategory(parentCategoryId ,productCategoryName ,description ,showFlag ,showIndex ,remark, addTime) values (#{parentCategoryId},#{productCategoryName},#{description} ,#{showFlag} ,#{showIndex} ,#{remark} ,#{addTime,jdbcType=TIMESTAMP})]]><selectKey resultType="java.lang.Integer" keyProperty="recId" order="AFTER"> SELECT top 1 recId from [productCategory] where productCategoryName = #{productCategoryName}  order by recId desc </selectKey>			</insert><update id="update" parameterType="productCategory"><!-- recId,productCategoryId can not update -->update productCategory set parentCategoryId = #{parentCategoryId},productCategoryName = #{productCategoryName},description = #{description},showFlag = #{showFlag},showIndex = #{showIndex},remark = #{remark},addTime = #{addTime,jdbcType=TIMESTAMP}where recId = #{recId}</update><delete id="delete" parameterType="productCategory">delete from productCategory where recId = #{recId}</delete><delete id="deleteByRecId" parameterType="Integer">delete from productCategory where recId = #{recId}</delete><delete id="deleteByProductCategoryId" parameterType="Integer">delete from productCategory where productCategoryId = #{productCategoryId}</delete><delete id="deleteByParentCategoryId" parameterType="Integer">delete from productCategory where parentCategoryId = #{parentCategoryId}</delete>
</mapper>

这里基本设计到建立 mybatis 配置文件时,遇到的常见问题,参考这些,可以很方便写出自己需要的配置文件,简单吧?懒人计划 大笑...

欢迎拍砖讨论...



这篇关于一个由库表到POJO的myBatis配置文件建立的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/444383

相关文章

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

Mybatis Plus JSqlParser解析sql语句及JSqlParser安装步骤

《MybatisPlusJSqlParser解析sql语句及JSqlParser安装步骤》JSqlParser是一个用于解析SQL语句的Java库,它可以将SQL语句解析为一个Java对象树,允许... 目录【一】jsqlParser 是什么【二】JSqlParser 的安装步骤【三】使用场景【1】sql语

mybatis的mapper对应的xml写法及配置详解

《mybatis的mapper对应的xml写法及配置详解》这篇文章给大家介绍mybatis的mapper对应的xml写法及配置详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录前置mapper 对应 XML 基础配置mapper 对应 xml 复杂配置Mapper 中的相