1. 程式人生 > >mybatis(7)---spring整合mybatis進行自動生成程式碼

mybatis(7)---spring整合mybatis進行自動生成程式碼

1、引入mybatis的generator包

<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>

2、在src下新增配置檔案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="DB2Tables" targetRuntime="MyBatis3">
  	<!-- 配置生成的sql對映檔案不需要註釋 -->
  	<commentGenerator>
  		<property name="suppressAllComments" value="true" />
	</commentGenerator>
  	<!-- 1、配置資料庫連線 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/qx"
        userId="root"
        password="root">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
	<!-- 2、配置javaBean生成的位置 -->
    <javaModelGenerator targetPackage="com.cn.entity" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
	<!-- 3、指定sql對映檔案生成的位置 -->
    <sqlMapGenerator targetPackage="com.cn.mapping"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
	<!-- 4、指定mapper介面生成的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.cn.mapper"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
	<!-- 5、指定每個表的生成策略 -->
    <!-- <table tableName="t_emp" domainObjectName="Employee" />
    <table tableName="t_dept" domainObjectName="Dept" /> -->
    <!-- 5、指定每個表的生成策略 -->
    <table schema="general" tableName="user" domainObjectName="User"
    		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
    		enableSelectByExample="false" selectByExampleQueryId="false">
    	
    </table>
	<table schema="general" tableName="t_dept" domainObjectName="Dept"
    		enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
    		enableSelectByExample="false" selectByExampleQueryId="false">
    	
    </table>
  </context>
</generatorConfiguration>

3、在src下新建包com.cn.test,然後在該包下新建測試類MyBatsGeneratorTest

package com.cn.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
 * 用於生成mapper對映檔案、介面、實體類
 * */
