4.1使用逆向工程所需的工程模式:
4.2 GeneratorSqlmap.java
package com.eduask.generator;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
importorg.mybatis.generator.api.MyBatisGenerator;
importorg.mybatis.generator.config.Configuration;
importorg.mybatis.generator.config.xml.ConfigurationParser;
importorg.mybatis.generator.exception.XMLParserException;
importorg.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSqlmap {
publicvoid generator() throws Exception{
List
booleanoverwrite = true;
FileconfigFile = new File("generatorConfig.xml");
ConfigurationParsercp = new ConfigurationParser(warnings);
Configurationconfig = cp.parseConfiguration(configFile);
DefaultShellCallback callback = newDefaultShellCallback(overwrite);
MyBatisGeneratormyBatisGenerator = new MyBatisGenerator(config,
callback,warnings);
myBatisGenerator.generate(null);
}
publicstatic void main(String[] args) throws Exception {
try{
GeneratorSqlmapgeneratorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
}catch (Exception e) {
e.printStackTrace();
}
}
}
4.3 generatorConfig.xml
xml version="1.0"encoding="UTF-8"?>
DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables"targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"userId="root"
password="root">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
javaTypeResolver>
<javaModelGenerator targetPackage="com.eduask.pojo"
targetProject=".\src">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="com.eduask.mapper"
targetProject=".\src">
<property name="enableSubPackages" value="false" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.eduask.mapper"
targetProject=".\src">
<property name="enableSubPackages" value="false" />
javaClientGenerator>
<table tableName="user1">table>
context>
generatorConfiguration>
4.4 下面就开始编写建立数据库表:
4.5 执行GeneratorSqlmap.java代码:
4.6刷新工程,之后会看到:
4.7之后我们新建一个测试类,用于数据的增删改查;
代码如下:
packagecom.eduask.test;
//新建一个User1Test的测试类,用于数据的增删改查;
importjava.io.IOException;
importjava.io.InputStream;
importorg.apache.ibatis.io.Resources;
importorg.apache.ibatis.session.SqlSession;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.apache.ibatis.session.SqlSessionFactoryBuilder;
importorg.junit.Before;
importorg.junit.Test;
importcom.eduask.mapper.User1Mapper;
importcom.eduask.pojo.User1;
import com.eduask.pojo.User1Example;
public class User1Test {
//初始化;
private SqlSessionFactory sessionFactory;
@Before
public void up() throws IOException{
InputStream is=Resources.getResourceAsStream("SQLMapConfig.xml");
sessionFactory=new SqlSessionFactoryBuilder().build(is);
}
//user1Add
@Test
public void insertUser() throws IOException {
SqlSession session=sessionFactory.openSession();
User1Mapperuser1Mapper=session.getMapper(User1Mapper.class);
User1 u=new User1(1, "tom", "123456");
int result= user1Mapper.insert(u);
session.commit();
session.close();
if(result==1){
System.out.println("add success");
}else{
System.out.println("excute failure");
}
}
}
4.8数据库中查询: