mybatis-逆向工程

1,导包


  org.mybatis
  mybatis
  3.4.5




  org.mybatis.generator
  mybatis-generator-core
  1.3.5

2,配置文件
①,gengratorConfig.xml






    
    
        
    

    
    
    

    
    
        
    

    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
        
    

    
    
    

②,mybatis-config.xml


 




   
    
    
    



    



    
        
        
        
        
            
            
            
            
        
    



    


③,db.properties

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///appmanager?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=12345678

3,逆向工程生成

package com.study;

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;

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

/**
 * Created by Administrator on 2017/10/26.
 */
public class GeneratorSqlmap {
public void generator() throws Exception {
    List warnings = new ArrayList();
    boolean overwrite = true;
    // 指定逆向工程配置文件
    File configFile = new File("E:\\git\\mybatisgener\\src\\main\\resources\\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();
    }
}
}

4,测试

package com.study;

import com.study.dao.UserMapper;
import com.study.entity.User;
import com.study.entity.UserExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


/**
 * Created by Administrator on 2017/10/26.
 */
public class Mytest {

@Test
public void test1() throws IOException {
    InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    SqlSession session = factory.openSession();
    //查询全部
    /* List objects = session.selectList("com.study.dao.UserMapper.selectByExample");
    System.out.println(objects);*/

    UserMapper mapper = session.getMapper(UserMapper.class);
    //条件查询
   /* UserExample userExample=new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andUserNameEqualTo("admin").andUserPwdEqualTo("123456");
    List users = mapper.selectByExample(userExample);
    System.out.println(users);*/

    //插入
    User user=new User("libai","132");
    int insert = mapper.insert(user);
    System.out.println(insert);


}

}
 

                            
                        
                    
                    
                    

你可能感兴趣的:(mybatis-逆向工程)