一个由库表到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的整体架构分为三层: 1.基础支持层 该层包括:数据源模块、事务管理模块、缓存模块、Binding模块、反射模块、类型转换模块、日志模块、资源加载模块、解析器模块 2.核心处理层 该层包括:配置解析、参数映射、SQL解析、SQL执行、结果集映射、插件 3.接口层 该层包括:SqlSession 基础支持层 该层保护mybatis的基础模块,它们为核心处理层提供了良好的支撑。

建立升序链表

题目1181:遍历链表 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2744 解决:1186 题目描述: 建立一个升序链表并遍历输出。 输入: 输入的每个案例中第一行包括1个整数:n(1<=n<=1000),接下来的一行包括n个整数。 输出: 可能有多组测试数据,对于每组数据, 将n个整数建立升序链表,之后遍历链表并输出。 样例输

Spring+MyBatis+jeasyui 功能树列表

java代码@EnablePaging@RequestMapping(value = "/queryFunctionList.html")@ResponseBodypublic Map<String, Object> queryFunctionList() {String parentId = "";List<FunctionDisplay> tables = query(parent

Mybatis中的like查询

<if test="templateName != null and templateName != ''">AND template_name LIKE CONCAT('%',#{templateName,jdbcType=VARCHAR},'%')</if>

JavaWeb【day09】--(Mybatis)

1. Mybatis基础操作 学习完mybatis入门后,我们继续学习mybatis基础操作。 1.1 需求 需求说明: 根据资料中提供的《tlias智能学习辅助系统》页面原型及需求,完成员工管理的需求开发。 通过分析以上的页面原型和需求,我们确定了功能列表: 查询 根据主键ID查询 条件查询 新增 更新 删除 根据主键ID删除 根据主键ID批量删除

MyBatis 切换不同的类型数据库方案

下属案例例当前结合SpringBoot 配置进行讲解。 背景: 实现一个工程里面在部署阶段支持切换不同类型数据库支持。 方案一 数据源配置 关键代码(是什么数据库,该怎么配就怎么配) spring:datasource:name: test# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSource# @需要修改 数据库连接及驱动u

web群集--nginx配置文件location匹配符的优先级顺序详解及验证

文章目录 前言优先级顺序优先级顺序(详解)1. 精确匹配(Exact Match)2. 正则表达式匹配(Regex Match)3. 前缀匹配(Prefix Match) 匹配规则的综合应用验证优先级 前言 location的作用 在 NGINX 中,location 指令用于定义如何处理特定的请求 URI。由于网站往往需要不同的处理方式来适应各种请求,NGINX 提供了多种匹

前端-06-eslint9大变样后,如何生成旧版本的.eslintrc.cjs配置文件

目录 问题解决办法 问题 最近在写一个vue3+ts的项目,看了尚硅谷的视频,到了配置eslintrc.cjs的时候我犯了难,因为eslint从9.0之后重大更新,跟以前完全不一样,但是我还是想用和老师一样的eslintrc.cjs文件,该怎么做呢? 视频链接:尚硅谷Vue项目实战硅谷甄选,vue3项目+TypeScript前端项目一套通关 解决办法 首先 eslint 要

JavaFX环境的搭建和一个简单的例子

之前在网上搜了很多与javaFX相关的资料,都说要在Eclepse上要安装sdk插件什么的,反正就是乱七八糟的一大片,最后还是没搞成功,所以我在这里写下我搭建javaFX成功的环境给大家做一个参考吧。希望能帮助到你们! 1.首先要保证你的jdk版本能够支持JavaFX的开发,jdk-7u25版本以上的都能支持,最好安装jdk8吧,因为jdk8对支持JavaFX有新的特性了,比如:3D等;

Caused by: org.hibernate.MappingException: Could not determine type for: org.cgh.ssh.pojo.GoodsType,

MappingException:这个主要是类映射上的异常,Could not determine type for: org.cgh.ssh.pojo.GoodsType,这句话表示GoodsType这个类没有被映射到