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

相关文章

python3 gunicorn配置文件的用法解读

《python3gunicorn配置文件的用法解读》:本文主要介绍python3gunicorn配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python3 gunicorn配置文件配置文件服务启动、重启、关闭启动重启关闭总结python3 gun

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Spring Boot结成MyBatis-Plus最全配置指南

《SpringBoot结成MyBatis-Plus最全配置指南》本文主要介绍了SpringBoot结成MyBatis-Plus最全配置指南,包括依赖引入、配置数据源、Mapper扫描、基本CRUD操... 目录前言详细操作一.创建项目并引入相关依赖二.配置数据源信息三.编写相关代码查zsRArly询数据库数

Spring Boot 整合 MyBatis 连接数据库及常见问题

《SpringBoot整合MyBatis连接数据库及常见问题》MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射,下面详细介绍如何在SpringBoot项目中整合My... 目录一、基本配置1. 添加依赖2. 配置数据库连接二、项目结构三、核心组件实现(示例)1. 实体类2. Ma