摘要:每个微服务仅关注于完成一件任务并很好地完成该任务。在一个微服务的开发过程中很可能只关注对单表的操作。本文将说到在的项目中如何去配置形式和配置类形式和使用以及生成代码的两种方式形式和注解形式,在中更推荐去使用注解的形式。
介绍
Mybatis Generator(MBG)是Mybatis的一个代码生成工具。MBG解决了对数据库操作有最大影响的一些CRUD操作,很大程度上提升开发效率。如果需要联合查询仍然需要手写sql。相信很多人都听说过微服务,各个微服务之间是松耦合的。每个微服务仅关注于完成一件任务并很好地完成该任务。在一个微服务的开发过程中很可能只关注对单表的操作。所以MBG在开发过程中可以快速的生成代码提升开发效率。
本文将说到在springboot的项目中如何去配置(XML形式和Java配置类形式)和使用MBG以及MBG生成代码的两种方式(XML形式和注解形式),在springboot中更推荐去使用注解的形式。
MBG配置添加依赖
org.mybatis.generator mybatis-generator-core 1.3.5
2.XML配置
配置示例:在新建springboot项目的根目录下创建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配置是相对应的。直接运行该示例即可生成数据表对于的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");
//自动识别数据库关键字,默认false,如果设置为true,
//根据SqlReservedWords中定义的关键字列表;一般保留默认值,遇到数据库关键字(Java关键字),
//使用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);
//设置是否去除生成注释
CommentGeneratorConfiguration commentGeneratorConfiguration = new CommentGeneratorConfiguration();
commentGeneratorConfiguration.addProperty("suppressAllComments","true");
//commentGeneratorConfiguration.addProperty("suppressDate","true");
context.setCommentGeneratorConfiguration(commentGeneratorConfiguration);
//设置连接数据库
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可自动转化以下类型(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);
}
/**
* 根据ID更新用户信息
* @param userInfo
* @return
*/
public Integer updateUser(UserInfo userInfo) {
return userInfoMapper.updateByPrimaryKey(userInfo);
}
/**
* 根据ID删除用户
* @param id
* @return
*/
public Integer deleteUserById(Integer id) {
return userInfoMapper.deleteByPrimaryKey(id);
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/72182.html
摘要:整合想着每次搭建新项目时框架都要从新搭建,基本常用的也就哪几种,现在就来搭建一种常用的后台框架,以后新开小项目可以直接拿来使用项目整体结构图新建空白项目,选中依赖略,也可以完全根据本人代码操作文件依赖项展示 springboot整合tkMapper 想着每次搭建新项目时框架都要从新搭建,基本常用的也就哪几种,现在就来搭建一种常用的springboot后台框架,以后新开小项目可以直接拿来...
摘要:如要运行多次,请把上次生成的映射文件代码删除再运行。层启动类扫描接口,必须加上提一嘴,这个注解非常的关键,这个对应了项目中所对应的包路径,必须加上,否则会导致异常。另外,关注之后在发送可领取免费学习资料。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前...
摘要:优化当我们在数据库中增加字段时,需要在对应的实体类中增加字段,中也需要去增加字段,去维护,会消耗大量的时间我们可以让接口去继承,删除接口中的所有方法,因为中都已经实现了。遇到这里问题不会报错,只要注意打印出来的语句即可。 SpringBoot集成Mybatis 自动生成实体类和Mapper 1.使用IDEA创建一个空的SpringBoot项目 2.在pom.xml中引入以下配置 ...
摘要:引入了新的环境和概要信息,是一种更揭秘与实战六消息队列篇掘金本文,讲解如何集成,实现消息队列。博客地址揭秘与实战二数据缓存篇掘金本文,讲解如何集成,实现缓存。 Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控 - 掘金Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...
阅读 2640·2023-04-26 03:06
阅读 3866·2023-04-26 01:51
阅读 2463·2021-11-24 09:38
阅读 2968·2021-11-17 17:00
阅读 2679·2021-09-28 09:36
阅读 1236·2021-09-24 09:47
阅读 2840·2019-08-30 15:54
阅读 1784·2019-08-30 15:44