mybatis的逆向工程是很大的减小了程序员对代码的编写工做,因为mybatis是半自动的sql语句使用,咱们在项目中通常都是采用逆向工程来生成mybatis的文件,mapper接口至关于咱们日常所说的dao接口,利用逆向工程,能够直接生成。java
须要注意的是:mybatis自动生成的代码只能操做单表,若是是处理多表的话,须要本身写sqlmysql
前提:数据库的表已经建立完毕,逆向工程是由数据库表来生成的dao层。主要是进行数据库的通讯和供service的调用,具体咱们这几个步骤:程序员
第一步:数据库表已经建立完成。web
导入逆向工程出错缘由:sql
mybatis的逆向工程有两个文件:数据库
咱们须要导入generatorSqlmapCustom这个工程文件才不会报错。api
出错缘由,因为使用mybatis的逆向工程不能重复生成相同名字的表,不然会报mapper错误,由于它会在原来的基础上再重复生成文件。mybatis
因此咱们须要将逆向工程在eclipse中删除掉,而后再从新导入从新生成。app
这个时候删除的时候注意:eclipse
有时候不经意把这个删除框勾选,结果把逆向工程的文件也给删掉了。下次导入的时候就找不到这个工程文件了。
第二步:在java工程中导入用来生成逆向工程的文件,一个java文件和一个.xml的文件。
通常咱们会封装成一个java的项目文件,而后直接导入web工程,执行后就能够自动生成。
例如这里的:
导入到项目中,
在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文件就会生成逆向文件:
会生成上面两个文件,红框中表示的是一个数据库表的生成类型
TbItemMapper.java主要是对数据库操做的一些接口,直接调用便可,TbItemMapper.xml是对sql语句的拼接,之后能够直接修改sql语句。
pojo类前面已经说过,其实就是指的一个javabean对象,用来实现持久层和控制层的调用。
对sql的查询除了固定的id查询外,还有自定义条件查询,即生成的TbItemExample.java的使用,由它生成criteria文件来实现查询。
下面例子:
@Servicepublic 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; }