Mybatis逆向工程
MyBatis逆向工程,就是通过相应插件,自动生成MyBatis数据库连接的一些文件
mybatis需要编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码–可以生成接口、实体类、映射文件
等,可以提高工作效率
入门案例
新创建一个模块
配置pom.xml
<dependencies>
<!--1 mybatis逆向工程核心包-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
配置mybatis-generator-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!--导入属性配置-->
<properties resource="jdbc.properties"></properties>
<!--指定特定数据库的jdbc驱动jar包的位置-->
<classPathEntry location="${jdbc.path}"/>
<context id="context" targetRuntime="MyBatis3">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}"/>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--指定包名生成实体类 以及生成的地址 (可以自定义地址,如果路径不存在会自动创建) -->
<javaModelGenerator targetPackage="com.dsf.domain.factory"
targetProject="E:\tools\workspace\export_parent\mybatis_generate\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的mapper文件 -->
<sqlMapGenerator targetPackage="com.dsf.dao.factory"
targetProject="E:\tools\workspace\export_parent\mybatis_generate\src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator targetPackage="com.dsf.dao.factory"
targetProject="E:\tools\workspace\export_parent\mybatis_generate\src\main\java" type="XMLMAPPER">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="saas-export" tableName="co_factory" domainObjectName="Factory" mapperName="IFactoryDao"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
创建jdbc.properties
jdbc.path=E:\\tools\\maven\\repository\\mysql\\mysql-connector-java\\5.1.6\\mysql-connector-java-5.1.6.jar
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/saas-export
jdbc.username=root
jdbc.password=123456
编写生成类Generate
public class Generate {
public void generator() throws Exception{
List<String> warnings=new ArrayList<String>();
boolean overwrite=true;
InputStream in=Generate.class.getClassLoader().getResourceAsStream("mybatis-generator-config.xml");
ConfigurationParser cp=new ConfigurationParser(warnings);
Configuration config=cp.parseConfiguration(in);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
in.close();
}
@Test
public void test01(){
try {
Generate generate=new Generate();
generate.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试接口
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-dao.xml")
public class IFactoryDaoTest{
@Autowired
IFactoryDao dao;
@Test
public void testDeleteByPrimaryKey() {
dao.deleteByPrimaryKey("53b2fd0f-3390-4fef-92e8-3c767c330f25");
}
@Test
public void testInsert() {
Factory factory=new Factory();
factory.setId("666");
factory.setCreateTime(new Date());
factory.setUpdateTime(new Date());
factory.setFullName("华为总公司");
dao.insert(factory);
System.out.println(factory);
}
@Test
public void testInsertSelective() {
Factory factory=new Factory();
factory.setId(UUID.randomUUID().toString());
factory.setUpdateTime(new Date());
factory.setCreateTime(new Date());
dao.insertSelective(factory);
System.out.println(factory);
}
@Test
public void testSelectByPrimaryKey() {
Factory factory=dao.selectByPrimaryKey("666");
System.out.println(factory);
}
public void testUpdateByPrimaryKeySelective() {
}
public void testUpdateByPrimaryKey() {
}
}