maven+ssm框架,自动生成实体类、DAO接口和Mapping映射文件

博客介绍了使用Maven和SSM框架自动生成实体类的方法。首先要在Maven的pom.xml节点里配置插件支持,接着在src/main/resources下添加generatorConfig.xml配置文件,然后创建启动类,运行启动类成功后刷新项目就能看到自动生成的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、加入依赖包

首先maven的pom.xml的节点里,配置一下插件的支持

<!-- 自动生成代码 -->
 		<dependency>
           <groupId>org.mybatis.generator</groupId>
           <artifactId>mybatis-generator-core</artifactId>
           <version>1.3.2</version>
       </dependency>
       
		 <!-- https://mvnrepository.com/artifact/ant/ant -->
		<dependency>
		    <groupId>ant</groupId>
		    <artifactId>ant</artifactId>
		    <version>1.7.0</version>
		</dependency>

2、添加配置文件

在src/main/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>
    <context id="springmvc" targetRuntime="MyBatis3"  >
        <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"></plugin> 
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> 
        <!-- Pagination -->
         
        <commentGenerator>
        <!-- 是否去除自动生成的注释 -->
          <property name="suppressAllComments" value="true"/>
          <property name="suppressDate" value="true"/>
        </commentGenerator>
         
        <!-- 数据库连接url、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/fruitshop?characterEncoding=utf-8" 
            	userId="root" password="123456">
        </jdbcConnection>
         
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
         
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="cn.lingnan.edu.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
         
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="cn.lingnan.edu.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
         
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.lingnan.edu.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
         
        <!-- 要生成的表 tableName对应数据库中的表名、domainObjectName对应实体类名 后面根据需要可继续添加 -->
        <table tableName="fs_user" domainObjectName="User"></table>
        <table tableName="fs_addr" domainObjectName="Address"></table>
        <table tableName="city" domainObjectName="CityUntil"></table>
        <table tableName="fs_comment" domainObjectName="Comment"></table>
        <table tableName="fs_favorite" domainObjectName="Favorite"></table>
        <table tableName="fs_fruit" domainObjectName="Fruit"></table>
    </context>
</generatorConfiguration>

三、创建启动类

package cn.lingnan.edu.util;

import org.apache.tools.ant.BuildException;
import org.mybatis.generator.ant.GeneratorAntTask;

/**
 * @author chigs
 * @time 2019年3月20日
 * 启动mybatis生成器自动生成实体类、Dao、*Map.xml。
 */
public class Start {

	private static final String CONFIG_FILE_PATH = "src/main/resources/generatorConfig.xml";
	
	public static void main(String[] args) {
		try {
			GeneratorAntTask task = new GeneratorAntTask();
			task.setConfigfile(CONFIG_FILE_PATH);
			task.setOverwrite(true);
			task.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

四、运行启动类,运行成功后刷新项目即可看到自动生成的文件

参考博客:https://www.cnblogs.com/yyyyd/p/8304138.html 

https://blog.youkuaiyun.com/sinat_37239798/article/details/79011532

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值