美文网首页
java基础-day58-SSM分模块整合

java基础-day58-SSM分模块整合

作者: 触手不可及 | 来源:发表于2021-07-17 22:12 被阅读0次

SSM分模块整合

1.创建父工程 music-parent,打包方式为 Pom

2.创建子工程 music-dao,打包方式为 Jar

2.1 导入依赖 pom.xml
<dependencies>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.19</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        
        <!-- 分頁插件 pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.11</version>
        </dependency>

    </dependencies>
2.2 编写实体类
package com.qf.pojo;

import lombok.Data;

@Data
public class Music {

    private String music_id;
    private String music_name;
    private String music_album_name;
    private String music_album_picUrl;
    private String music_mp3Url;
    private String music_artist_name;
    private Integer sheet_id;
}

2.3 编写 Mapper
package com.qf.dao;

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

import java.util.List;

@Repository
public interface MusicMapper {

    //@Select("select * from tb_music")
    List<Music> findAll();

    List<Music> findByPage();
}

2.4 编写 Mapper.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.qf.dao.MusicMapper">

    <select id="findAll" resultType="com.qf.pojo.Music">
        select * from tb_music
    </select>

    <select id="findByPage" resultType="com.qf.pojo.Music">
        select * from tb_music
    </select>

</mapper>
2.5 编写 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wyy_music?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
2.6 编写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        <!-- 扫描注解 -->
        <context:component-scan base-package="com.qf"/>

    <!-- 配置 读取properties文件 jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!-- 配置 数据源 -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
    </bean>

    <!--配置SqlSessionFactoryBean-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!--配置mybatis 插件-->
        <property name="plugins">
            <set>
                <!--配置pageHelper 分页插件-->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <!-- 数据库方言,可选择:oracle,mysql,mariadb 等-->
                            <prop key="helperDialect">mysql</prop>
                            <!--reasonable:分页合理化参数,默认值:false。
                            当该参数设置为true时,pageNum<=0时会查询第一页,
                            pageNum>pages(超过总数时),会查询最后一页-->
                            <prop key="reasonable">true</prop>
                            <!--supportMethodsArguments:
                            是否支持通过 Mapper 接口参数来传递分页参数,默认值:false-->
                            <prop key="supportMethodsArguments">true</prop>
                        </props>
                    </property>
                </bean>
            </set>
        </property>
    </bean>

    <!--配置Mapper扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--设置Mapper扫描包-->
        <property name="basePackage" value="com.qf.dao"></property>
    </bean>

</beans>

3.创建子工程music-service,打包方式为Jar

3.1 导入依赖 pom.xml
<dependencies>
        <dependency>
            <groupId>com.qf</groupId>
            <artifactId>music-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
3.2 编写接口
package com.qf.service;

import com.qf.pojo.Music;

import java.util.List;


public interface MusicService {

    List<Music> findAll();

    List<Music> findByPage();
}

3.3 编写实现类
package com.qf.service.impl;import com.qf.dao.MusicMapper;import com.qf.pojo.Music;import com.qf.service.MusicService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class MusicServiceImpl implements MusicService {    @Autowired    private MusicMapper musicMapper;    public List<Music> findAll() {        return musicMapper.findAll();    }    public List<Music> findByPage() {        return musicMapper.findByPage();    }}

4.创建子工程 music-web,打包方式为 War

4.1 导入依赖 pom.xml
<dependencies>        <dependency>            <groupId>com.qf</groupId>            <artifactId>music-service</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>       <!-- springmvc -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>5.2.4.RELEASE</version>        </dependency>     <!-- servlet -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>            <version>2.5</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>jsp-api</artifactId>            <version>2.0</version>            <scope>provided</scope>        </dependency>        <!-- jackson -->        <dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>            <version>2.10.3</version>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>            <scope>test</scope>        </dependency>    </dependencies>
4.2 配置 springmvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="com.qf"></context:component-scan>    <mvc:annotation-driven></mvc:annotation-driven>    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>
4.3 配置 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>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <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>    <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>
4.4 编写 Controller
package com.qf.controller;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.qf.dao.MusicMapper;import com.qf.pojo.Music;import com.qf.service.MusicService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.Mapping;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;@RestControllerpublic class MusicController {    @Autowired    private MusicService musicService;    @RequestMapping("music/findAll")    public List<Music> findAll(){        return musicService.findAll();    }    @RequestMapping("music/findByPage")    public PageInfo findByPage(@RequestParam(required = false,defaultValue = "2") Integer pageNum,                               @RequestParam(required = false,defaultValue = "2") Integer pageSize){        PageHelper.startPage(pageNum,pageSize);        List<Music> list=musicService.findByPage();        PageInfo pageInfo = new PageInfo(list);        return pageInfo;    }}
依次 Install music-dao,music-service 然后启动 music-web 访问,该案例中整合并测试分页插件

相关文章

  • java基础-day58-SSM分模块整合

    SSM分模块整合 1.创建父工程 music-parent,打包方式为 Pom 2.创建子工程 music-dao...

  • 23.深入浅出模块化(含 tree shaking)(上)

    模块化是工程化的基础:只有能将代码模块化,拆分为合理单元,才具备调度整合的能力,才有架构和工程一说。早期,Java...

  • Java面试题

    Java面试 Java、框架、JVM Java 基础、容器、多线程、反射、对象拷贝、Java web模块、异常模块...

  • Spring-SSM整合

    零、本文纲要 一、SSM整合基础 创建工程 SSM整合 功能模块 项目目录结构 二、详细代码 基础依赖pom.xm...

  • JAVA基础整合

    Win7下如何打开DOS控制台? a:开始--所有程序--附件--命令提示符 b:开始--搜索程序和文件--cmd...

  • 2019年最新145道面试题整理(Java向)这些都会还不轻松入

    面试题模块介绍 我们这份面试题,包含的内容了十二个模块:Java 基础、Java IO、Java Web、JVM、...

  • Spring Framework

    WHY? Java应用中有大量的对象,他们之间存在着依赖关系。虽然Java语言很强大易用,但却缺少模块整合...

  • 2020年的秋招已经开始了!最新Java面试题大全(文末附参考答

    包含的模块 本文分为十九个模块,分别是:Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网...

  • JAVA面试(上)

    包含的模块本文分为十九个模块,分别是: Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网...

  • JAVA(上)

    包含的模块本文分为十九个模块,分别是: Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网...

网友评论

      本文标题:java基础-day58-SSM分模块整合

      本文链接:https://www.haomeiwen.com/subject/mplgpltx.html