Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)

创建Spring Boot项目

1、New Project

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第1张图片

 

 

 2)取名为sb2 (springboot2.x)

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第2张图片

 

 

 

 

3、勾选Spring Web

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第3张图片

 

 

4、勾选MyBatis framework

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第4张图片

 

 

5、下一步,然后点击完成

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第5张图片

 

6、pom.xml增加jar和插件

jar

dependency>
    mysql
    mysql-connector-java
    5.1.48

 

插件


                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    
                        mysql
                        mysql-connector-java
                        5.1.48
                    
                
            

  

 

7、增加resources/generatorConfig.xml文件



 

    
        
            
            
        
        
        
        
        
        
            
        
 
        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        

  

  

8、双击mybatis-generator:generate

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第6张图片

 

 

9、application.properties

server.port=8080
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.bean
mybatis.configuration.map-underscore-to-camel-case=true

  

  

 

10、配置扫码范围

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class Sb2Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Sb2Application.class, args);
    }
 
}

  

 

11、创建Service层

@Component
public class DemoService
{
 
    @Autowired
    private TestMapper testMapper;
 
    public Test getTestInfoById(Integer id){
        return Optional.ofNullable(testMapper.selectByPrimaryKey(id)).orElse(null);
    }
 
}

  

  

12、创建Controller层

@Controller
@RequestMapping("/demo")
public class DemoController {
 
    @Autowired
    private DemoService demoService;
 
 
    @RequestMapping("/hello/{id}")
    @ResponseBody
    public String hello(@PathVariable(value = "id") Integer id){
        return Optional.ofNullable(demoService.getTestInfoById(id)).map(Test::toString).orElse("empty String");
    }
 
}

  

 

其中咋Test增加toString方法

public class Test {
    private Integer id;
 
    private String name;
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
 
    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
  

  

 

 

13、运行

Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql)_第7张图片

 

 

14、MySql数据库名为test

表test的表结构:

 

 表数据

你可能感兴趣的:(Spring Boot项目搭建(Spring Boot 2.2.4 + MyBatis + MySql))