mybatis的逆向工程
mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说的dao接口,利用逆向工程,可以直接生成。
需要注意的是:mybatis自动生成的代码只能操作单表,如果是处理多表的话,需要自己写sql
前提:数据库的表已经创建完毕,逆向工程是由数据库表来生成的dao层。主要是进行数据库的通信和供service的调用,具体我们这几个步骤:
第一步:数据库表已经创建完成。
导入逆向工程出错原因:
mybatis的逆向工程有两个文件:
我们需要导入generatorSqlmapCustom这个工程文件才不会报错。
出错原因,由于使用mybatis的逆向工程不能重复生成相同名字的表,否则会报mapper错误,因为它会在原来的基础上再重复生成文件。
所以我们需要将逆向工程在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文件来实现查询。
下面例子:
@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;
}
相关文章
- SpringBoot入门之基于XML的Mybatis
- Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
- Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
- Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
- Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
- oracle+mybatis报错:BindingException("Invalid bound statement (not found): ")
- 二、在SSM框架中简单使用mybatis
- Mybatis常考面试题汇总(附答案)
- Java_myBatis_逆向工程
- Spring+Mybatis+SpringMVC+Maven+MySql搭建实例
- mybatis批量新增或更新
- MyBatis批量新增和更新
- Mybatis 3学习笔记(一)
- Atitit mybatis topic file list Total 300ge (9+条消息)MyBatis框架核心之(五)注解使用resultMap及多表查询 - 弱弱的猿 - CSD
- Atitit 数据库结果集映射 ResultSetHandler 目录 1. 常见的四种配置ResultSetHandler1 2. Dbutil1 3. Mybatis 致敬3 4. H
- Atitit mybatis业务流程配置化管理总结 目录 1. Mybatis1 2. 流程模型常见的bpm模式1 2.1. 活动task 流程,getway流程控制(分支跳转 循环等)1 3
- Atitit mybatis使用简明教程 目录 1.1. Mybatis.xml 配置文件1 1.2. Datamp.xml mapper文件主要作用是分模块放sql语句2 1.3. 查询
- Atitit mybatisutil 模块测试功能列表 1.1. mybatis读取mapper配置的sql语句 使用xml类库1 1.2. Mybatis替换参数工具类型3 Itfs pn
- springboot集成mybatis依赖,并且使用PageHelper实现分页效果
- 【MyBatis笔记11】Mybatis中的一级缓存和二级缓存
- Mybatis-Plus常用的查询方法
- 基于springboot+thymeleaf+mybatis-plus实现增删改查&分页查询
- 使用 SpringBoot 整合 MyBatis 开发 开启驼峰映射功能
- mybatis 批量 操作数据
- mybatis如何直接 执行传入的任意sql语句 并按照顺序取出查询的结果集
- Spring+SpringMVC+Mybatis(开发必备技能)04、mybatis自动生成mapper_dao_model(包含工具与视频讲解) 纯绿色版本、配套使用视频,100%运行成功
- Spring+SpringMVC+Mybatis(开发必备技能)05、task任务配置