Getting Started with MyBatis (7) ---Reverse Engineering
1. Reverse Engineering
1.1, Overview
mybatis requires SQL written by the program number.
mybatis officially provides reverse engineering, which can automatically generate the code required for mybatis execution for a single table
(mapper, java, maper.xml, po...)
is generally generated from database to java code. Process
2. Import jar package
2.1, mybatis-generator
3. Configure xml
3.1, generatorConfig.xml
br/>PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 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{ 五、测试 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; The above is the content of Getting Started with MyBatis (7)---Reverse Engineering. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!
"http:// /mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
"
"
" " "
; --> & gt; l & lt;/jdbcoconnection & gt;
& lt;-default False, analyze JDBC Decimal and NUMERIC as Integer, as to True, JDBC Decimal
and Numeric Types are parsed into java.math. .Bigdecimal -& gt;
& lt ;javaTypeResolver>
;javaModelGenerator targetPackage=" com.mybatis.po "
targetproject =". src "& gt;
& lt; proprity name =" enableSubpackages "value =" false " /& gt;
& lt; rimstrings "value =" true " /& gt;
& lt; proprity name = "enablesubpackages" value = "false" /& gt;
& lt;! Before and after space- -> =" XMLMAPPER"
targetPackage="com.pb.mybatis.mapper" ages" value="false" />
" " ="blog">
" "
4. Run the java program to generate
4.1, java program
List
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();
}
}
@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
System.out.println(list.size());
}
//Query based on primary key ID @Test public void testSelectByPrimaryKey() {
Author author= Mapper. 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");
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

JPA and MyBatis: Function and Performance Comparative Analysis Introduction: In Java development, the persistence framework plays a very important role. Common persistence frameworks include JPA (JavaPersistenceAPI) and MyBatis. This article will conduct a comparative analysis of the functions and performance of the two frameworks and provide specific code examples. 1. Function comparison: JPA: JPA is part of JavaEE and provides an object-oriented data persistence solution. It is passed annotation or X

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

Detailed explanation of how to use MyBatis batch delete statements requires specific code examples. Introduction: MyBatis is an excellent persistence layer framework that provides rich SQL operation functions. In actual project development, we often encounter situations where data needs to be deleted in batches. This article will introduce in detail how to use MyBatis batch delete statements, and attach specific code examples. Usage scenario: When deleting a large amount of data in the database, it is inefficient to execute the delete statements one by one. At this point, you can use the batch deletion function of MyBatis

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When

iBatis and MyBatis are two mainstream ORM (Object-Relational Mapping) frameworks. They have many similarities in design and use, but also have some subtle differences. This article will compare the similarities and differences between iBatis and MyBatis in detail, and illustrate their characteristics through specific code examples. 1. The history and background of iBatis and MyBatis iBatis is Apache Software Foundat

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values
