在SpringBoot项目里面生成Mybatis注解代码

全面使用注解,Mybatis自动生成代码。

1.maven里面的重要依赖库

<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-sleuth</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
		</dependency>

		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator -->
		<dependency>
			    <groupId>org.mybatis.generator</groupId>
			    <artifactId>mybatis-generator-core</artifactId>
			    <version>1.3.7</version>
			</dependency>

2. 从main执行代码生成

public static void main(String[] args) {
		
		//
		List<String> warnings = new ArrayList<String>();
		   boolean overwrite = true;
		   //GenMain.class.getResource("generatorConfig.xml").getFile()
		   File configFile = new File(GenerateMybatisCode.class.getResource("/generatorConfig.xml").getFile());
		   ConfigurationParser cp = new ConfigurationParser(warnings);
		   Configuration config;
		try {
			config = cp.parseConfiguration(configFile);
			  DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			   myBatisGenerator.generate(null);
		} catch (IOException | XMLParserException | InvalidConfigurationException | SQLException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("========================generate the code sucess.==================");
		 
	}

Mybatis的generatorConfig.xml配置文件请参考官方github。

自动生成完的代码,比如Mapper:

@Mapper
public interface ProductMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table product
     *
     * @mbg.generated Thu Aug 01 11:10:53 CST 2019
     */
    @Delete({
        "delete from product",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

   
    @Insert({
        "insert into product (product_type, ",
        "name, retail_price, ",
        "cost_price, create_time)",
        "values (#{productType,jdbcType=INTEGER}, ",
        "#{name,jdbcType=VARCHAR}, #{retailPrice,jdbcType=NUMERIC}, ",
        "#{costPrice,jdbcType=NUMERIC}, #{createTime,jdbcType=TIMESTAMP})"
    })
    int insert(Product record);

 省略若干代码。

Github代码参考

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值