提示:以下是本篇文章正文内容,下面案例可供参考
1. 在pom文件中导入依赖:依赖一定要在plugins标签的第一行
<!--反向生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的路径-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>
2. 在resources目录下创建 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>
<!--1、数据库驱动jar:添加自己的jar路径 -->
<classPathEntry
location="E:\1.kaikeba\8.Maven\repository\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar" />
<context id="MyBatis" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--2、数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/kkb-mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT"
userId="root"
password="数据库密码">
# 可以识别table中的主键 不加可能会出现Mapper文件没有主键查询
<property name="useInformationSchema" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--3、生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
使用Maven生成在target目录下,会自动创建) -->
<javaModelGenerator targetPackage="org.xzk.bean"
targetProject="src\main\java">
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--4、生成SQLmapper.xml映射文件 -->
<sqlMapGenerator targetPackage="org.xzk.mapper"
targetProject="src\main\resources">
</sqlMapGenerator>
<!--5、生成Dao(Mapper)接口文件,-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="org.xzk.mapper"
targetProject="src\main\java">
</javaClientGenerator>
<!--6、要生成哪些表(更改tableName和domainObjectName就可以) -->
<!-- tableName:要生成的表名
enableCountByExample:Count语句中加入where条件查询,默认为true开启
enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
-->
<!-- <table tableName="Team"-->
<!-- enableCountByExample="false"-->
<!-- enableUpdateByExample="false"-->
<!-- enableUpdateByPrimaryKey="false"-->
<!-- enableDeleteByExample="false"-->
<!-- enableDeleteByPrimaryKey="false"-->
<!-- enableSelectByExample="false"-->
<!-- selectByExampleQueryId="false">-->
<!-- <property name="useActualColumnNames" value="true"/>-->
<!-- </table>-->
<!-- tableName:数据库中的表名,不区分大小写-->
<table tableName="Team" enableCountByExample="false">
<!-- 会根据数据库中的表数据名称进行驼峰命令规范-->
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
运行:要重复创建时,必须把之前创建好的文件夹和文件全部删除
显示:BUILD SUCCESS 即成功
测试:
1. 多条件查询
@Test
public void de2(){
//多条件、排序的类
TeamExample teamExample = new TeamExample();
//创建Team对象的规则 理解为盛放条件的容器,每一个对象都对应一个盛放条件的容器
TeamExample.Criteria criteria = teamExample.createCriteria();
//向容器中添加条件
criteria.andNameLike("人");
criteria.andIdBetween(1,100);
teamExample.setOrderByClause("name");
List<Team> teams = mapper.selectByExample(teamExample);
teams.forEach(team -> System.out.println(team));
}
2. 普通查询
@Test
public void de1(){
Team team = new Team();
team.setName("lina");
// 动态sql语句查询
int i = mapper.insertSelective(team);
MybatisUtil.getOpenSession().commit();
System.out.println(i);
}
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
提示:
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_50677223/article/details/119567551
内容来源于网络,如有侵权,请联系作者删除!