一个由库表到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

相关文章

MyBatis配置文件中最常用的设置

《MyBatis配置文件中最常用的设置》文章主要介绍了MyBatis配置的优化方法,包括引用外部的properties配置文件、配置外置以实现环境解耦、配置文件中最常用的6个核心设置以及三种常用的Ma... 目录MyBATis配置优化mybatis的配置中引用外部的propertis配置文件⚠️ 注意事项X

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

MyBatis-Plus使用动态表名分表查询的实现

《MyBatis-Plus使用动态表名分表查询的实现》本文主要介绍了MyBatis-Plus使用动态表名分表查询,主要是动态修改表名的几种常见场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录1. 引入依赖2. myBATis-plus配置3. TenantContext 类:租户上下文

Spring Boot 集成 mybatis核心机制

《SpringBoot集成mybatis核心机制》这篇文章给大家介绍SpringBoot集成mybatis核心机制,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值... 目录Spring Boot浅析1.依赖管理(Starter POMs)2.自动配置(AutoConfigu

在SpringBoot+MyBatis项目中实现MySQL读写分离的实战指南

《在SpringBoot+MyBatis项目中实现MySQL读写分离的实战指南》在SpringBoot和MyBatis项目中实现MySQL读写分离,主要有两种思路:一种是在应用层通过代码和配置手动控制... 目录如何选择实现方案核心实现:应用层手动分离实施中的关键问题与解决方案总结在Spring Boot和

使用MyBatis TypeHandler实现数据加密与解密的具体方案

《使用MyBatisTypeHandler实现数据加密与解密的具体方案》在我们日常的开发工作中,经常会遇到一些敏感数据需要存储,比如用户的手机号、身份证号、银行卡号等,为了保障数据安全,我们通常会对... 目录1. 核心概念:什么是 TypeHandler?2. 实战场景3. 代码实现步骤步骤 1:定义 E

MyBatis中的大于等于、小于等于写法

《MyBatis中的大于等于、小于等于写法》MyBatisXML映射文件中处理大于等于和小于等于符号的两种方法:使用转义字符和CDATA块,转义字符更为常见,而CDATA块则提供了一种更易读的解决方案... 目录1. 使用转义字符(推荐)2. 使用 CDATA 块注意事项总结在 MyBATis 的 XML

springboot+mybatis一对多查询+懒加载实例

《springboot+mybatis一对多查询+懒加载实例》文章介绍了如何在SpringBoot和MyBatis中实现一对多查询的懒加载,通过配置MyBatis的`fetchType`属性,可以全局... 目录springboot+myBATis一对多查询+懒加载parent相关代码child 相关代码懒

linux实现对.jar文件的配置文件进行修改

《linux实现对.jar文件的配置文件进行修改》文章讲述了如何使用Linux系统修改.jar文件的配置文件,包括进入文件夹、编辑文件、保存并退出编辑器,以及重新启动项目... 目录linux对.jar文件的配置文件进行修改第一步第二步 第三步第四步总结linux对.jar文件的配置文件进行修改第一步进

Springboot主配置文件解析

《Springboot主配置文件解析》SpringBoot主配置文件application.yml支持多种核心值类型,包括字符串、数字、布尔值等,文章详细介绍了Profile环境配置和加载位置,本文... 目录Profile环境配置配置文件加载位置Springboot主配置文件 application.ym