jdbc实体类自动生成
时间: 2023-09-30 07:08:52 浏览: 74
回答: 通过给出的引用内容,我们可以看到在这个实体类中,使用了jdbc来建立数据库连接。在引用中,generator.properties文件中配置了数据库连接的相关信息,包括驱动类、连接URL、用户名和密码。而在引用中的SuperDao类中,也提供了建立连接和关闭连接的方法。在getConnection()方法中,使用了com.mysql.jdbc.Driver作为驱动类,连接URL、用户名和密码也在代码中定义了。需要注意的是,在引用中提到com.mysql.jdbc.Driver这个驱动类已经废弃,应该使用com.mysql.cj.jdbc.Driver。所以在实际使用时,建议使用新的驱动类来建立数据库连接。
相关问题
mybatis自动生成实体类
MyBatis可以通过逆向工程生成实体类,步骤如下:
1. 在项目中添加MyBatis Generator插件依赖。
2. 创建一个MyBatis Generator配置文件,通常命名为`generatorConfig.xml`。
3. 配置MyBatis Generator,包括数据源、实体类生成路径、表名和实体类名的映射关系等。
4. 运行MyBatis Generator,自动生成实体类。
以下是一个简单的MyBatis Generator配置文件示例:
```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="mysql" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test"
userId="root"
password="root">
</jdbcConnection>
<!-- 实体类生成路径 -->
<javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射文件生成路径 -->
<sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- DAO接口生成路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 表名和实体类名映射关系 -->
<table tableName="user" domainObjectName="User"></table>
</context>
</generatorConfiguration>
```
运行MyBatis Generator可以使用命令行工具或者IDEA插件,具体操作可以参考MyBatis官方文档。
mp如何自动生成实体类
在使用 MyBatis-Plus 时,可以通过使用 MyBatis-Plus 的代码生成器来自动生成实体类。具体步骤如下:
1. 配置 pom.xml 文件,添加 MyBatis-Plus 的依赖。
2. 在项目的 resources 目录下创建 generator 目录,并在该目录下创建 generatorConfig.xml 配置文件,在该文件中配置代码生成器的相关信息,包括数据库连接信息、生成文件保存路径、生成的表配置等。
3. 在代码中编写代码生成器的启动类,通过配置文件的方式加载配置信息,生成代码。
4. 运行代码生成器启动类,即可自动生成实体类、Mapper 接口以及 XML 映射文件。
下面是一个简单的示例:
1. pom.xml 文件中添加 MyBatis-Plus 的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2. 在 resources 目录下创建 generator 目录,并在该目录下创建 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="MySqlContext" targetRuntime="MyBatis3">
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"
userId="root"
password="root">
</jdbcConnection>
<!-- Java 模型生成器配置,生成实体类 -->
<javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- Mapper.xml 文件生成器配置,生成 XML 映射文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- Mapper 接口生成器配置,生成 Mapper 接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 表配置,生成指定表的代码 -->
<table tableName="user"/>
</context>
</generatorConfiguration>
```
3. 在代码中编写代码生成器启动类,如下所示:
```
public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("your name");
gc.setOpen(false);
// 实体属性 Swagger2 注解
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
pc.setParent("com.example");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
strategy.setSuperControllerClass("com.example.controller.BaseController");
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id");
strategy.setInclude("user");
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
mpg.execute();
}
}
```
4. 运行 CodeGenerator 类,即可自动生成实体类、Mapper 接口以及 XML 映射文件。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)