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<String>warnings = new ArrayList<String>();
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>
<!-- 是否去除自动生成的注释 true:是: false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"userId="root"
password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.eduask.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.eduask.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.eduask.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<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数据库中查询: