新建项目spring boot + mybatis

一、安装IDEA

1、下载 java8,配置环境变量
2、下载 idea,安装

二、新建项目

1、File - New - ProjectSpring InitializrService URL使用Default
2、填写包名相关信息com.yzyfdf.shares,Java选8
3、勾选依赖

    - web
        - Spring Web
    - SQL
        - JDBC API
        - MyBatis Framework
        - MySQL Driver

4、选择项目位置
5、完成配置等待项目初始化

三、添加数据

1、安装 MySQL,idea连接数据库,可以参照 PyCharm连接MySQL
2、创建数据库,新建一个表,比如:

create table sharesdata
(
    myId       int primary key auto_increment comment '主键id',
    timestamp  varchar(13) comment '收盘时间',
    symbol     varchar(12) not null comment '股票代码',
    index (symbol),
    name       varchar(8)  not null comment '股票名称',
    current    float comment '当前股价',
    chg        float comment '股价变更',
    percent    float comment '股价变更百分比',
    high       float comment '最高',
    low        float comment '最低',
    open       float comment '今开',
    last_close float comment '昨收',
    limit_up   float comment '涨停',
    limit_down float comment '跌停',
    volume     bigint comment '成交量',
    amount     float comment '成交额'
);

3、插入一条数据

insert into sharesdata (symbol, name, current, chg, percent, high, low, open, last_close, limit_up, limit_down)
values ('SH600000', '浦发银行', 10.46, -0.04, -0.38, 10.57, 10.44, 10.57, 10.5, 11.55, 9.45, 38982822, 408921984);

四、配置mybatis-generator插件

1、pom.xml中plugins标签下加入

    
        org.mybatis.generator
        mybatis-generator-maven-plugin
        1.3.5
        
            
            true
            
            true
            src/main/resources/generatorConfig.xml
        
        
        
            
                mysql
                mysql-connector-java
                8.0.15
            
        
    

2、跟启动类Application同级,创建4个目录:controllerservicemapperentity,在resources下新建mapper文件夹

3、添加配置
resources下新建generatorConfig.xml,最下方table标签注意改表名和实体类名,内容:





    
    

    
    

        
        
            
            
            
            
        

        
        
            
        

        
        
            
            
        

        
        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
            
            
        

        
        
            
            
        

        
        
        
        

resources下新建mybatisGeneratorinit.properties,内容:

#DataSource
#数据库驱动
jdbc_driver=com.mysql.jdbc.Driver
#数据库链接
jdbc_url=jdbc:mysql://localhost:3306/shares?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#数据库用户名
jdbc_user=root
#数据库密码
jdbc_password=123456
#生成类位置
location_entity=com.yzyfdf.shares.entity
location_mapper=com.yzyfdf.shares.mapper

application.yml中添加:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/db_test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.yzyfdf.shares.entity

4、运行插件
右侧Maven - 展开Plugins - mybatis-genertor,双击mybatis-generator:generate,等待编译,出现下面的就成功了

[INFO] Saving file ShareMapper.xml
[INFO] Saving file Share.java
[INFO] Saving file ShareMapper.java
[INFO] ---------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------
[INFO] Total time:  1.141 s

自动生成了三个文件

五、补全代码

1、给ShareMapper.java类加上@Repository注解
2、在service下新建ShareService

import com.yzyfdf.shares.entity.Share;
import com.yzyfdf.shares.mapper.ShareMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShareService {

    @Autowired
    ShareMapper shareMapper;

    public Share getShareById(int id) {
        return shareMapper.selectByPrimaryKey(id);
    }
}

3、在controller下新建ShareController

import com.yzyfdf.shares.entity.Share;
import com.yzyfdf.shares.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/shares")
public class ShareController {
    
    @Autowired
    private ShareService shareService;

    @RequestMapping("/{id}")
    public Share getShare(@PathVariable int id) {
        return shareService.getShareById(id);
    }
}

4、给启动类加上注解@MapperScan("com.yzyfdf.shares.mapper")

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

@MapperScan("com.yzyfdf.shares.mapper")
@SpringBootApplication
public class SharesApplication {

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

}

5、在application.yml中加上端口

server:
  port: 8015

六、启动

1、在启动类右键,选择Run,等待启动完
2、在浏览器输入http://localhost:8015/shares/1,就能看到数据了

七、分环境配置

1、application.ymldatasource部分放入application-dev.yml
2、再创建一个application-prod.yml,内容同application-dev.yml,用户名密码自行修改
3、application.yml中加入

spring:
  profiles:
    active: prod

修改active改变环境

你可能感兴趣的:(新建项目spring boot + mybatis)