相关文章推荐
气宇轩昂的紫菜  ·  群晖6.2 安装qbittorrent ...·  5 月前    · 
打篮球的烤面包  ·  c++如何将一个类序列化再反序列化 - ...·  1 年前    · 
礼貌的椰子  ·  go 中解析JSON的三种姿势 - ...·  1 年前    · 
活泼的松鼠  ·  java用CollectionUtils工具 ...·  1 年前    · 
坚强的椅子  ·  php 字符转ansi,php – ...·  1 年前    · 
Code  ›  MyBatis逆向工程的使用(十二)-天翼云
mybatis main函数 天翼云 逆向工程
https://www.ctyun.cn/zhishi/p-224927
不爱学习的伏特加
2 年前

最新活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
查看全部活动
热门活动
  • 618翼购节 HOT S2云主机1核2G只要33.9元起/3个月
  • 云上钜惠 HOT 限制抢购 爆款云主机畅享3个月只要33元起!
  • 中小企业上云场景组合购 NEW 建站、域名等场景一站式购齐,组合购9元起!
  • 天翼云·创新直播间 NEW 与专家共同解读云计算领域前沿趋势
  • 新客特惠 NEW 热销云主机秒杀低至0.85折!
  • 天翼云电脑专场 HOT 移动办公神器天翼云电脑4核8G畅享3个月只要130元起!
  • 云主机特惠 HOT 新老同享云主机2.5折起,升级高性能云主机更划算!
免费活动
  • 企业免费试用专区 HOT 数十款产品免费试用,完成企业实名认证领取2000元试用金!
  • 个人免费试用专区 HOT 完成个人实名认证即可获得1000元试用金!
  • 新客特惠 爆款云主机0.85折起
  • 云上安全专场 安全产品全场6折起!

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务。
进入甄选商城 进入云市场
办公协同
  • WPS云文档天翼云版 翼企云( License版) 天翼云企业云盘(标准服务版) 蓝信安全移动工作平台(SAAS) SSL证书 新域名服务 税务风控云 商企云财税(SAAS版) 翼云加速-标准版包月套餐 天翼云管家2.0 天翼云管家 翼备份(SaaS版)300G 智慧工厂_生产流程管理解决方案 天翼智慧工地 天翼全栈混合云敏捷版(软件) 天翼全栈混合云敏捷版(一体机)

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云技术合作伙伴
  • 天翼云OpenAPI中心 天翼云诸葛AI平台 天翼云EasyCoding平台
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云培训认证
  • 天翼云学堂 天翼云市场商学院
天翼云服务合作伙伴
  • 天翼云MSP合作伙伴
天翼云合作计划
  • 央企云合作计划
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴 天翼云甄选商城合作伙伴

开发者

开发者相关功能入口汇聚
技术社区
资源与工具
  • 天翼云OpenAPI EasyCoding敏捷开发平台 AI创作间
培训与认证
  • 天翼云学堂 天翼云认证

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • OpenAPI中心 客户支持计划

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云 天翼云4.0 天翼云APP 全球基础设施 分析师和白皮书 2023云生态大会 2022云生态大会 天翼云中国行 支持与服务 了解天翼云
    • 弹性云主机
    • 天翼云电脑(公众版)
    • 对象存储
    搜索发现
    弹性云主机 天翼云电脑(公众版) 对象存储 云硬盘 边缘安全加速平台 云专线CDA 服务器安全卫士 云堡垒机 关系数据库MySQL版 关系数据库PostgreSQL版
  • 站点切换
    • 中国站
  • 文档
  • 控制中心
  • 备案中心
  • 管理中心

MyBatis逆向工程的使用(十二)

首页 知识中心 其他 文章详情页

MyBatis逆向工程的使用(十二)

2023-6-7 阅读次数:26

java, MyBatis, sql

一. MyBatis 逆向工程

MyBatis 逆向工程,存放于github, 就是通过数据库表,来自动生成 POJO 实体类和 mapper 中的XxxMapper.java 和XxxMapper.xml 配置文件,只能对单表进行相应的操作,暂时不能生成关联表的映射。 老蝴蝶本文所用的是 1.3.2 版本。

解压之后:

其中,lib包下有:

以mybatis数据库表下的四个表为例。 前面讲关联关系时所用到的表。

为了避免出现文件覆盖,最好是将自动生成代码的逆向工程单纯创建一个项目,然后copy 自动生成的文件到mybatis 项目中。

二. MyBatis 的逆向工程

