搜索
首页Javajava教程MyBatis入门(七)---逆向工程

一、逆向工程

1.1、概述

mybatis需要程序号自己编写的SQL。

mybatis官方提供了逆向工程,可以针对单表自动生成mybatis执行所需要的代码

(mapper,java,maper.xml,po...)

一般都是由数据库到java代码, 的生成过程

二、导入jar包

2.1、mybatis-generator

47.jpg

三、配置xml

3.1、generatorConfig.xml

 

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
   
   

   
       
           
           
       


       
        connectionURL="jdbc:mysql://localhost:3306/mybatis?character=utf8"
userId="root" password="root">
       

       
       
           
       

       
        targetProject=".\src">
           
           
       

               
        targetProject=".\src">
           
           
           
           
       

   
        targetPackage="com.pb.mybatis.mapper" targetProject=".\src">
           
           
       

       
       


       

       


   

 

 

 

 

 

四、运行java程序生成

4.1、java程序

 

import java.io.File;import java.io.IOException;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.exception.XMLParserException;import org.mybatis.generator.internal.DefaultShellCallback;public class GeneratorSqlmap {    public void generator() throws Exception{

       List warnings = new ArrayList();        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();
       }
       
   }


48.jpg

五、测试

5.1、测试类

 

package com.pb.ssm.mapper;import static org.junit.Assert.fail;import java.util.Date;import java.util.List;import javax.crypto.Cipher;import org.junit.Before;import org.junit.Test;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pb.ssm.po.Author;import com.pb.ssm.po.AuthorExample;import com.pb.ssm.po.AuthorExample.Criteria;public class AuthorMapperTest {    private ApplicationContext applicationContext;    private AuthorMapper authorMapper;

   @Before    public void setUp() throws Exception {
       applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
       authorMapper=(AuthorMapper) applicationContext.getBean("authorMapper");
   }    
   //根据条件查询记录数    @Test    public void testCountByExample() {
       AuthorExample example=new AuthorExample();        //可以加条件,不加条件默认查询全部
       Criteria criteria=example.createCriteria();        //加条件,介绍不是空的        criteria.andAuthorBioIsNotNull();        int num=authorMapper.countByExample(example);
       System.out.println("num="+num);
   }    
   //根据条件删除    @Test    public void testDeleteByExample() {
       AuthorExample example=new AuthorExample();        //可以加条件,不加条件默认查询全部
       Criteria criteria=example.createCriteria();
       criteria.andAuthorUsernameEqualTo("程序员");        int num=authorMapper.deleteByExample(example);
       System.out.println("num="+num);
       
   }    
   //根据主键ID删除    @Test    public void testDeleteByPrimaryKey() {        int num=authorMapper.deleteByPrimaryKey(18);
       System.out.println("num="+num);
   }    //插入    @Test    public void testInsert() {
       
       Author author=new Author();
       author.setAuthorUsername("再测试一下");
       author.setAuthorPassword("admin123");
       author.setAuthorEmail("admin1234@QQ.com");        
       
       int num=authorMapper.insert(author);
       System.out.println("num="+num);        //这个方法插入,默认不会把数据库自增加ID返回,如果需要,可以手动增加
       System.out.println("插入后的ID"+author.getAuthorId());
       
   }    //插入    @Test    public void testInsertSelective() {
       Author author=new Author();
       author.setAuthorUsername("再测试一下");
       author.setAuthorPassword("admin123");
       author.setAuthorEmail("admin1234@qq.com");
       author.setRegisterTime(new Date());        
       
       int num=authorMapper.insert(author);
       System.out.println("num="+num);        //这个方法插入,默认不会把数据库自增加ID返回,如果需要,可以手动增加
       System.out.println("插入后的ID"+author.getAuthorId());
       
   }    //自定义 条件查询    @Test    public void testSelectByExample() {        //声明一个对象
       AuthorExample authorExample=new AuthorExample();        //创建criteria对象添加条件 and 连接
       Criteria criteria=authorExample.createCriteria();        //需要手动加%
       criteria.andAuthorUsernameLike("%张三%");
       
       List list=authorMapper.selectByExample(authorExample);
       System.out.println(list.size());
   }    
   //根据主键ID查询    @Test    public void testSelectByPrimaryKey() {
        Author author= authorMapper.selectByPrimaryKey(6);
       System.out.println(author.getAuthorUsername()+"..."+author.getAuthorBio());
   }

   @Test    public void testUpdateByExampleSelective() {
       fail("Not yet implemented");
   }

   @Test    public void testUpdateByExample() {
       fail("Not yet implemented");
   }

   @Test    public void testUpdateByPrimaryKeySelective() {
       fail("Not yet implemented");
   }

   @Test    public void testUpdateByPrimaryKey() {
       fail("Not yet implemented");
   }

}

 以上就是MyBatis入门(七)---逆向工程的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Go语言如何实现国密SM4和SM2算法的加解密以及互联互通?Go语言如何实现国密SM4和SM2算法的加解密以及互联互通?Apr 19, 2025 pm 06:27 PM

Go语言实现国密SM4和SM2加解密本文将详细介绍如何使用Go语言实现国密SM4和SM2算法的加解密流程,以满足与Java应�...

在Python项目中是否需要进行分层?在Python项目中是否需要进行分层?Apr 19, 2025 pm 06:24 PM

在Python项目中是否需要分层?最近我在学习Python时,注意到Django开源项目中,很多都在views函数里编写了大量的�...

如何使用MapStruct简化系统对接中的字段映射问题?如何使用MapStruct简化系统对接中的字段映射问题?Apr 19, 2025 pm 06:21 PM

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

IntelliJ IDEA是如何通过JavaAgent和RMI技术识别Spring Boot项目的端口号的?IntelliJ IDEA是如何通过JavaAgent和RMI技术识别Spring Boot项目的端口号的?Apr 19, 2025 pm 06:18 PM

IntelliJIDEA如何识别SpringBoot项目的端口号?在使用IntelliJIDEAUltimate版本启动Spring...

高效编程:如何才能找到可靠的代码工具和资源?高效编程:如何才能找到可靠的代码工具和资源?Apr 19, 2025 pm 06:15 PM

高效编程:寻找可靠的代码工具和资源很多程序员都渴望找到便捷的代码工具网站,以提高效率,避免在海量信...

JWT能否实现动态权限变更?与Session机制有何区别?JWT能否实现动态权限变更?与Session机制有何区别?Apr 19, 2025 pm 06:12 PM

关于JWT和Session的困惑与解答许多初学者在学习JWT和Session时,常常会对其本质和适用场景感到困惑。本文将围绕J...

Windows Server 2019防火墙如何正确配置才能支持WebSocket通信?Windows Server 2019防火墙如何正确配置才能支持WebSocket通信?Apr 19, 2025 pm 06:09 PM

WindowsServer2019防火墙与WebSocket通信问题详解在使用SpringBoot开发的Jar程序部署于WindowsServer2019...

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境