springboot学习-整合mybatis使用Generator自动生成代码

第零步,在pom.xml中新增加generator插件,如图:

springboot学习-整合mybatis使用Generator自动生成代码_第1张图片

 

配置代码贴出来:


        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
        

        
            com.alibaba
            druid
            1.1.0
        

        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        

    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
            
        
    

第一步,编辑generator文件:




    
    
    
        
            
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        
   

此时的项目结构如图所示:

springboot学习-整合mybatis使用Generator自动生成代码_第2张图片

 

第二步,运行generator生成代码:

run-Edit Configurations-点击绿色的加号,选择maven:

 

springboot学习-整合mybatis使用Generator自动生成代码_第3张图片

填写name及运行命令mybatis-generator:generate -e:

 

springboot学习-整合mybatis使用Generator自动生成代码_第4张图片

第三步,运行generator ,生成代码:

 

运行结束之后,如果成功,我们可以看到按照预想已经生成了entity、dao、及Mapper映射文件。

springboot学习-整合mybatis使用Generator自动生成代码_第5张图片

第四步,编写controller,实现我们所需的功能:

package com.javazhiyin.controller;

import com.javazhiyin.dao.UserMapper;
import com.javazhiyin.entity.User;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
* Created by 57783 on 2018/7/6.
*/
@RestController
public class UserController {
    @Resource
    private UserMapper userMapper;

    @GetMapping("/showUser/{id}")
    public User getUser(@PathVariable("id") Integer id){
        User user = this.userMapper.selectByPrimaryKey(id);
        return user;
    }

    @PostMapping(value = "/addUser")
    public void addUser(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("age") Integer age){
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        user.setAge(age);
        userMapper.insert(user);
    }

    @PutMapping(value = "updUser/{id}")
    public void updUser(@PathVariable("id") Integer id,
                        @RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("age") Integer age){
        User user = new User();
        user.setId(id);
        user.setUserName(username);
        user.setPassword(password);
        user.setAge(age);
        userMapper.updateByPrimaryKey(user);

    }

    @DeleteMapping(value = "delUser/{id}")
    public void delUser(@PathVariable("id") Integer id){
        userMapper.deleteByPrimaryKey(id);
    }

}

最后,我们修改Bootdemo04Application启动类,添加@MapperScan注解,使其可以扫描DAO层接口:

package com.javazhiyin;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan("com.javazhiyin.dao")
public class Bootdemo04Application {

    public static void main(String[] args) {
        SpringApplication.run(Bootdemo04Application.class, args);
    }
}

只有这四个增删改查可能不会感觉多方便,如果有大量的表,省去我们编写基本的DAO、Entity及Mapper映射,回非常的nice.

注:本文转载自https://www.javazhiyin.com/,若有侵权,联系我(QQ:1414005315)删除,

你可能感兴趣的:(编程语言,学习经验)