springboot结合mybatis多套druid数据源入门级音乐网站后台

SpringBoot+Vue+MyBatis全栈音乐网站开发实战 前后端分离架构是现代Web开发的主流模式,通过RESTful API实现前后端解耦。SpringBoot作为后端框架提供自动配置和快速开发能力,Vue.js构建响应式前端界面,MyBatis处理数据持久化层。这种技术组合特别适合实现包含复杂业务逻辑的Web应用,如音乐类网站常见的多条件查询、文件上传和用户交互功能。项目中展示的动态SQL编写、跨域解决方案和Druid连接池配置,都是企业级开发中的高频技术要点。通过完整的专辑管理系统案例,开发者可以掌握从数据库设计到容器化部署的全流程实践技能。 阅读详情

   这几天研究springboot和mybatis,正好手里边有个原来做过的javaweb项目,就直接拿过来整合了起来。这里前台用vue.js实现的代码就不分享了,前后端分离么,不影响看后边。想看整套源码可以到文章末尾去拿。


自动忽略那几个字。。。实在是想不起什么歌名了。。。

首先pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    <!--在这里加了热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>

    </dependency>

    <!--springboot test-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- ali datasource -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.9</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15</version>
    </dependency>
</dependencies>

项目结构图



有两个配置文件,也可以放到一起,当时因为整两套数据源老出错,就把它们分开了

这个config目录下的配置

server.port=8088
server.servlet.context-path=/music
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java
spring.devtools.restart.additional-exclude=static

这是classpath:下的

musicweb.datasource.url=jdbc:mysql://localhost:3306/musicweb?useSSL=false&characterEncoding=utf8
musicweb.datasource.username=root
musicweb.datasource.password=leishao
musicweb.datasource.driverClassName=com.mysql.jdbc.Driver

musictop.datasource.url=jdbc:mysql://localhost:3306/musictop?useSSL=false&characterEncoding=utf8
musictop.datasource.username=root
musictop.datasource.password=leishao
musictop.datasource.driverClassName=com.mysql.jdbc.Driver

关于DataSource和SqlSessionFactory在config目录下


下边是musictop数据源和它的SqlSessionFactory配置类

@Configuration
public class MusictopDataSource {

    @Value("${musictop.datasource.url}")
    private String url;

    @Value("${musictop.datasource.username}")
    private String user;

    @Value("${musictop.datasource.password}")
    private String password;

    @Value("${musictop.datasource.driverClassName}")
    private String driverClass;


    @Bean(name="musictop")
    @Qualifier("musictop")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }
    @Bean(name = "musictopTransactionManager")
    public DataSourceTransactionManager musictopTransactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

}
@Configuration
@MapperScan(basePackages = MusictopSessionFactory.PACKAGE,sqlSessionFactoryRef = "musictopSqlSessionFactory")
@ToString
public class MusictopSessionFactory {

    // 精确到 musictop 目录,以便跟其他数据源隔离
    static final String PACKAGE = "com.lei.music.dao.musictop";
    static final String MAPPER_LOCATION = "classpath:mapper/musictop/*.xml";



    @Bean(name = "musictopSqlSessionFactory")
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("musictop") DataSource musictopDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(musictopDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MusictopSessionFactory.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

下边是musicweb的DataSource和SqlSeessionFactory的配置

@Configuration
public class MusicwebDataSource {

    @Value("${musicweb.datasource.url}")
    private String url;

    @Value("${musicweb.datasource.username}")
    private String user;

    @Value("${musicweb.datasource.password}")
    private String password;

    @Value("${musicweb.datasource.driverClassName}")
    private String driverClass;


    @Bean(name="musicweb")
    @Primary
    @Qualifier("musicweb")
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;

    }
    @Bean(name = "musicwebTransactionManager")
    @Primary
    public DataSourceTransactionManager musictwebTransactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

}
@Configuration
//扫描Mapper接口并容器管理
@MapperScan(basePackages = MusicwebSessionFactory.PACKAGE,sqlSessionFactoryRef = "musicwebSqlSessionFactory")
@ToString
public class MusicwebSessionFactory {


    static final String PACKAGE = "com.lei.music.dao.musicweb";
    static final String MAPPER_LOCATION = "classpath:mapper/musicweb/*.xml";



    @Bean(name = "musicwebSqlSessionFactory")
    @Primary
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("musicweb") DataSource musicwebDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(musicwebDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MusicwebSessionFactory.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}
接下来就是设置跨域请求
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    //跨域请求

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                //跨域允许的时间
                .maxAge(3600);
    }
}

到了这一步,基本的配置就完毕了。其实之后的代码基本就是,完成一个,复制粘贴,没什么难度。我这里就展示一下其中的一个实现。

首先。entity下有两个目录,分别对应两个数据库中的表。简单点先看看musicweb下的member类


@Data
public class memBer {
    private Integer id;
    private String username;
    private String name;
    private String pwd;
    private String email;
    private String tel;
    private String date;


}

之后到DAO的musicweb目录下看与member对应的UserDao

