使用maven mybatis generator自动生成mybatis实体以及查询代码 mapper代码详细教程

mybatis-generator-maven-plugin是mybatis 官方发布的maven插件,能够自动生成代码,在项目开发前期设计完数据库库后,需要大量编写实体类,使用maven插件省时省力,快速高效,来看看怎么自动生成代码.
码云参考代码下载地址:下载
在实验过多个idea第三方插件后尝试无果,最终还是选择官方maven插件,成功插入数据.mabatis官方文档地址能不能打开看缘分,来看看详细的使用教程吧.

1. 在pom文件中引入pom插件,引入dependency和plugin



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.springboot
    blog
    0.0.1-SNAPSHOT
    blog
    Spring Boot blog

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.2
        

        
            org.mybatis.generator
            mybatis-generator-core
            1.4.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.4.0
                
                    
                        Generate MyBatis Artifacts
                        
                            generate
                        
                    
                
                
                    
                        mysql
                        mysql-connector-java
                        8.0.19
                    
                
            
        
    


2. 编写配置文件generatorConfig.xml

在resources目录下新建配置文件generatorConfig.xml编写数据库映射配置,包括需要映射的文件目录,以及需要映射的数据库表.来看下我的配置.





    
        
            
            
        
        
        
        

        
        
            
        

        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        

3. 执行maven命令生成代码

执行mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate生成代码,或者直接点击生成代码

image.png

4. 执行成功结果

等待执行成功后,会生成相应的文件,包括实体,mapper以及xml配置,
image.png

5.测试生成的代码是否可用.

以文章为例,我们再数据库中手动插入一条测试数据.
然后找到mapper/ArticleMapper 在类上添加@Mapper注解,自动生成的代码不带注解.

image.png

6.编写测试articleservice以及impl实现类是否可用.

public interface ArticleService {
    int deleteByPrimaryKey(Integer id);

    int insert(Article record);

    Article selectByPrimaryKey(Integer id);

    List
selectAll(); int updateByPrimaryKey(Article record); }
@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource//@Autowired或者使用
    private ArticleMapper mapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(Article record) {
        return 0;
    }

    @Override
    public Article selectByPrimaryKey(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List
selectAll() { return null; } @Override public int updateByPrimaryKey(Article record) { return 0; } }

7.编写测试articlecontroller.

@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService service;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Article selectOne(Integer id) {
        return this.service.selectByPrimaryKey(id);
    }
}

image.png

请求地址:http://127.0.0.1:8080/article/selectOne?id=1
image.png

请求成功.

你可能感兴趣的:(使用maven mybatis generator自动生成mybatis实体以及查询代码 mapper代码详细教程)