springboost数据库开发

springboost 配置文件

 spring.config.location=file:C:\\properties\\application.yml
 spring.config.name=application

Mybatis

1 添加依赖(gradle)
'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2' 'mysql:mysql-connector-java' 'com.alibaba:druid:1.1.6'
2 设置数据库配置(application.properties)

#配置数据库
spring.datasource.url=jdbc:mysql://192.168.1.8:3306/nls72?serverTimezone=UTC
spring.datasource.username=nls72
spring.datasource.password=nls72@NSN
#如果不使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource

#增加打印sql语句,一般用于本地开发测试
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3 创建数据库表

DROP TABLE if EXISTS `classfication`;
CREATE TABLE `classfication`(
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `name` varchar(128) NOT NULL,
    `create_time` datetime DEFAULT NULL,
    PRIMARY KEY(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

添加默认数据

insert into classfication(name, create_time) values('川菜', date());
insert into classfication(name, create_time) values('湘菜', date());

4 编写实体类

@Getter
@Setter
public class GoodsClassification {
    private Integer id;
    private String name;
    private DateTime createTime;
}

5 编写Mapper

*/
public interface GoodsClassificationMapper {

    @Select("SELECT * FROM classfication")
    @Results({
            @Result(column = "create_time",property = "createTime") 
    })
    List<GoodsClassification> getAll();
}

6 编写service

public interface GoodsClassifyService {
     List<GoodsClassification> getAll() ;
}

@Service
public class GoodsClassifyServiceImpl implements GoodsClassifyService  {
    @Autowired
    private GoodsClassificationMapper mapper;

    @Override
    public List<GoodsClassification> getAll() {
        return mapper.getAll();
    }
}

7 编写controller

@RestController
@RequestMapping("/goods")
public class GoodsClassifyController {
    @Autowired
    GoodsClassifyService goodsClassifyService;

    @GetMapping("/classes")
    public List<GoodsClassification> classfications() {
        return goodsClassifyService.getAll();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值