Heim > Java > javaLernprogramm > Hauptteil

Erste Schritte mit MyBatis (7) ---Reverse Engineering

黄舟
Freigeben: 2016-12-21 14:37:36
Original
1472 Leute haben es durchsucht

1. Reverse Engineering

1.1. Übersicht

mybatis erfordert SQL, das durch die Programmnummer geschrieben wird.

mybatis bietet offiziell Reverse Engineering an, das automatisch den für die mybatis-Ausführung erforderlichen Code für eine einzelne Tabelle generieren kann

(Mapper, Java, Maper.xml, Po...)

Im Allgemeinen von der Datenbank zum Java-Code, der Generierungsprozess

2. JAR-Paket importieren

2.1, Mybatis-Generator

Erste Schritte mit MyBatis (7) ---Reverse Engineering

3 . Konfigurations-XML

3.1, generatorConfig.xml

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





                                                                                                                                                                                                                userId="root" passwort="root "> Der Standardwert ist falsch und die JDBC-Typen DECIMAL und NUMERIC werden als Ganzzahl analysiert. Wenn dies wahr ist, werden die JDBC-Typen DECIMAL
und NUMERIC als Eigenschaft java.math.BigDecimal -->
analysiert name ="forceBigDecimals" value="false" />
                                                           mybatis.po "
targetproject =" src "& gt;
& lt; prop on; = "TrimStrings" Value = "TRUE" /& gt; & lt;/javamodelgenerator & lt;-targetproject: Mapper-Zuordnung der Datei-& gt; Zuordnung "
targetProject=".src">
🎜>                                                                                                                                                >
targetPackage="com.pb.mybatis.mapper" targetProject=".src">                                                                                                                                                                ;
       











4. Führen Sie das Java-Programm aus, um

4.1, Java-Programm

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{

       Liste 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 my BatisGenerator = new MyBatisGenerator(config,
               Rückruf, Warnungen);
       myBatisGenerator.generate(null);

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


Erste Schritte mit MyBatis (7) ---Reverse Engineering

五、测试

5.1、测试类

 

Paket 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();        //可以加条件,不加条件默认查询全部
       Kriterien Kriterien=example.createCriteria();        //Erweiterte Kriterien für die Erstellung von Kriterien        criteria.andAuthorBioIsNotNull();        int num = AuthorMapper.CountByExample (Beispiel);
System.out.println ("num ="+num); AuthorExample example=new AuthorExample();        //可以加条件,不加条件默认查询全部
       Criteria Kriterien=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() {
       
      Autor 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();        //Kriterienkriterien=authorExample.createCriteria();        //需要手动加%
       criteria.andAuthorUsernameLike("%张三%");
       
       Liste list=authorMapper.selectByExample(authorExample);
       System.out.println(list.size());
   }    
   //根据主键ID查询    @Test    public void testSelectByPrimaryKey() {
      Autor author= authorMapper.selectByPrimaryKey(6);
       System.out.println(author.getAuthorUsername()+"..."+author.getAuthorBio());
   }

   @Test    public void testUpdateByExampleSelective() {
       fail("Noch nicht implementiert");
   }

   @Test    public void testUpdateByExample() {
       fail("Noch nicht implementiert");
}

   @Test    public void testUpdateByPrimaryKeySelective() {
       fail("Noch nicht implementiert");
   }

   @Test    public void testUpdateByPrimaryKey() {
       fail( „Noch nicht implementiert“);
  }

}

注PHP中文网(www.php.cn)! 


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!