1.创建一个带webapp骨架的maven工程
?
这是我刚创建好的maven工程的pom.xml文件,我不用的东西,我都把它删了
?
2.导入依赖,下面是我的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ayit</groupId>
<artifactId>ssm-wyy-music</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssm-wyy-music Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<dependencies>
<!--springMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<!--spring连接数据库依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.10</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--mybatis整合spring依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--Druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.7</version>
</dependency>
<!--mysql连接数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--Jackson解析依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!--lombok插件依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!--pageHelper分页插件依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<!--log4j日志依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
?3.创建表
/*
SQLyog Ultimate v13.1.1 (64 bit)
MySQL - 5.7.29
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!--扫描对应包下的注解-->
<context:component-scan base-package="com.ayit.controller"></context:component-scan>
<!--配置组件-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--放行静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
?5.4.mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--PageHelper插件配置的第二种方式,在mybatis-config.xml文件中配置-->
<!-- <plugins>-->
<!-- <!– com.github.pagehelper为PageHelper类所在包名 –>-->
<!-- <plugin interceptor="com.github.pagehelper.PageInterceptor">-->
<!-- <!– 使用下面的方式配置参数,后面会有所有的参数介绍 –>-->
<!-- <property name="helperDialect" value="mysql"/>-->
<!-- <property name="reasonable" value="true"/>-->
<!-- <property name="supportMethodsArguments" value="true"/>-->
<!-- </plugin>-->
<!-- </plugins>-->
</configuration>
?5.5.applicationContext-bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描包下的注解-->
<context:component-scan base-package="com.ayit"></context:component-scan>
<!--引入db.properties文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="url" value="${db.url}"></property>
<property name="driverClassName" value="${db.driverClassName}"></property>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--加载Resources目录下的mapper.xml文件-->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
<!--配置分页插件PageHelper-->
<!-- 注意其他配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
<!--数据库方言-->
helperDialect=mysql
<!--合理化参数-->
reasonable=true
<!--支持通过mapper接口参数来传递分页参数-->
supportMethodArguments=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--扫描java目录下的mapper包下的mapper接口文件-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定扫描的包的路径-->
<property name="basePackage" value="com.ayit.mapper"></property>
</bean>
</beans>
5.6.applicationContext-tx.xml
(这个是事务配置的xml,我没配置,我没有写增删改的方法,所以就没有配置事务,我就简单的测试了查询所有和查询单个以及分页的方法,感兴趣的可以自己配置)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--事务-->
<!--AOP-->
</beans>
5.7.MusicMapper.xml
<?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.ayit.mapper.MusicMapper">
<resultMap id="musicMap" type="com.ayit.pojo.Music">
<id property="musicId" column="music_id"></id>
<result property="musicName" column="music_name"></result>
<result property="musicAlbumName" column="music_album_name"></result>
<result property="musicAlbumPicurl" column="music_album_picUrl"></result>
<result property="musicMp3url" column="music_mp3Url"></result>
<result property="musicArtistName" column="music_artist_name"></result>
<result property="sheetId" column="sheet_id"></result>
</resultMap>
<!--sql片段-->
<sql id="BaseSql">
select music_id,music_name,music_album_name,music_album_picUrl,music_mp3Url,music_artist_name,sheet_id from music
</sql>
<!--查询所有歌曲-->
<select id="findAll" resultMap="musicMap">
<!--引入sql片段-->
<include refid="BaseSql"></include>
</select>
<!--查询单个歌曲-->
<select id="findById" resultMap="musicMap">
<include refid="BaseSql"></include>
where music_id = #{id}
</select>
</mapper>
?6.配置webapp包下的web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--加载applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<!--配置监听器,服务器启动的时候就是applicationContext.xml文件加载的时候-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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<Music> 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<Music> 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<Music> 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<Music> 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<Music> musicList = musicService.findAll();
PageInfo<Music> 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整合的小案例完成,前后端分离的小项目,我只写了后端部分的功能,感兴趣的可以私聊,我把前端资源发给你
|