二.一 创建逆向工程项目 MyBatis_Generator,避免出现文件覆盖。

二.二 创建lib文件夹,放入jar包。

二.三 在mybatis_generator项目下放置generatorConfig.xml 配置文件

注意,放置在整个项目下,与src 同级,并不是放置在src目录下。

二.四 在generator包下创建 GeneratorSqlmap.java 类来自动生成代码

package generator;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

public void generator() throws Exception{

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定文件名,注意路径。
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);

}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}

}
}

二.五 创建好逆向工程的整个目录结构

二.六 generatorConfig.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
<context id="mybatisTest" targetRuntime="MyBatis3">

<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
<!--去除日期注释-->
<property name="suppressDate" value="true"/>
</commentGenerator>

<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
password="abc123">
</jdbcConnection>

<!-- 关于oracle的数据库连接信息 -->
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
userId="scott"
password="abc123">
</jdbcConnection> -->

<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- targetProject:生成POJO类的位置 -->
<javaModelGenerator targetPackage="com.yjl.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>

<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.yjl.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>

<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yjl.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>

<!-- 指定数据库表,哪些单表-->
<table tableName="user"></table>
<table tableName="dept"></table>
<table tableName="role"></table>
<table tableName="idcard"></table>
<!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>

其中,关于generatorConfig.xml 配置文件详细解释,可以参考 :​​javascript:void(0)​​ (不知道这位前辈的转载原处地址,所以无法贴出正确的原始地址,还请理解)。

二.七 运行GeneratorSqlmap 类,看控制台输出。

老蝴蝶的输出是: 其中column 为那些表下的列名。 根据不同的表,会有不同的输出情况。

2019-07-16 19:24:08,106 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "user"
2019-07-16 19:24:08,122 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis..user"
2019-07-16 19:24:08,122 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "mybatis..user"
2019-07-16 19:24:08,122 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "age", data type 4, in table "mybatis..user"
2019-07-16 19:24:08,123 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "sex", data type 12, in table "mybatis..user"
2019-07-16 19:24:08,123 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "description", data type 12, in table "mybatis..user"
2019-07-16 19:24:08,125 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "dept"
2019-07-16 19:24:08,128 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis..dept"
2019-07-16 19:24:08,128 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "mybatis..dept"
2019-07-16 19:24:08,128 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "description", data type 12, in table "mybatis..dept"
2019-07-16 19:24:08,130 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "role"
2019-07-16 19:24:08,133 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis..role"
2019-07-16 19:24:08,133 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "mybatis..role"
2019-07-16 19:24:08,133 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "idcard"
2019-07-16 19:24:08,137 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "mybatis..idcard"
2019-07-16 19:24:08,137 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "idnum", data type 12, in table "mybatis..idcard"

刷新项目,F5, 便可以观察到自动生成的代码。

将 mapper 和pojo 包下 copy 到MyBatis 项目中即可。

三. 自动生成的代码解析

三.一 POJO 类

1.pojo 类会根据表名(首字母大写) 创建相应的类名。 如user—>变成了User.java 类。

2. 会将user 表中的column 类名 变成类中的属性,默认为包装类型,并且创建setter和getter方法。

3. setter方法,如果在generatorConfig.xml 设置了

那么 setter 方法为:

