06、MyBatis 逆向工程


|
ehcache-core-2.6.8.jar
log4j.jar
mybatis-3.4.1.jar
mybatis-ehcache-1.0.3.jar
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.1.37-bin.jar
ojdbc6.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.2.jar
|

<!-- <context>元素用于指定生成一组对象的环境。子元素用于指定要连接的数据库,要生成的对象的类型以及要进行自检的表。 -->
<!-- targetRuntime="MyBatis3Simple":生成简单版的CRUD -->
<!-- targetRuntime="MyBatis3":生成复杂版的CRUD -->
<!-- id:此上下文的唯一标识符。该值将在某些错误消息中使用。 -->
<context id="DB2Tables" targetRuntime="MyBatis3">
</context>
<!-- jdbcConnection:指定如何连接到目标数据库 -->
<!-- MyBatis Generator使用JDBC的DatabaseMetaData类来发现您在配置中指定的表的属性。-->
<!-- 每个<context>元素都需要一个<connectionFactory>或<jdbcConnection >元素。 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
userId="root"
password="root123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaModelGenerator:指定javaBean的生成策略 -->
<!-- targetProject:生成目标包名 -->
<!-- targetProject:目标工程 -->
<!-- http://mybatis.org/generator/configreference/javaModelGenerator.html -->
<javaModelGenerator targetPackage="com.atguigu.mybatis.bean"
targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
- 如果指定sqlMapGenerator,则MBG将仅生成SQL映射XML文件和模型类。
- 如果您未指定sqlMapGenerator,则MBG将仅生成模型类。
<!-- sqlMapGenerator:sql映射生成策略; -->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"
targetProject=".\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClientGenerator:指定mapper接口所在的位置 -->
<!-- http://mybatis.org/generator/configreference/javaClientGenerator.html -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.mybatis.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
- MyBatis格式化的SQL Map文件
- 构成表“模型”的一组类,包括:
- 一个与表的主键匹配的类(如果表具有主键)。
- 一个类,用于匹配表中不在主键中的字段和非BLOB字段。如果有,该类将扩展主键。
- 一个用于保存表中任何BLOB字段(如果有)的类。该类将扩展前两个类之一,具体取决于表的配置。
- 一个用于在不同的“按示例”方法(selectByExample,deleteByExample)中生成动态where子句的类。
- (可选)MyBatis映射器界面
<!-- 指定要逆向分析哪些表:根据表要创建javaBean -->
<!-- http://mybatis.org/generator/configreference/table.html -->
<table tableName="tbl_dept" domainObjectName="Department"></table>
<table tableName="tbl_employee" domainObjectName="Employee"></table>



@Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.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);
}
@Test
public void testSimple() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
List<Employee> list = mapper.selectAll();
for (Employee employee : list) {
System.out.println(employee.getId());
}
}finally {
openSession.close();
}
}
@Test
public void testMyBatis3() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//1.查询所有ID
List<Employee> emps = mapper.selectByExample(null);
// for(Employee employee:emps) {
// System.out.println(employee.getId());
// } //2.查询查询员工名字中有e字母的和员工性别是1的 封装员工查询条件的example
EmployeeExample example = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件;
Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%e%");
criteria.andGenderEqualTo("1"); Criteria criteria2 = example.createCriteria();
criteria2.andEmailLike("%e");
example.or(criteria2); List<Employee> list = mapper.selectByExample(example);
for(Employee employee:list) {
System.out.println(employee.getId());
} }finally{
openSession.close();
}
}
package com.atguigu.mybatis.test; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback; import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.bean.EmployeeExample;
import com.atguigu.mybatis.bean.EmployeeExample.Criteria;
import com.atguigu.mybatis.dao.EmployeeMapper; class MyBatisTest { public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.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);
} // @Test
// public void testSimple() throws IOException {
// SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// SqlSession openSession = sqlSessionFactory.openSession();
// try {
// EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
// List<Employee> list = mapper.selectAll();
// for (Employee employee : list) {
// System.out.println(employee.getId());
// }
// }finally {
// openSession.close();
// }
// } @Test
public void testMyBatis3() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
//1.查询所有ID
List<Employee> emps = mapper.selectByExample(null);
// for(Employee employee:emps) {
// System.out.println(employee.getId());
// } //2.查询查询员工名字中有e字母的和员工性别是1的 封装员工查询条件的example
EmployeeExample example = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件;
Criteria criteria = example.createCriteria();
criteria.andLastNameLike("%e%");
criteria.andGenderEqualTo("1"); Criteria criteria2 = example.createCriteria();
criteria2.andEmailLike("%e");
example.or(criteria2); List<Employee> list = mapper.selectByExample(example);
for(Employee employee:list) {
System.out.println(employee.getId());
} }finally{
openSession.close();
}
} }
06、MyBatis 逆向工程的更多相关文章
- mybatis0212 mybatis逆向工程 (MyBatis Generator)
1mybatis逆向工程 (MyBatis Generator) .1什么是mybatis的逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括生成 :mapper.xml.m ...
- 【JAVA - SSM】之MyBatis逆向工程的使用
MyBatis逆向工程可以方便的从数据库中将表自动映射到JAVA POJO类,并同时生成Mapper.xml和Mapper接口,方便实用.下面介绍一下逆向工程的使用方法. 使用逆向工程,我们最好是新建 ...
- Mybatis 逆向工程
Mybatis逆向工程: 推荐用Java和XML Configuration的方式生成逆向文件 Java类: package generation; import java.io.File; impo ...
- mybatis逆向工程
一.背景 在实际开发中我们会自己去写mapper映射文件,接口,数据库表对应的实体类,如果需求任务比较少,咱们还可以慢慢的一个一个去写,但是这是不现实的,因为在工作中我们的任务是很多的,这时mybat ...
- JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程
1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...
- mybatis框架(7)---mybatis逆向工程
mybatis逆向工程 逆向工程的目的就是缩减了我们的开发时间.所谓mybatis逆向工程,就是mybatis会根据我们设计好的数据表,自动生成pojo.mapper以及mapper.xml. 接 ...
- 学习笔记01(mybatis逆向工程)
今天来看看一个常用的小功能,就是mybatis的逆向工程.(数据库是mysql) 什么是逆向工程呢?看名字就知道反方向的一个什么工程! 其实啊,如果是平常我们自己学习实践一些小项目的时候,应该是先瞎写 ...
- mybatis逆向工程的注意事项,以及数据库表
1.选择性更新,如果有新参数就更换成新参数,如果参数是null就不更新,还是原来的参数 2.mybatis使用逆向工程,数据库建表的字段user_id必须用下滑线隔开,这样生成的对象private L ...
- IDEA Maven项目的Mybatis逆向工程
IDEA Maven项目的Mybatis逆向工程 1.配置.pom 如果是在多模块开发下,该文件逆向工程要生成的那个模块下的pom文件. <build> <plugins> & ...
- Maven项目下的Mybatis逆向工程
IDEA Maven项目的Mybatis逆向工程 1.配置.pom 如果是在多模块开发下,该文件逆向工程要生成的那个模块下的pom文件. <build> <plugins> & ...
随机推荐
- 【Flutter 混合开发】与原生通信-BasicMessageChannel
Flutter 混合开发系列 包含如下: 嵌入原生View-Android 嵌入原生View-iOS 与原生通信-MethodChannel 与原生通信-BasicMessageChannel 与原生 ...
- windows10下IntelliJ IDEA使用logback设置日志输出目录
1.在项目的src/main/resources目录下新建文件:logback-spring.xml 2:在logback-spring.xml中进行如下配置: <?xml version=&q ...
- CNN作为denoiser的优势总结
图像恢复的MAP推理公式: $\hat{x}\text{}=\text{}$arg min$_{x}\frac{1}{2}||\textbf{y}\text{}-\text{}\textbf{H}x| ...
- linux压缩和解压文件命令
tar 解包:tar zxvf filename.tar 打包:tar czvf filename.tar dirnamegz命令 解压1:gunzip filename.gz 解压2:gzi ...
- 初识ABP vNext(12):模块的独立运行与托管
Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 目录 前言 开始 模块运行 动态 C# API 客户端 最后 前言 很久没更新这个系列...之前的章节中讲到ABP的模块是可以独立运行的 ...
- python基础三:函数
def name(参数1,参数2,参数3,...) 可以自定义一些自己需要的函数来简化自己的工作. 如:自定义一个计算函数 def mycount(a,b,c): y=a+b-c return y ...
- http接口和web service接口测试区别是什么?
1.web service有一套完整的协议标准,其中有soap协议,用来进行消息的传递. 2.soap请求是HTTP POST的一个专用版本,遵循一种特殊的xml消息格式 Content-type设置 ...
- git 上传代码报错eslint --fix found some errors. Please fix them and try committing again.
在提交时用下面这句 git commit --no-verify -m "提交时的注释"
- 面试小问题——Object中有哪些常用方法?
一.equals方法 Object类中的equals方法用于检测一个对象是否等于另外一个对象.Java语言规范要求equals方法具有下面的特性: (1)自反性:对于任何非空引用x,x.equals( ...
- leetcode两数之和go语言
两数之和(Go语言) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复 ...