在SSM架构中,mybatis被选择作为持久层框架来实现pojo和对象之间的映射,持久层可以将数据业务存储到磁盘,具备长期的储存能力,只要磁盘不损坏,数据具有很好的复现性,持久层可以使用巨大的磁盘空间,他的缺点就是比较慢,相对内存而言的,然而,通过了解,发现其有3个较好的优势:
1.不屏蔽SQL,意味着可以更为精确的定位SQL语句,可以对其进行优化和改造,还有利于互联网系统整体性能的提高。
2.提供强大的、灵活的映射机制,方便Java开发人员的使用,提供动态SQL的功能,允许我们根据不同的条件组装SQL语句,远远比其他工具和Java代码编写的可维护程度更高,也满足了互联网应用多变的需要。
3.在Mybatis中,提供了使用Mapper的接口编程,只要一个接口可以个XML文档,即可创建映射器,简化工作,使得很多的框架的API在Mybatis中消失,开发者更能够集中精力,开发业务逻辑。
这节的内容不是讲述Mybatis的,我们只是用Mybatis的一个逆向工程来生成数据库中对应表格的映射关系,方便我们很快的生成pojo对象和建立Mapper接口,下面是在newstyles工程中的应用:
首先创建一个Java工程,同时添加参考的库,也就是说,下面的文件是这个工程需要添加的外部文件,库中包含log4j,mybatis,mybatis-generateor-core,mysql-connector-java的jar包,因为是Mybatis逆向生成,所以同mybatis的core和generator有关,同时是根据数据库的表格等生成,所以需要连接mysql数据库,这里就应用了mysql-connector的jar包。使用log4j是为了打印输出内容。
在src目录下喜欢建一个默认包,在包里面写一个java类,类的设计如下:
public class GeneratorSqlmap {
//generator方法,用于生成代码
public void generator() throws Exception{
//接收警告信息
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//读取配置文件,利用File类
File configFile = new File("generatorConfig.xml");
//来源于org.mybatis.generator包中
ConfigurationParser cp = new ConfigurationParser(warnings);
//读取配置文件
Configuration config = cp.parseConfiguration(configFile);
//来源于org.mybatis.generator.internal.DefaultShellCallback.DefaultShellCallback(boolean overwrite)
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();
}
}
}
前面提到过,要生成文件,还需要一个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>
<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/newstyles" userId="root"
password="***********">
</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.newstyles.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.newstyles.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.newstyles.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_desc"></table>
<table schema="" tableName="tb_item_param"></table>
<table schema="" tableName="tb_item_param_item"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_order_shipping"></table>
<table schema="" tableName="tb_user"></table>
</context>
</generatorConfiguration>
配置以后,运行一下:就可以生成所需的接口和对应的pojo类;分别将他们粘贴到newstyles-mapper和newstyles-pojo之中就可以了。生成的i文件可以看到其是在com.newstyles.pojo/mappper中的。其次还要注意,每次生成新的文件时,记得将上一次的文件删除,因为这个逆向工程不会覆盖掉原来的文件,而是在文件的后面进行追加的操作,这一点要注意一下。