1.描述:分为mysql和oracle的数据库来区别其实在springboot里区别就在配置文件和mybatis的编写,其他的都可以完全复用。
2.各层级
a.controller,可以复用,同时写了个定时器每天下午五点只要服务启动了就会跑一遍接口。
package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.enetity.ResponseBean;
import com.example.demo.service.MyMusicServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin
@RestController
@RequestMapping("/music")
public class MyMusicController {
@Autowired
MyMusicServer myMusicServer;
@Scheduled(cron = "0 0 17 * * *")
@RequestMapping(value = "/load")
public ResponseBean loadMusicList() {
return myMusicServer.loadMusicList();
}
}
b.接口以及实现类
package com.example.demo.service;
import com.example.demo.enetity.ResponseBean;
public interface MyMusicServer {
public ResponseBean loadMusicList();
}
package com.example.demo.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.NsMusicMapper;
import com.example.demo.enetity.NsMusic;
import com.example.demo.enetity.ResponseBean;
import com.example.demo.service.MyMusicServer;
import com.example.demo.util.CommonUtils;
import com.example.demo.util.FileUtils;
import com.example.demo.util.MP3Player;
import com.example.demo.util.Mp3PlayUtils;
import javazoom.jl.player.Player;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class MyMusicImpl implements MyMusicServer {
@Autowired
NsMusicMapper musicMapper;
public static Player player;
@Transactional
@Override
public ResponseBean loadMusicList() {
FileUtils fileUtils = new FileUtils();
List localMusicList = fileUtils.getLocalMusicList();
ArrayList arrayList = new ArrayList();
Date date = new Date();
Mp3PlayUtils mp3PlayUtils = new Mp3PlayUtils();
for (int i = 0; i < localMusicList.size(); i++) {
NsMusic nsMusic = new NsMusic();
nsMusic.setUpdateTime(date);
String musicName = localMusicList.get(i) + "";
if (musicName != null && musicName.indexOf("mp3") != -1) {
nsMusic.setMusicName(mp3PlayUtils.returnMusicInfo(musicName).get("musicName") + "");
nsMusic.setSinger(mp3PlayUtils.returnMusicInfo(musicName).get("singer") + "");
if (mp3PlayUtils.returnMusicInfo(musicName).get("time") != null) {
nsMusic.setTime(Long.parseLong(mp3PlayUtils.returnMusicInfo(musicName).get("time") + ""));
}
nsMusic.setLocalUrl(musicName);
arrayList.add(nsMusic);
}
}
musicMapper.insertList(arrayList);
return CommonUtils.succssonJson();
}
}
c.实体类
package com.example.demo.enetity;
import java.util.Date;
public class NsMusic {
private Long id;
private String musicName;
private String singer;
private String localUrl;
private String downUrl;
private Date updateTime;
private Long time;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMusicName() {
return musicName;
}
public void setMusicName(String musicName) {
this.musicName = musicName == null ? null : musicName.trim();
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer == null ? null : singer.trim();
}
public String getLocalUrl() {
return localUrl;
}
public void setLocalUrl(String localUrl) {
this.localUrl = localUrl == null ? null : localUrl.trim();
}
public String getDownUrl() {
return downUrl;
}
public void setDownUrl(String downUrl) {
this.downUrl = downUrl == null ? null : downUrl.trim();
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
}
d.其实到这里为止都一样的oracle和mysql还全部都是一样的。
e。不同点:
application.xml的配置
#mysql的配置
spring.datasource.url=jdbc:mysql://localhost:3306/ytuse?serverTimezone=Hongkong&characterEncoding=UTF-8&allowMultiWueries=true
spring.datasource.username=root
spring.datasource.password=11yantao
spring.datasoure.driver-class-name=com.mysql.jdbc.Driver
#oracle的配置
#spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
#spring.datasource.username=scott
#spring.datasource.password=tiger
#spring.datasoure.driver-class-name=oracle.jdbc.driver.OracleDriver
批量插入sql的编写
<insert id="insertListMysql">
insert into ns_music ( music_name, singer,
local_url, down_url, update_time,
time)
values
<foreach collection="list" item="bean" separator=",">
( #{
bean.musicName,jdbcType=VARCHAR}, #{
bean.singer,jdbcType=VARCHAR},
#{
bean.localUrl,jdbcType=VARCHAR}, #{
bean.downUrl,jdbcType=VARCHAR}, #{
bean.updateTime,jdbcType=TIMESTAMP},
#{
bean.time,jdbcType=BIGINT})
</foreach>;
</insert>
<insert id="insertListOracle" useGeneratedKeys="false">
INSERT
into ns_music (id, music_name, singer,
local_url, down_url, update_time,
time)
(select
ns_music_sequence.nextval,temp.* from (
<foreach collection="list" item="bean" separator="union all">
select
#{
bean.musicName,jdbcType=VARCHAR} music_name, #{
bean.singer,jdbcType=VARCHAR} singer,
#{
bean.localUrl,jdbcType=VARCHAR} local_url, #{
bean.downUrl,jdbcType=VARCHAR} down_url,
#{
bean.updateTime,jdbcType=TIMESTAMP} update_time,
#{
bean.time,jdbcType=BIGINT} times
from dual
</foreach>
) temp
)
</insert>
这里需要注意的是 ns_music_sequence这个是oracle的自增索引 CREATE SEQUENCE ns_music_sequence BY 1 START WITH 1 ,命名的话建议以表名加上索引英文命名好点,然后每次插入取nextval就行,而mysql就不需要这样了,直接给个null,
在列名上设置了主键自增就自己增加,无需代码里添加。至于有些工具类的,是我写的方法,可以交流下,主要是想比较下
mysql和oracle批量插入的异同,感觉挺好的。
3.批量更新操作,其实理论上还是一条一条更新,毕竟条件不一样,改变的东西不一样吧,需要配置文件修改成允许批量操作才可。
<update id="updateList" parameterType="com.example.demo.enetity.NsMusic">
<foreach collection="list" item="bean" open="begin" separator=";" close=";end;">
update ns_music
set music_name = #{
bean.musicName,jdbcType=VARCHAR},
singer = #{
bean.singer,jdbcType=VARCHAR},
local_url = #{
bean.localUrl,jdbcType=VARCHAR},
down_url = #{
bean.downUrl,jdbcType=VARCHAR},
update_time = #{
bean.updateTime,jdbcType=TIMESTAMP},
time = #{
bean.time,jdbcType=BIGINT}
where id = #{
bean.id,jdbcType=BIGINT}
</foreach>
</update>