public void setName(String name) {
this.name = name == null ? null : name.trim();
}
  • orderByClause , 设置排序, 单个的 “id desc”, 多个的"age desc,id asc" 。 即order by 后面的排序语句。
  • distinct 是否去重, 是select distinct 中的distinct, 是个boolean 值。
  • oredCriteria, 是条件的集合,用于添加不同的条件。
  • <!-- 定义表的resultMap, 并且指定属性的jdbcType -->
    <resultMap id="BaseResultMap" type="com.yjl.pojo.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="sex" property="sex" jdbcType="VARCHAR" />
    <result column="description" property="description" jdbcType="VARCHAR" />
    </resultMap>

    2 . 定义查询条件的sql片段,用于拼接 where 后的查询条件。

    <!--拼接 查询时所用的sql片段-->
    <sql id="Example_Where_Clause" >
    <where >
    <foreach collection="oredCriteria" item="criteria" separator="or" >
    <if test="criteria.valid" >
    <trim prefix="(" suffix=")" prefixOverrides="and" >
    <foreach collection="criteria.criteria" item="criterion" >
    <choose >
    <when test="criterion.noValue" >
    and ${criterion.condition}
    </when>
    <when test="criterion.singleValue" >
    and ${criterion.condition} #{criterion.value}
    </when>
    <when test="criterion.betweenValue" >
    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
    </when>
    <when test="criterion.listValue" >
    and ${criterion.condition}
    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
    #{listItem}
    </foreach>
    </when>
    </choose>
    </foreach>
    </trim>
    </if>
    </foreach>
    </where>
    </sql>

    3 . 定义更新条件的sql 片段,用于拼接 where 后的更新条件。

    <!--定义更新时,所用的sql片段-->
    <sql id="Update_By_Example_Where_Clause" >
    <where >
    <foreach collection="example.oredCriteria" item="criteria" separator="or" >
    <if test="criteria.valid" >
    <trim prefix="(" suffix=")" prefixOverrides="and" >
    <foreach collection="criteria.criteria" item="criterion" >
    <choose >
    <when test="criterion.noValue" >
    and ${criterion.condition}
    </when>
    <when test="criterion.singleValue" >
    and ${criterion.condition} #{criterion.value}
    </when>
    <when test="criterion.betweenValue" >
    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
    </when>
    <when test="criterion.listValue" >
    and ${criterion.condition}
    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
    #{listItem}
    </foreach>
    </when>
    </choose>
    </foreach>
    </trim>
    </if>
    </foreach>
    </where>
    </sql>

    4 . 定义基本的列片段,方便查询使用。

    <!-- 定义 基本的列片段,方便查询使用 -->
    <sql id="Base_Column_List" >
    id, name, age, sex, description
    </sql>

    接下来,就是具体的查询方法了。

    5 . selectByExample

    <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.yjl.pojo.UserExample" >
    select
    <!-- 看是否去重,为boolean 值 -->
    <if test="distinct" >
    distinct
    </if>
    <include refid="Base_Column_List" />
    from user
    <if test="_parameter != null" >
    <include refid="Example_Where_Clause" />
    </if>
    <!-- 有值,就进行排序,拼接排序后的字段 -->
    <if test="orderByClause != null" >
    order by ${orderByClause}
    </if>
    </select>

    6 . selectByPrimaryKey

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
    </select>

    7 . deleteByPrimaryKey

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where id = #{id,jdbcType=INTEGER}
    </delete>

    8 . deleteByExample

    <delete id="deleteByExample" parameterType="com.yjl.pojo.UserExample" >
    delete from user
    <if test="_parameter != null" >
    <include refid="Example_Where_Clause" />
    </if>
    </delete>

    9 . insert 插入全部的字段

    <insert id="insert" parameterType="com.yjl.pojo.User" >
    insert into user (id, name, age,
    sex, description)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
    #{sex,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR})
    </insert>

    10 . insertSelective

    <insert id="insertSelective" parameterType="com.yjl.pojo.User" >
    insert into user
    <!--有选择性的拼接属性-->
    <trim prefix="(" suffix=")" suffixOverrides="," >
    <if test="id != null" >
    id,
    </if>
    <if test="name != null" >
    name,
    </if>
    <if test="age != null" >
    age,
    </if>
    <if test="sex != null" >
    sex,
    </if>
    <if test="description != null" >
    description,
    </if>
    </trim>
    <!--拼接value后半部分-->
    <trim prefix="values (" suffix=")" suffixOverrides="," >
    <if test="id != null" >
    #{id,jdbcType=INTEGER},
    </if>
    <if test="name != null" >
    #{name,jdbcType=VARCHAR},
    </if>
    <if test="age != null" >
    #{age,jdbcType=INTEGER},
    </if>
    <if test="sex != null" >
    #{sex,jdbcType=VARCHAR},
    </if>
    <if test="description != null" >
    #{description,jdbcType=VARCHAR},
    </if>
    </trim>
    </insert>

    11 . countByExample

    <select id="countByExample" parameterType="com.yjl.pojo.UserExample" resultType="java.lang.Integer" >
    select count(*) from user
    <if test="_parameter != null" >
    <include refid="Example_Where_Clause" />
    </if>
    </select>

    12 . updateByExampleSelective

    <update id="updateByExampleSelective" parameterType="map" >
    update user
    <set >
    <!-- 有,才进行更新 -->
    <if test="record.id != null" >
    id = #{record.id,jdbcType=INTEGER},
    </if>
    <if test="record.name != null" >
    name = #{record.name,jdbcType=VARCHAR},
    </if>
    <if test="record.age != null" >
    age = #{record.age,jdbcType=INTEGER},
    </if>
    <if test="record.sex != null" >
    sex = #{record.sex,jdbcType=VARCHAR},
    </if>
    <if test="record.description != null" >
    description = #{record.description,jdbcType=VARCHAR},
    </if>
    </set>
    <!--拼接更新 where 语句-->
    <if test="_parameter != null" >
    <include refid="Update_By_Example_Where_Clause" />
    </if>
    </update>

    13 . updateByExample

    <update id="updateByExample" parameterType="map" >
    update user
    set id = #{record.id,jdbcType=INTEGER},
    name = #{record.name,jdbcType=VARCHAR},
    age = #{record.age,jdbcType=INTEGER},
    sex = #{record.sex,jdbcType=VARCHAR},
    description = #{record.description,jdbcType=VARCHAR}
    <if test="_parameter != null" >
    <include refid="Update_By_Example_Where_Clause" />
    </if>
    </update>

    14 . updateByPrimaryKeySelective

    <update id="updateByPrimaryKeySelective" parameterType="com.yjl.pojo.User" >
    update user
    <set >
    <if test="name != null" >
    name = #{name,jdbcType=VARCHAR},
    </if>
    <if test="age != null" >
    age = #{age,jdbcType=INTEGER},
    </if>
    <if test="sex != null" >
    sex = #{sex,jdbcType=VARCHAR},
    </if>
    <if test="description != null" >
    description = #{description,jdbcType=VARCHAR},
    </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
    </update>

    15 . updateByPrimaryKey

    <update id="updateByPrimaryKey" parameterType="com.yjl.pojo.User" >
    update user
    set name = #{name,jdbcType=VARCHAR},
    age = #{age,jdbcType=INTEGER},
    sex = #{sex,jdbcType=VARCHAR},
    description = #{description,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
    </update>

    这就是XxxMapper.xml 自动生成的主要代码。

    四. 测试

    四.一 insert() 方法的测试

    @Test
    public void insertF1Test(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    User user=new User();
    //不设置id,就报id为null的错误。 必须手动填写id
    user.setId(7);
    user.setName("自动生成插入名称");
    user.setSex("自动生成插入性别");
    user.setAge(24);
    user.setDescription("自动生成插入描述");
    userMapper.insert(user);
    sqlSession.commit();
    System.out.println(user);
    }

    四.二 insertSelective() 方法的测试

    @Test
    public void insertF2Test(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    User user=new User();
    user.setName("自动生成插入名称");
    user.setSex("自动生成插入性别");
    user.setAge(24);
    user.setDescription("自动生成插入描述");
    int result=userMapper.insertSelective(user);
    sqlSession.commit();
    //可以自动生成,但不会返回id .
    System.out.println(user); //并不会打印出user中 id的值,不会回显。
    System.out.println("输出插入后的id:"+result); //1
    }

    四.三 updateByPrimaryKey() 方法的测试

    @Test
    public void updateF1Test(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    User user=new User();
    user.setId(8);
    user.setName("自动修改插入名称");
    user.setSex("自动修改插入性别");
    user.setDescription("自动生成插入描述");
    userMapper.updateByPrimaryKey(user);
    sqlSession.commit();
    System.out.println(user);
    }

    四.四 updateByPrimaryKeySelective() 方法的测试

    @Test
    public void updateF2Test(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    User user=new User();
    user.setId(8);
    user.setName("自动修改插入名称");
    user.setSex("自动修改插入性别123");
    userMapper.updateByPrimaryKeySelective(user);
    sqlSession.commit();
    //可以自动生成,但不会返回id .
    System.out.println(user);
    }

    四.五 updateByExampleSelective() 方法的测试

    @Test
    public void updateF3Test(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    User user=new User();
    user.setId(8);
    user.setName("自动条件插入名称");
    user.setSex("自动条件插入性别123");
    user.setAge(26);
    UserExample example=new UserExample();
    Criteria criteria=example.createCriteria();
    criteria.andIdEqualTo(8);
    criteria.andAgeIsNull();
    userMapper.updateByExampleSelective(user,example);
    sqlSession.commit();
    //可以自动生成,但不会返回id .
    System.out.println(user);
    }

    四.六 updateByExample() 方法的测试

    @Test
    public void updateF4Test(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    User user=new User();
    user.setId(8);
    user.setName("自动条件插入名称");
    user.setAge(26);
    UserExample example=new UserExample();
    Criteria criteria=example.createCriteria();
    criteria.andIdEqualTo(8);
    criteria.andAgeIsNull();
    userMapper.updateByExample(user,example);
    sqlSession.commit();
    //可以自动生成,但不会返回id .
    System.out.println(user);
    }

    四.七 countByExample() 方法的测试

    @Test
    public void countByExampleTest(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    UserExample example=new UserExample();
    Criteria criteria=example.createCriteria();
    criteria.andAgeEqualTo(24);
    criteria.andSexEqualTo("男");
    int count=userMapper.countByExample(example);
    System.out.println("数目是:"+count);
    }

    四.八 selectByExample() 方法的测试

    @Test
    public void selectByExampleTest(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    UserExample example=new UserExample();
    Criteria criteria=example.createCriteria();
    criteria.andAgeEqualTo(24);
    criteria.andSexEqualTo("男");
    List<User> userList=userMapper.selectByExample(example);
    userList.forEach(n ->System.out.println(n));
    }

    四。九 deleteByExample() 方法的测试

    @Test
    public void deleteTest(){
    SqlSession sqlSession=SqlSessionFactoryUtils.getSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    UserExample example=new UserExample();
    Criteria criteria=example.createCriteria();
    criteria.andIdEqualTo(8);
    criteria.andAgeIsNull();
    userMapper.deleteByExample(example);
    sqlSession.commit();
    }

    其他的方法与其类似,就不讲述了。

    谢谢!!!

    版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/yueshushu/5841094,作者:两个蝴蝶飞,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

    上一篇: mybatis和spring整合---Demo1(超详细)

    下一篇: MyBatis的动态Sql查询(七)

    相关文章

    2023-6-8

    JSP原理深度刨析

    1. 我的第一个JSP程序  351.1 原理  35 在WEB-INF目录之外创建一个index.jsp文件,然后这个文件中没有任何内容。- 将上面的项目部署之后,启动服务器,打开浏览器,访问以下地址

    2023-6-10

    如何将未开源的jar包用在Maven项目中

    如何将未开源的jar包用在Maven项目中

    maven
    2023-6-7

    MyBatis-核心配置文件

    properties用来定义属性及读取属性文件。settings这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。typeAliases类型别名是为 Java 类型设置一个短的名字。定义单个别名批量别名定

    mapper MyBatis
    2023-6-7

    完整的Mybatis程序(Junit的使用+日志主键+工具类)

    1. 代码   12main中 com.powernode.mybatis.testMyBatisCompleteTestresources下的mybatis-config.xmlCarMapper.xml2. Junit的使用   13/

    Mybatis
    2023-6-7

    mybatis核心配置文件

    1. configuration:根标签,表示配置信息。 24environments:环境(多个),以“s”结尾表示复数,也就是说mybatis的环境可以配置多个数据源。default属性:表示默认使⽤的是哪个环境,default后⾯填写

    mybatis
    2023-6-10

    idea中设置maven和依赖管理以及常用属性

    1. IDEA 集成 Maven  191.1 在idea中设置maven ,让idea和maven结合使用。19idea中内置了maven ,一般不使用内置的, 因为用内置修改maven的设置不方便。使用自己安装的maven, 需要覆盖i

    maven
    2023-6-8

    单表的CRUD操作升级

    1.  使用模板方法设计模式优化oa项目  34- 上面的注解解决了配置文件的问题。但是现在的oa项目仍然存在一个比较臃肿的问题。  - 一个单标的CRUD,就写了6个Servlet。如果一个复杂的业务系统,这种开发方式,显然会导致类爆炸。

    2023-6-10

    Maven将下载的jar包导入到本地仓库里去

    Maven将下载的jar包导入到本地仓库里去

    maven
    2023-6-19

    git error 已解决【Another git process seems to be running in this repository, e.g. an editor opened by】

    文章目录​​异常​​​​原因​​​​解决办法​​​​图解​​异常原因 很明显,文件被锁住了,删除文件 ​​​.git/index. lock​​解决办法删除文件 ​​.git/index. lock​​重新提交图解

    github
    2023-6-16

    Tomcat启动时加载本地证书出错的问题

    错误信息的上下文:INFO: Initializing ProtocolHandler[“http-bio-9099”]Dec 11, 2019 5:28:45 PM org. apache. tomcat. util. net. jsse

    linux tomcat

    作者介绍

    天翼云小翼
    天翼云用户

    文章

    19525

    阅读量

    893162

    查看更多

    最新文章

    JSP原理深度刨析

    2023-6-8

    如何将未开源的jar包用在Maven项目中

    2023-6-10

    Maven将下载的jar包导入到本地仓库里去

    2023-6-10

    git error 已解决【Another git process seems to be running in this repository, e.g. an editor opened by】

    2023-6-19

    Tomcat启动时加载本地证书出错的问题

    2023-6-16

    Tomcat启动startup.bat一闪而过就消失和乱码的解决方法

    2023-6-16

    查看更多

    热门文章

    GC是什么? 为什么要有GC?

    2023-3-22

    在java正则表达式中为什么要对 . 进行两次的转义操作 (\\.)才表示真正的 .

    2023-3-22

    docker-compose下的java应用启动顺序两部曲之一:问题分析

    2023-5-11

    axios&spring前后端分离传参规范总结

    2023-5-21

    Jsp Ajax之模拟用户注册

    2022-11-9

    定义一个函数,接收三个参数返回一元二次方程

    2023-1-3

    查看更多

    热门标签

    linux python docker Linux javascript kubernetes elasticsearch centos maven github android
    查看更多

    相关产品

    弹性云主机

    随时自助获取、弹性伸缩的云服务器资源

    天翼云电脑(公众版)

    便捷、安全、高效的云电脑服务

    对象存储

    高品质、低成本的云上存储服务

    云硬盘

    为云上计算资源提供持久性块存储

    查看更多

    随机文章

    在java正则表达式中为什么要对 . 进行两次的转义操作 (\\.)才表示真正的 .

    java学习——面向对象

    react18-学习笔记16-约束泛型

    docker-compose下的java应用启动顺序两部曲之一:问题分析

    强记 分支结构语法 java 230217

    #yyds干货盘点# Lc131_分割回文串

    推荐助力领好礼 邀请好友完成推荐任务 最高可得千元电信卡
    查看详情
    • 7*24小时售后
    • 无忧退款
    • 免费备案
    • 专家服务
    售前咨询热线
    400-810-9889转1
    • 天翼云APP 微信公众号
    服务与支持
    • 备案中心
    • 售前咨询
    • 智能客服
    • 自助服务
    • 工单管理
    • 客户公告
    账户管理
    • 管理中心
    • 订单管理
    • 余额管理
    • 发票管理
    • 充值汇款
    • 续费管理
    快速入口
    • 文档中心
    • 最新活动
    • 免费试用
    • 安全专区
    • 信任中心
    • 天翼云学堂
    云网生态
    • 甄选商城
    • 渠道合作
    • 云市场合作
    了解天翼云
    • 关于天翼云
    • 天翼云APP
    • 服务案例
    • 新闻资讯
    • 联系我们
    • 校园招聘
    热门产品
    • 弹性云主机
    • 边缘安全加速平台
    • 天翼云电脑
    • 天翼云手机
    • 关系型数据库
    • 对象存储
    • 云硬盘
    • Web应用防火墙
    • 服务器安全卫士
    • 天翼云办公
    热门推荐
    • 云服务备份
    • 云空间
    • 全站加速
    • 安全加速
    • 超融合服务-iStack
    • 智能边缘云
    • 应用编排服务
    • 微服务引擎
    • 共享流量包
    更多推荐
    • web应用防火墙
    • 密钥管理
    • 等保咨询
    • 安全专区
    • 应用运维管理
    • 云日志服务
    • 云网融合
    • 云搜索服务
    • 数据湖探索
    • 数据仓库服务
    友情链接
    • 天翼云盘
    • 天翼企业云盘
    • 189邮箱
    • 中国电信集团
    ©2023 京ICP备 2021034386号
     
    推荐文章
    气宇轩昂的紫菜  ·  群晖6.2 安装qbittorrent | 群晖 entware OPK-CSDN博客
    5 月前
    打篮球的烤面包  ·  c++如何将一个类序列化再反序列化 - CSDN文库
    1 年前
    礼貌的椰子  ·  go 中解析JSON的三种姿势 - Go和分布式IM - 博客园
    1 年前
    活泼的松鼠  ·  java用CollectionUtils工具类判断集合是否为空 - 哔哩哔哩
    1 年前
    坚强的椅子  ·  php 字符转ansi,php – 将UTF-8转换为ANSI(windows-1252)-CSDN博客
    1 年前
    今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
    删除内容请联系邮箱 2879853325@qq.com
    Code - 代码工具平台
    © 2024 ~ 沪ICP备11025650号