溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置

發布時間:2020-08-05 00:13:08 來源:ITPUB博客 閱讀:220 作者:hky87 欄目:編程語言

一、在Eclipse中安裝mybatis generator

     菜單選擇: Help->Eclipse Marketplace

Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置


二、 創建generatorConfig.xml配置文檔

Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis 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>
	<!--加載屬性文件 -->
	<properties resource="application.properties" />
	<!-- context:生成一組對象的環境 id:必選,上下文id,用于在生成錯誤時提示 defaultModelType:指定生成對象的樣式 
		1,conditional:類似hierarchical; 2,flat:所有內容(主鍵,blob)等全部生成在一個對象中; 3,hierarchical:主鍵生成一個XXKey對象(key 
		class),Blob等單獨生成一個對象,其他簡單屬性在一個對象中(record class) targetRuntime: 1,MyBatis3:默認的值,生成基于MyBatis3.x以上版本的內容,包括XXXBySample; 
		2,MyBatis3Simple:類似MyBatis3,只是不生成XXXBySample; introspectedColumnImpl:類全限定名,用于擴展MBG -->
	<context id="context1" defaultModelType="hierarchical"
		targetRuntime="MyBatis3Simple">
		<!-- 自動識別數據庫關鍵字,默認false,如果設置為true,根據SqlReservedWords中定義的關鍵字列表; 一般保留默認值,遇到數據庫關鍵字(Java關鍵字),使用columnOverride覆蓋 -->
		<property name="autoDelimitKeywords" value="false" />
		<!-- 生成的Java文件的編碼 -->
		<property name="javaFileEncoding" value="UTF-8" />
		<!-- 格式化java代碼 -->
		<property name="javaFormatter"
			value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
		<!-- 格式化XML代碼 -->
		<property name="xmlFormatter"
			value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
		<!-- beginningDelimiter和endingDelimiter:指明數據庫的用于標記數據庫對象名的符號,比如ORACLE就是雙引號,MYSQL默認是`反引號; -->
		<property name="beginningDelimiter" value="`" />
		<property name="endingDelimiter" value="`" />
		<!-- 實現自定義的代碼生成器plugin -->
		<!-- <plugin type="org.mybatis.PaginationPlugin" /> -->
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 數據庫連接URL,用戶名,密碼 -->
		<jdbcConnection
			driverClass="${spring.datasource.driver-class-name}"
			connectionURL="${spring.datasource.url}"
			userId="${spring.datasource.username}"
			password="${spring.datasource.password}">
			<property name="nullCatalogMeansCurrent" value="true" />
		</jdbcConnection>
		<!-- java類型處理器 用于處理DB中的類型到Java中的類型,默認使用JavaTypeResolverDefaultImpl; 注意一點,默認會先嘗試使用Integer,Long,Short等來對應DECIMAL和 
			NUMERIC數據類型; -->
		<javaTypeResolver
			type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
			<!-- true:使用BigDecimal對應DECIMAL和 NUMERIC數據類型 false:默認, scale>0;length>18:使用BigDecimal; 
				scale=0;length[10,18]:使用Long; scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; -->
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!--生成模型的包名和位置 -->
		<javaModelGenerator
			targetPackage="com.kai.demo.model" targetProject="demo/src/main/java">
			<!-- for MyBatis3/MyBatis3Simple 自動為每一個生成的類創建一個構造方法,構造方法包含了所有的field;而不是使用setter; -->
			<property name="constructorBased" value="false" />
		</javaModelGenerator>
		<!--映射文件的包名和位置 -->
		<sqlMapGenerator targetPackage="com.kai.demo.mapper"
			targetProject="demo/src/main/java" />
		<!--DAO的包名和位置 -->
		<javaClientGenerator
			targetPackage="com.kai.demo.dao" targetProject="demo/src/main/java"
			type="XMLMAPPER">
			<!-- 是否允許建立子包,對應Mysql的Schema -->
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!--要生成哪些表 -->
		<table schema="mybatis" tableName="%"
			enableSelectByExample="false" enableDeleteByExample="false"
			enableCountByExample="false" enableUpdateByExample="false"
			selectByExampleQueryId="false">
		</table>
	</context>
</generatorConfiguration>

(我也不知道為什么itpub的xml片段插入之后,結尾會多了點,正確的xml幾位應該是)

Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置

選中generatorConfig.xml文件,右鍵菜單Run As->Run Mybatis Generator  生成Model、Dao、Mapper

Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置

創建User表的SQL:

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `nickname` varchar(50) DEFAULT NULL,
  `regtime` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


四、對UserMapper進行單元測試

在項目的src/test/java下創建類文件UserMapperTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
	
	@Autowired
	private UserMapper userMapper;
	
	@Test
	public void testQuery() throws Exception {		
		User user = userMapper.selectByPrimaryKey(1);
		System.out.println(user.toString());
	}
}

右鍵Run As->JUnit Test Spring Boot + Mybatis + Spring MVC環境配置(二):Mybatis Generator配置


完整環境下載地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC  

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女