public interface UserDao {
    List<memBer> queryMembers();
    int saveMember(memBer member);
    int updateMember(memBer member);
    int removeMember(Integer id);
}

完后再resource的mapper目录下建立与UserDao对应的UserMapper文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lei.music.dao.musicweb.UserDao">
    <resultMap id="BaseResultMap" type="com.lei.music.entity.musicweb.memBer">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="pwd" property="pwd" jdbcType="VARCHAR"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="tel" property="tel" jdbcType="VARCHAR"/>
        <result column="date" property="date" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,username,name,pwd,email,tel,date
    </sql>
    <select id="queryMembers" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from member
    </select>

    <insert id="saveMember" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.lei.music.entity.musicweb.memBer">
        insert  into member(username,name,pwd,email,tel,date) values (#{username},#{name},#{pwd},#{email},#{tel},#{date})
    </insert>

    <delete id="removeMember" parameterType="Integer">
        delete from member where id=#{id}
    </delete>

    <update id="updateMember" parameterType="com.lei.music.entity.musicweb.memBer">
        update member set username=#{username},name=#{name},pwd=#{pwd},email=#{email},tel=#{tel},date=#{date} where id=#{id}
    </update>


</mapper>

到这流程就差不多了。

源码地址:https://download.youkuaiyun.com/download/weixin_41228362/10503975

全栈音乐网站开发项目:Spring Boot+Mybatis+Vue.js实战 后端服务是现代Web应用的核心,它处理前端发来的请求,并返回相应的数据或执行特定的操作。Spring Boot以其“约定优于配置”的理念,大大简化了基于Spring框架的项目搭建和开发过程。通过Spring Initializr可以快速生成项目结构,而Maven或Gradle则作为依赖管理和构建工具,是现代Java开发的标配。MyBatis是一个优秀的持久层框架,支持定制化SQL、存储过程以及高级映射。它避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。 阅读详情

相关推荐

springboot+mybatis后台vue.js实现前端的音乐网站项目大学生毕业设计

看界面效果可以在百度搜素“springboot结合mybatis多套druid数据源入门级音乐网站后台”那篇博客,里边有后台实现的代码,还有几张前台的照片。

springboot+mybatis+druid 多数据源整合

前言:在上一篇文章里面我们进行了spring boot 和 mybatis 的整合 《springboot整合mybatis使用druid数据源》, 文中是使用的单数据源,因为项目中有很多需要用到多数据源的场景,比如主从同步(读写分离), 或者业务库拆分等,这样就需要操作不同的数据库,所以,集成多数据源就很有必要了。 网上关于 spring boot mybati...

white_ice的博客 3955

springboot+mtbatis前后端分离开发音乐网站

springboot+mtbatis前后端分离开发音乐网站,资源包括后台代码、前台网页、sql文件

SpringBoot整合三------MyBatis篇

springboot整合mybatis

qq_43513370的博客 1758

vue+springboot+mybatis plus整合.rar

vue+springboot+mybatis plus整合crm模块,思路注释清晰,仅限学习参考

【5G】5G 核心网(5GC)基本架构

本文旨在科普5G核心网相关知识...

Learning makes me happy (0^◇^0) 4万+

【springboot&vue 音乐网站 1】<03-04>环境配置、工程搭建

跟做,课程视频:https://www.bilibili.com/video/BV1Ck4y127cg 后台搭建music-server alimaven central aliyun maven http://maven.aliyun.com/nexus/content/groups/public 添加包(pom) <properties> <java.version>1.8</java.version> </pro

m0_51252051的博客 214

Halcon异常检测训练避坑指南:如何避免GPU内存溢出和样本不足问题

本文针对Halcon深度学习异常检测训练中常见的GPU内存溢出和样本不足两大难题,提供了从原理分析到实战解决方案的深度指南。通过调整批次大小、图像尺寸、使用梯度累积等技巧有效管理显存,并利用针对性数据增强与人工合成策略破解小样本困境,帮助开发者构建稳定高效的训练流程。

river的博客 215

2026-2027年最值得选择的Java毕业设计选题大全:1000个热门选题推荐✅✅✅

💗博主介绍:✌全网粉丝1W+,优快云作者、博客专家、全栈领域优质创作者,博客之星、平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌💗🌟🌟毕业设计选题非常重要!非常重要!非常重要!所以,建议对课题实现技术不清晰的同学,最好是找自己的研究生学长或者老师详细的把关机技术以及实现流程理清楚,当然也可以。文章最下面有微信,获取完整源码和数据库,非开源!!!

java全包开发,java辅导 8342

Android 的端智能学习路径

基础层:先补齐 Android 进阶(JNI / 性能优化)和 AI 基础概念,无需深入算法开发;核心层:掌握 TFLite 的模型加载、推理、量化,能完成基础端侧推理;落地层:聚焦实战场景(图像 / OCR / 语音),解决工程化问题(模型管理、异常处理);优化层:通过模型量化、硬件加速、工程优化,提升推理性能和用户体验。

hqiangtai的博客 344

逆变器方案(工频全套)工频正弦波逆变器开发板全套设计资

逆变器方案(工频全套)工频正弦波逆变器开发板,24V-72V通用12只TO-247封装MOS管,额定输出功率6500W以下;配有触摸屏操作界面,运行数据一目了然,带市电互补UPS功能,可选择电池优先或市电有限工作,我们的开发板均从实战角度出发,让您的学习更便捷,包含全套设计资料,PCB.原理图.源代码,程序均有中文注释,MCU选用美国微芯公的PIC-8位单片机,民用数字电源首选单片机,抗干扰能力强,运行稳定。...

m0_73494033的博客 890

2026-2076年最值得选择的Java毕业设计选题大全:1000个热门选题推荐✅✅✅

💗博主介绍:✌全网粉丝10W+,优快云作者、博客专家、全栈领域优质创作者,博客之星、平台优质作者、专注于Java、小程序、python等技术领域和毕业项目实战✌💗🌟🌟毕业设计选题非常重要!非常重要!非常重要!所以,建议对课题实现技术不清晰的同学,最好是找自己的研究生学长或者老师详细的把关机技术以及实现流程理清楚,当然也可以文章最下面有微信,获取完整源码和数据库,非开源!!!

java,小程序,python,大数据项目定制开发 3020

2023年第二十届五一数学建模竞赛C题:“双碳”目标下低碳建筑研究-思路详解与代码答案

2023年第二十届五一数学建模竞赛C题:“双碳”目标下低碳建筑研究-思路详解与代码答案

lichensun的博客 1万+

2026-2027年最值得选择的Java、Python毕业设计选题大全:1000个热门选题推荐✅✅✅

💗博主介绍:✌全网粉丝20W+,优快云作者、博客专家、全栈领域优质创作者,博客之星、平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌💗🌟🌟毕业设计选题非常重要!非常重要!非常重要!所以,建议对课题实现技术不清晰的同学,最好是找自己的研究生学长或者老师详细的把关机技术以及实现流程理清楚,当然也可以。文章最下面,获取完整源码和数据库,非开源!!!

leishencode的博客 1232

保姆级教程:用安卓手机+IP摄像头App,5分钟搭建一个免费的RTSP监控测试环境

本文提供了一份详细的保姆级教程,教你如何利用安卓手机和IP摄像头App在5分钟内搭建免费的RTSP监控测试环境。通过零成本方案,开发者可以快速获取实时视频流,适用于计算机视觉、物联网开发等多种场景。教程涵盖工具选型、配置详解、电脑端测试及OpenCV集成,助你高效完成开发测试。

weixin_27419879的博客 274

为什么我的Win11家庭版找不到BitLocker?教你两种强制关闭加密的隐藏方法

本文解释了Win11家庭版为何缺少BitLocker功能,并提供了两种强制关闭加密的隐藏方法。通过注册表修改和PowerShell命令,用户可以调出隐藏的加密控制面板或直接操控加密引擎,解决家庭版设备加密的管理难题。

weixin_42535703的博客 1761

2026-2027年最值得选择的Java设计选题大全:1000个热门选题推荐✅✅✅

基于jsp+mysql+Spring+mybatis+Spring boot+Vue的SpringBoot企业固定资产管理系统。基于jsp+mysql+Spring+mybatis+Springboot的Springboot在线家庭记账管理平台。基于jsp+mysql+Spring+mybatis+Spring boot的Springboot医疗挂号管理系统。基于jsp+mysql+Spring+mybatis+Spring boot的Springboot医疗挂号管理系统。

java,python,小程序,大数据开发 217

SAP ABAP 实用小技巧合集 (一)

本合集包含了笔者从事 ABAP 开发 17 年以来积累的小技巧。持续更新。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3215

spring boot整合mybatis通用mapper实现Druid多数据源

springboot加通用mapper实现Druid多数据源

LMS的专栏 7599

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

基于Vue-cli,Springboot,mybaits的java音乐网站

基于Vue-cli,Springboot的java音乐网站主页注册登录界面歌手分类歌单分类歌手介绍歌曲介绍歌曲播放个人主页 主页 注册登录界面 歌手分类 歌单分类 歌手介绍 歌曲介绍 歌曲播放 个人主页 ...

Dominiquewei的博客 2331

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一、多数据源的应用场景二、运行 springboot-mybatis-mutil-datasource 工程案例三、springboot-mybatis-mutil-datasource 工程代码配置详解 一、多数据源的应用场景 ...

weixin_34419326的博客 473

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

SpringBoot入门之基于Druid配置Mybatis多数据源

上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源。SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,SpringBoot就会基于约定根据配置信息实例化对象,但是一般大型的项目都是有多个子系统或者多个数据源组成,那怎么使用SpringBoot进行Mybatis多数据源配置呢? 一、数据库准备 我们这里准备...

weixin_34186931的博客 449
上一篇: InteliJ IDEA开发工具简单实现Spring-boot+mybatis注解形式和xml形式的maven项目
下一篇: 关于mave的一些运用
刘sir--
博客等级 码龄9年 20粉丝 33原创
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值