mybatis的逆向工程

时间:2023-03-10 06:05:54
mybatis的逆向工程

mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说的dao接口,利用逆向工程,可以直接生成。

前提:数据库的表已经创建完毕,逆向工程是由数据库表来生成的dao层。主要是进行数据库的通信和供service的调用,具体我们这几个步骤:

第一步:数据库表已经创建完成。

第二步:在java工程中导入用来生成逆向工程的文件,一个java文件和一个.xml的文件。

一般我们会封装成一个java的项目文件,然后直接导入web工程,执行后就可以自动生成。

例如这里的:mybatis的逆向工程

mybatis的逆向工程

mybatis的逆向工程

导入到项目中,mybatis的逆向工程

在GeneratorSqlmap.java中的代码是这样的,主要用来调用它的.XML文件:

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

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {

public void generator() throws Exception{

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;

File configFile = new File("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();
}

}

}

第二个文件是generatorConfig.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/taotao" 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.taotao.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置,这里要标明 -->
<sqlMapGenerator targetPackage="com.taotao.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.taotao.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>

在工程中,执行下.java文件就会生成逆向文件:

mybatis的逆向工程mybatis的逆向工程

会生成上面两个文件,红框中表示的是一个数据库表的生成类型

TbItemMapper.java主要是对数据库操作的一些接口,直接调用即可,TbItemMapper.xml是对sql语句的拼接,以后可以直接修改sql语句。

pojo类前面已经说过,其实就是指的一个javabean对象,用来实现持久层和控制层的调用。

对sql的查询除了固定的id查询外,还有自定义条件查询,即生成的TbItemExample.java的使用,由它生成criteria文件来实现查询。

下面例子:

@Service
public class ItemServiceImpl implements ItemService{
//这里的所说的mapper接口其实就是dao接口,所以我们需要引入商品的dao接口
@Autowired
private TbItemMapper itemMapper;
@Override
public TbItem getItemById(long itemId) {
//这是根据id进行查询:两种方法查询:方法一:TbItem item=itemMapper.selectByPrimaryKey(itemId);
//方法二:下面一个是根据条件进行查询
TbItemExample example=new TbItemExample();
//这样写不会报错,添加查询条件
TbItemExample.Criteria criteria=example.createCriteria();
criteria.andIdEqualTo(itemId);//这里会自动将itemId传入,生成sql语句。
List<TbItem> list= itemMapper.selectByExample(example);
if (list!=null &&list.size()>0) {
TbItem item=list.get(0);
return item;
}