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

相关文章

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

spring6+JDK17实现SSM起步配置文件

《spring6+JDK17实现SSM起步配置文件》本文介绍了使用Spring6和JDK17配置SSM(Spring+SpringMVC+MyBatis)框架,文中通过示例代码介绍的非常详细,对大家的... 目录1.配置POM文件2.在resource目录下新建beans.XML文件,用于配置spirng3

Mysql8.0修改配置文件my.ini的坑及解决

《Mysql8.0修改配置文件my.ini的坑及解决》使用记事本直接编辑my.ini文件保存后,可能会导致MySQL无法启动,因为MySQL会以ANSI编码读取该文件,解决方法是使用Notepad++... 目录Myhttp://www.chinasem.cnsql8.0修改配置文件my.ini的坑出现的问题