十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇文章給大家分享的是有關(guān)Mybatis Generator怎么在springboot中使用,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)巴南免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
MBG配置
1.添加依賴
org.mybatis.generator mybatis-generator-core 1.3.5
2.XML配置
配置示例:在新建springboot項目的根目錄下創(chuàng)建mbg.xml文件。
配置詳解
生成代碼:
public class TestMGB {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List warnings =new ArrayList();
boolean overwrite=true;
File configFile=new File("mgb.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);
}
} 3.Java配置示例
基于Java的配置是和上面的XML配置是相對應(yīng)的。直接運行該示例即可生成數(shù)據(jù)表對于的pojo,mapper接口和一個sqlprovider Java類。
package com.mgb.test;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import org.mybatis.generator.config.JavaTypeResolverConfiguration;
import org.mybatis.generator.config.ModelType;
import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.config.SqlMapGeneratorConfiguration;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MGBConfig {
public static void main(String[] args) throws Exception{
//配置xml配置項
List warnings = new ArrayList();
boolean overwrite = true;
Configuration config = new Configuration();
Context context = new Context(ModelType.CONDITIONAL);
context.setTargetRuntime("MyBatis3");
context.setId("defaultContext");
//自動識別數(shù)據(jù)庫關(guān)鍵字,默認false,如果設(shè)置為true,
//根據(jù)SqlReservedWords中定義的關(guān)鍵字列表;一般保留默認值,遇到數(shù)據(jù)庫關(guān)鍵字(Java關(guān)鍵字),
//使用columnOverride覆蓋
context.addProperty("autoDelimitKeywords","true");
//生成的Java文件的編碼
context.addProperty("javaFileEncoding","utf-8");
context.addProperty("beginningDelimiter","`");
context.addProperty("endingDelimiter","`");
//格式化java代碼
context.addProperty("javaFormatter","org.mybatis.generator.api.dom.DefaultJavaFormatter");
//格式化xml代碼
context.addProperty("xmlFormatter","org.mybatis.generator.api.dom.DefaultXmlFormatter");
//格式化信息
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.setConfigurationType("org.mybatis.generator.plugins.SerializablePlugin");
pluginConfiguration.setConfigurationType("org.mybatis.generator.plugins.ToStringPlugin");
context.addPluginConfiguration(pluginConfiguration);
//設(shè)置是否去除生成注釋
CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
commentGeneratorConfiguration.addProperty("suppressAllComments","true");
//commentGeneratorConfiguration.addProperty("suppressDate","true");
context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
//設(shè)置連接數(shù)據(jù)庫
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setDriverClass("com.mysql.jdbc.Driver");
jdbcConnectionConfiguration.setConnectionURL("jdbc:mysql://localhost:3306/definesys");
jdbcConnectionConfiguration.setPassword("welcome1");
jdbcConnectionConfiguration.setUserId("root");
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
JavaTypeResolverConfiguration javaTypeResolverConfiguration = new JavaTypeResolverConfiguration();
//是否使用bigDecimal, false可自動轉(zhuǎn)化以下類型(Long, Integer, Short, etc.)
javaTypeResolverConfiguration.addProperty("forceBigDecimals","false");
context.setJavaTypeResolverConfiguration(javaTypeResolverConfiguration);
//生成實體類的地址
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetPackage("com.mgb.domain");
javaModelGeneratorConfiguration.setTargetProject("src/main/java");
javaModelGeneratorConfiguration.addProperty("enableSubPackages","true");
javaModelGeneratorConfiguration.addProperty("trimStrings","true");
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
//生成的xml的地址
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject("src/main/java");
sqlMapGeneratorConfiguration.setTargetPackage("com.mgb.mapper");
sqlMapGeneratorConfiguration.addProperty("enableSubPackages","true");
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
//生成注解接口
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setTargetPackage("com.mgb.dao");
javaClientGeneratorConfiguration.setTargetProject("src/main/java");
//注解形式 ANNOTATEDMAPPER xml形式 XMLMAPPER
javaClientGeneratorConfiguration.setConfigurationType("ANNOTATEDMAPPER");
javaClientGeneratorConfiguration.addProperty("enableSubPackages","true");
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName("user_info");
tableConfiguration.setCountByExampleStatementEnabled(true);
tableConfiguration.setUpdateByExampleStatementEnabled(true);
tableConfiguration.setDeleteByExampleStatementEnabled(true);
tableConfiguration.setInsertStatementEnabled(true);
tableConfiguration.setDeleteByPrimaryKeyStatementEnabled(true);
context.addTableConfiguration(tableConfiguration);
config.addContext(context);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
} 使用
package com.mgb.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mgb.dao.UserInfoMapper;
import com.mgb.domain.UserInfo;
import com.mgb.domain.UserInfoExample;
@Service
public class UserService {
@Autowired
private UserInfoMapper userInfoMapper;
/**
* 按姓名查詢
* @param name
* @return
*/
public List getUserByName(String name){
UserInfoExample uerInfoExample=new UserInfoExample();
uerInfoExample.createCriteria().andNameEqualTo(name);
return userInfoMapper.selectByExample(uerInfoExample);
}
/**
* 有條件的insert
* @param userInfo
* @return
*/
public Integer addUser(UserInfo userInfo) {
return userInfoMapper.insertSelective(userInfo);
}
/**
* 根據(jù)ID更新用戶信息
* @param userInfo
* @return
*/
public Integer updateUser(UserInfo userInfo) {
return userInfoMapper.updateByPrimaryKey(userInfo);
}
/**
* 根據(jù)ID刪除用戶
* @param id
* @return
*/
public Integer deleteUserById(Integer id) {
return userInfoMapper.deleteByPrimaryKey(id);
}
} 以上就是Mybatis Generator怎么在springboot中使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。