SSM整合(Spring、SpringMVC、MyBatis三者整合的小案例)

SSM整合(Spring、SpringMVC、MyBatis三者整合的小案例),第1张

SSM整合(Spring、SpringMVC、MyBatis三者整合的小案例) 1.创建一个带webapp骨架的maven工程

 

这是我刚创建好的maven工程的pom.xml文件,我不用的东西,我都把它删了

 

2.导入依赖,下面是我的pom.xml文件



  4.0.0

  com.ayit
  ssm-wyy-music
  1.0-SNAPSHOT
  war

  ssm-wyy-music Maven Webapp
  
  http://www.example.com


  
    
    
      org.springframework
      spring-webmvc
      5.3.10
    
    
    
      org.springframework
      spring-jdbc
      5.3.10
    
    
    
      org.mybatis
      mybatis
      3.5.7
    
    
    
      org.mybatis
      mybatis-spring
      2.0.6
    
    
    
      com.alibaba
      druid
      1.2.7
    
    
    
      mysql
      mysql-connector-java
      8.0.26
    
    
    
      javax.servlet
      jsp-api
      2.0
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.12.3
    
    
    
      org.projectlombok
      lombok
      1.18.20
    
    
    
      com.github.pagehelper
      pagehelper
      5.2.0
    
    
    
      log4j
      log4j
      1.2.17
    
  

 3.创建表
;

create table `music` (
	`music_id` int (11),
	`music_name` varchar (765),
	`music_album_name` varchar (765),
	`music_album_picUrl` varchar (765),
	`music_mp3Url` varchar (765),
	`music_artist_name` varchar (765),
	`sheet_id` int (11)
); 
insert into `music` (`music_id`, `music_name`, `music_album_name`, `music_album_picUrl`, `music_mp3Url`, `music_artist_name`, `sheet_id`) 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 `music` (`music_id`, `music_name`, `music_album_name`, `music_album_picUrl`, `music_mp3Url`, `music_artist_name`, `sheet_id`) 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 `music` (`music_id`, `music_name`, `music_album_name`, `music_album_picUrl`, `music_mp3Url`, `music_artist_name`, `sheet_id`) values('3','只要平凡','只要平凡','https://imgessl.kugou.com/stdmusic/20180622/20180622194005815458.jpg','https://webfs.ali.kugou.com/202109061309/edb2e89d90e66b9d125950dba107e9eb/KGTX/CLTX001/38aead7ed546b0736791ebb25c3a3951.mp3','张杰/张碧晨','2');
 4.创建java、resources目录,并配置

在main包下创建文件夹java和resources

 选中java,点击Mark Directory as 点击Sources root

 

  选中resources,点击Mark Directory as 点击ReSources root

5.创建db.properties、log4j.properties、springmvc.xml、mybatis.xml、applicationContext-bean.xml、applicationContext-tx.xml、MusicMapper.xml配置文件

下面是我的完整目录结构

 

 5.1 db.properties文件(这是我的数据库连接配置文件,可根据自己的数据库改改)
db.username = root
db.password = 123456
db.url = jdbc:mysql://localhost:3306/java2109?serverTimezone=Asia/Shanghai&characterEncoding=UTF8&useSSL=false&useUnicode=true
db.driverClassName = com.mysql.cj.jdbc.Driver
5.2.log4j配置文件 
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
 5.3.springmvc.xml

 

    
    

    
    

    
    


 5.4.mybatis.xml



    
    
    










 5.5.applicationContext-bean.xml



    
    

    
    

    
    
        
        
        
        
    

    
    
        
        

        
        

        
        

        
        
        
            
                
                    
                        
                        
                            
                            helperDialect=mysql
                            
                            reasonable=true
                            
                            supportMethodArguments=true
                        
                    
                
            
        
    

    
    
        
        
    
5.6.applicationContext-tx.xml

(这个是事务配置的xml,我没配置,我没有写增删改的方法,所以就没有配置事务,我就简单的测试了查询所有和查询单个以及分页的方法,感兴趣的可以自己配置)




    

    
5.7.MusicMapper.xml




    
        
        
        
        
        
        
        
    

    
    
        select music_id,music_name,music_album_name,music_album_picUrl,music_mp3Url,music_artist_name,sheet_id from music
    

    
    
    
    
 6.配置webapp包下的web.xml文件



  Archetype Created Web Application

  
  
    contextConfigLocation
    classpath*:applicationContext-*.xml
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:springmvc.xml
    
  
  
    dispatcherServlet
    /
  

7.编写Music实体类
package com.ayit.pojo;

import lombok.Data;

@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;
}
 8.编写dao层的MusicMapper接口
package com.ayit.mapper;

import com.ayit.pojo.Music;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MusicMapper {
    List findAll();

    Music findById(Integer id);
}

 9.编写service层的MusicService接口及对应的实现类
package com.ayit.service;

import com.ayit.pojo.Music;

import java.util.List;

public interface MusicService {
    List findAll();

    Music findById(Integer id);
}
package com.ayit.service.impl;

import com.ayit.mapper.MusicMapper;
import com.ayit.pojo.Music;
import com.ayit.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicMapper musicMapper;

    @Override
    public List findAll() {
        return musicMapper.findAll();
    }

    @Override
    public Music findById(Integer id) {
        return musicMapper.findById(id);
    }
}
10.编写controller层的MusicController
package com.ayit.controller;

import com.ayit.pojo.Music;
import com.ayit.service.MusicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("music")
public class MusicController {

    @Autowired
    private MusicService musicService;

    //查询所有歌曲方法
    @RequestMapping("findAll")
    public List findAll(){
        return musicService.findAll();
    }

    //查询单个歌曲的方法
    @RequestMapping("findById")
    public Music findById(@RequestParam("musicId") Integer id){
        return musicService.findById(id);
    }

    //分页查询方法
    @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;
    }
}
11.启动服务器,输入网址(url)进行测试

 

 输入对应网址:http://localhost:8080/music/findAll

因为做的是前后端分离项目,所以页面呈现的是从数据库中查出来的转换成的Json格式的数据

 输入对应的网址:http://localhost:8080/music/findById?musicId=1

musicId=?后面传参你可以自己设置1或则2或则3(我创建的表只有三条数据)

 输入对应网址:http://localhost:8080/music/findByPage?pageNum=1&pageSize=2

pageNum:对应的是当前页码,PageSize:对应的是当前页显示的数据的条数

我传的数据是第一页,每页显示两条数据

 到此,ssm整合的小案例完成,前后端分离的小项目,我只写了后端部分的功能,感兴趣的可以私聊,我把前端资源发给你

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4746178.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存