第一篇有介绍,如何使用网站创建springboot项目,这里介绍如何使用idea创建一个springboot项目
1.1 新建项目
1.2
3.
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
mysql
mysql-connector-java
com.alibaba
druid-spring-boot-starter
1.2.6
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
test
上面是在properties的文件中配置信息,下面是在yml文件中配置信息
# 在properties的文件中配置信息
#配置数据库
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/wyy_music?serverTimezone=Asia/Shanghai&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#mybatis
#指定mapper.xml文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
#指定控制台输出日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#配置连接数据库
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/wyy_music?serverTimezone=Asia/Shanghai&characterEncoding=utf8
type: com.alibaba.druid.pool.DruidDataSource
#配置mybatis
mybatis:
configuration:
#控制台输出
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#mapper.xml文件位置
mapper-locations: classpath:mapper/*.xml
表的创建语句(注意,这是作者写音乐网站小模板用到的表,可以跟我的表不同但是记得更改配置信息中的数据库)
CREATE DATABASE wyy_music;
USE wyy_music;
DROP TABLE IF EXISTS `tb_music`;
CREATE TABLE `tb_music` (
`music_id` INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, -- 歌曲ID
`music_name` VARCHAR(255) NOT NULL, -- 歌曲名称
`music_album_name` VARCHAR(255) NOT NULL, -- 专辑名称
`music_album_picUrl` VARCHAR(255) NOT NULL, -- 专辑图片路径
`music_mp3Url` VARCHAR(255) NOT NULL, -- 歌曲播放路径
`music_artist_name` VARCHAR(255) NOT NULL, -- 歌手名称
`sheet_id` INT(11) DEFAULT NULL -- 对应的歌单ID
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO tb_music VALUES ('1', '光年之外', '光年之外', 'https://imgessl.kugou.com/stdmusic/20161229/20161229233400375274.jpg', 'https://webfs.tx.kugou.com/202109061310/31fb3f36e2048b2172a70e327bbfc8e3/KGTX/CLTX001/f87095bff0de7c636c3a3b8aac702d76.mp3', 'G.E.M.邓紫棋','1');
INSERT INTO tb_music VALUES ('2', '夜空中最亮的星', '世界', 'https://imgessl.kugou.com/stdmusic/20150719/20150719010047203836.jpg', 'https://webfs.ali.kugou.com/202109061306/1b30ae27a5749debd602507b3bf1fea6/G202/M04/1B/13/aocBAF55G0-ADd0HAD2Y88Efqbw072.mp3', '逃跑计划','1');
INSERT INTO tb_music VALUES ('3', '只要平凡', '只要平凡', 'https://imgessl.kugou.com/stdmusic/20180622/20180622194005815458.jpg', 'https://webfs.ali.kugou.com/202109061309/edb2e89d90e66b9d125950dba107e9eb/KGTX/CLTX001/38aead7ed546b0736791ebb25c3a3951.mp3', '张杰/张碧晨','2');
DROP TABLE IF EXISTS `tb_sheet`;
CREATE TABLE `tb_sheet` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sheet_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO tb_sheet VALUES ('1', '热歌榜');
INSERT INTO tb_sheet VALUES ('2', '新歌榜');
INSERT INTO tb_sheet VALUES ('3', '原创榜');
创建实体类
@Data
public class Music {
private Integer musicId;
private String musicName;
private String musicAlbumName;
private String musicAlbumPicurl;
private String musicMp3url;
private String musicArtistName;
private Integer sheetId;
}
创建mapper
@Repository
public interface MusicMapper {
List findAll();
}
创建MusicMapper.xml存放的地址在src\main\resources\mapper
select music_id,music_name,music_album_name,music_album_picUrl,music_mp3Url,music_artist_name,sheet_id from tb_music
创建service
public interface MusicService {
List findAll();
}
创建serviceImpl
@Service
public class MusicServiceImpl implements MusicService {
@Autowired
private MusicMapper musicMapper;
@Override
public List findAll() {
return musicMapper.findAll();
}
}
创建controller
@RestController
@RequestMapping("music")
public class MusicController {
@Autowired
private MusicService musicService;
@RequestMapping("findAll")
public List findAll(){
return musicService.findAll();
}
}
配置启动类
@SpringBootApplication
@MapperScan("com.yt.mapper")//扫描mapper接口填写包名.类名
public class Springboot03Application {
public static void main(String[] args) {
SpringApplication.run(Springboot03Application.class, args);
}
}
启动测试
url为controller下的findAll方法
com.github.pagehelper
pagehelper-spring-boot-starter
1.4.1
//分页
@RequestMapping("findByPage")
public PageInfo findByPage(
@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
@RequestParam(value = "pageSize",required = false,defaultValue = "2")Integer pageSize){
PageHelper.startPage(pageNum,pageSize);
List musicList = musicService.findAll();
PageInfo musicPageInfo = new PageInfo<>(musicList);
return musicPageInfo;
}
#配置分页
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
在postman测试