public class MyBatisGeneratorTest {
	public static void main(String[] args) throws Exception, XMLParserException {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("G:\\workPlace\\taotao2\\qxSystem\\config\\spring\\mybatis-generator.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);
	}
}

4、執行測試類後,重新整理src目錄會看到生成的實體類、mapper介面和mapper對映檔案。注意要先建表,是根據表的欄位生成的

相關推薦

mybatis7---spring整合mybatis進行自動生成程式碼

1、引入mybatis的generator包 <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-

Mybatis學習————— Spring整合mybatis

一、Spring整合mybatis思路           非常簡單,這裡先回顧一下mybatis最基礎的根基,       mybatis,有兩個配置檔案         全域性配置檔案SqlMapConfig.xml(配置資料來源,全域性變數,載入對映檔案等東西)         對映檔案xxxM

SSM進階Spring整合Mybatis-----Aop事務

Spring整合Mybatis-----Aop事務 首先對於Spring整合Mybatis有兩種方式那就是使用sql絕對定位以及使用Mapper代理來進行開發,本篇博文先來介紹第一種。        所謂的Sql絕對定位就是namespace+id得方式來進行定位

企業分布式微服務雲SpringCloud SpringBoot mybatis Spring Boot中使用Spring Security進行安全控制

spring ron public 控制 應用 app ebs cloud 來源 準備工作 首先,構建一個簡單的Web工程,以用於後續添加安全控制,也可以用之前Chapter3-1-2做為基礎工程。若對如何使用Spring Boot構建Web應用,可以先閱讀《Spring

企業分布式微服務雲SpringCloud SpringBoot mybatis 十三Spring Boot整合MyBatis

ech 字段 osc 操作 with public assert 連接 ref Spring中整合MyBatis就不多說了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步驟。搜了一下Spring Boot整合MyBatis的文

spring-boot整合mybatis

這兩天啟動了一個新專案因為專案組成員一直都使用的是mybatis,雖然個人比較喜歡jpa這種極簡的模式,但是為了專案保持統一性技術選型還是定了 mybatis。到網上找了一下關於spring boot和mybatis組合的相關資料,各種各樣的形式都有,看的人心累,結合了mybatis的官方demo和

日常Exception:springmybatis整合時報錯

問題:最近在整合SSM框架,搭建一半之後(即專案建立完畢,spring與mybatis整合完畢),開始測試spring與mybatis是否整合成功。貼出部分程式碼:/** * 配置spring和junit整合,junit啟動時載入springIOC容器 spring-tes

Spring Boot 2.x整合Mybatis的四種方式

前言 目前的大環境下,使用Mybatis作為持久層框架還是佔了絕大多數的,下面我們來說一下使用Mybatis的幾種姿勢。 姿勢一:

Spring Boot 2.x整合Mybatis-Plus

簡介 Mybatis-Plus是在Mybatis的基礎上,國人開發的一款持久層框架。 並且榮獲了2018年度開源中國最受歡迎的中

Spring Boot入門系列如何整合Mybatis實現增刪改查

前面介紹了Spring Boot 中的整合Thymeleaf前端html框架,同時也介紹了Thymeleaf 的用法。不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html。 今天主要講解Springboot整合M

企業分布式微服務雲SpringCloud SpringBoot mybatis Spring Boot中使用JdbcTemplate訪問數據庫

ger sele 應該 創建 測試環境 oid reg tis eat 本文介紹在Spring Boot基礎下配置數據源和通過JdbcTemplate編寫數據訪問的示例。 數據源配置 在我們訪問數據庫的時候,需要先配置一個數據源,下面分別介紹一下幾種不同的數據庫配置方式。

企業分布式微服務雲SpringCloud SpringBoot mybatis Spring Boot屬性配置文件詳解

public profile 功能 tps with oot oschina 基本 ava 相信很多人選擇Spring Boot主要是考慮到它既能兼顧Spring的強大功能,還能實現快速開發的便捷。我們在Spring Boot使用過程中,最直觀的感受就是沒有了原來自己整合S

企業分布式微服務雲SpringCloud SpringBoot mybatis Spring Boot開發Web應用

我們 als rep .com inf 模型 uil java加密 min 靜態資源訪問 在我們開發Web應用的時候,需要引用大量的js、css、圖片等靜態資源。 默認配置 Spring Boot默認提供靜態資源目錄位置需置於classpath下,目錄名需符合如下規則:

企業分布式微服務雲SpringCloud SpringBoot mybatis Spring Boot中Web應用的統一異常處理

src one exception learn 微服務 public .net 可能 訪問 我們在做Web應用的時候,請求處理過程中發生錯誤是非常常見的情況。Spring Boot提供了一個默認的映射:/error,當處理中拋出異常之後,會轉到該請求中處理,並且該請求有一個

MyBatis——使用PageHelper外掛進行分頁

一、概述   PageHelper是MyBatis中非常方便的第三方分頁外掛。      官方文件: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md   Ho

企業級 SpringBoot 教程 springboot整合mybatis

引入依賴 在pom檔案引入mybatis-spring-boot-starter的依賴: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>

spring-shiro-- spring boot + mybatis +shrio+spring mvc

前面對shiro記性了一些邏輯的操作,和具體的程式碼片段的操作,現在將我寫的一個小的案例,總結一下。總結完明天就上班了。 本案例使用的是spring boot + spring mvc +mybatis 專案的結構 首先對pom檔案進行操作 <?xml version="

微服務 SpringBoot 2.0整合Mybatis

我是SQL小白,我選Mybatis —— Java面試必修 引言 在第五章我們已經整合了Thymeleaf頁面框架,第七章也整合了JdbcTemplate,那今天我們再結合資料庫整合Mybatis框架 在接下來的文章中,我會用一個開源的部落格原始碼來做講解

RabbitMQ的學習(五):最全面Spring整合RabbitMQ進行RPC遠端呼叫

前面用java簡單對rabbitmq進行了學習,但rabbitmq的功能遠非如此,配合Spring或Springboot進行開發,才是java工程師做的工作,下面直接進行實戰: 一、建立SSM專案 PS初期web專案下載地址(單純的web,用於練習): https://downloa

SpringBoot 2.x整合Mybatis的四種方式

前言 目前的大環境下,使用Mybatis作為持久層框架還是佔了絕大多數的,下面我們來說一下使用Mybatis的幾種姿勢。 姿勢一:零配置註解開發 第一步:引入依賴 首先,我們需要在pom檔案中新增依賴: 第二步:配置檔案 這裡我們採用yml來進行編寫,與properties檔案相比,yml看