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();
}
}