SSM分模块整合

SSM分模块整合

1.创建父工程 ssm-wyy-music-parent

打包方式为 pom

2.创建子工程 ssm-wyy-music-dao

打包方式为 jar

2.1 导入依赖 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">
    <parent>
        <artifactId>ssm-wyy-music-parent</artifactId>
        <groupId>com.qf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ssm-wyy-music-dao</artifactId>

    <!--  打包方式jar -->
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.10</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>

    </dependencies>

</project>
2.2 使用逆向工程生成pojo,mapper以及mapper.xml并拷贝到指定位置

在Mappper接口上添加@Repository注解

2.3 编写 db.properties
db.username = root
db.password = root
db.url = jdbc:mysql:///java2109?serverTimezone=Asia/Shanghai&characterEncoding=UTF8
db.driverClassName = com.mysql.cj.jdbc.Driver
2.4 编写applicationContext-dao.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"> <!-- bean definitions here -->

        <!-- 扫描对应包下的注解 -->
        <context:component-scan base-package="com.qf">
            <!-- 排除不扫描的注解 -->
<!--            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
        </context:component-scan>

        <!-- 引入配置文件-->
        <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>

        <!-- 配置SqlSessionFactory -->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 必选属性,配置dataSource数据源 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 非必选属性,可引入mybatis-config.xml配置文件 -->
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
            <!-- 非必选属性,当Mapper接口和对应的Mapper.xml文件不在同一目录时,
            可配置Mapper.xml文件的具体位置 -->
<!--            <property name="mapperLocations" value="classpath:mapper/MusicMapper.xml"></property>-->
<!--            <property name="mapperLocations" value="classpath:mapper/*.xml"></property>-->

            <!-- 注意其他配置 -->
            <property name="plugins">
                <array>
                    <bean class="com.github.pagehelper.PageInterceptor">
                        <property name="properties">
                            <!--使用下面的方式配置参数,一行配置一个 -->
                            <value>
                                helperDialect=mysql
                                reasonable=true
                                supportMethodsArguments=true
                            </value>
                        </property>
                    </bean>
                </array>
            </property>

        </bean>

        <!-- 扫描Mapper接口,生成代理对象 -->
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.qf.mapper"></property>
        </bean>

</beans>
2.5 如果需要则创建mybatis-config.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>

<!--<plugins>-->
<!--    <plugin interceptor="com.github.pagehelper.PageInterceptor">-->
<!--        <property name="helperDialect" value="mysql"/>-->
<!--        <property name="reasonable" value="true"/>-->
<!--        <property name="supportMethodsArguments" value="true"/>-->
<!--    </plugin>-->
<!--</plugins>-->

</configuration>

3.创建子工程ssm-wyy-music-service

打包方式为jar

3.1 导入依赖 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">
    <parent>
        <artifactId>ssm-wyy-music-parent</artifactId>
        <groupId>com.qf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ssm-wyy-music-service</artifactId>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.qf</groupId>
            <artifactId>ssm-wyy-music-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.10</version>
        </dependency>

    </dependencies>

</project>
3.2 编写接口

MusicService

package com.qf.service;

import com.qf.pojo.Music;
import com.qf.vo.QueryVo;

import java.util.List;

public interface MusicService {

    public List<Music> findAll();

    public Music findById(Integer id);

}
3.3 编写实现类

MusicServiceImpl

package com.qf.service.impl;

import com.qf.mapper.MusicMapper;
import com.qf.pojo.Music;
import com.qf.pojo.MusicExample;
import com.qf.service.MusicService;
import com.qf.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private MusicMapper musicMapper;

    @Override
    public List<Music> findAll() {
        return musicMapper.selectByExample(null);
    }

    @Override
    public Music findById(Integer id) {
        return musicMapper.selectByPrimaryKey(id);
    }

}
3.4 如果需要,创建applicationContext-service.xml

dataSource可能报红,但不影响

<?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"> <!-- bean definitions here -->

        <!-- 事务平台管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>

        <!-- 配置事务通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
                <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
            </tx:attributes>
        </tx:advice>

        <!-- 配置事务切面 -->
        <aop:config>
            <aop:pointcut id="txPc" expression="execution(* com.qf.service.impl.*ServiceImpl.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"></aop:advisor>
        </aop:config>


</beans>

4.创建工程 ssm-wyy-music-web

打包方式为 war

4.1 导入依赖 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.qf</groupId>
  <artifactId>ssm-wyy-music-web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>ssm-wyy-music-web Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <dependencies>
    <dependency>
      <groupId>com.qf</groupId>
      <artifactId>ssm-wyy-music-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.10</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.0</version>
    </dependency>

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

  </dependencies>


</project>
4.2 配置 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.qf.controller"></context:component-scan>

        <!-- 配置组件 -->
        <mvc:annotation-driven></mvc:annotation-driven>

        <!-- 放行静态资源 -->
        <mvc:default-servlet-handler></mvc:default-servlet-handler>

</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>

  <!-- 加载applicationContext.xml -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--    <param-value>classpath:applicationContext.xml</param-value>-->
    <!-- 当有多个applicationContext-*.xml配置文件时,进行以下配置 -->
    <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>
4.4 编写 Controller
package com.qf.controller;

import com.qf.pojo.Music;
import com.qf.service.MusicService;
import com.qf.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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("findById/{musicId}")
    public Music findById(@PathVariable("musicId") Integer id){
        return musicService.findById(id);
    }

}
4.5 测试

依次 Install ssm-wyy-music-dao,ssm-wyy-music-service (或者直接install父工程),然后启动 ssm-wyy-music-web 进行测试,当修改dao和service中的代码时,需要再次install

posted @ 2022-07-10 18:36  qtyanan  阅读(18)  评论(0编辑  收藏  举报