欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven高级SSM项目多模块开发(开发版)

程序员文章站 2022-06-05 18:09:30
...

此指南详细的介绍了在Maven工程里如何进行SSM工程项目的多模块开发;本指南里面包含:“聚合”,“继承”,“属性”,“资源配置”,"多环境开发"等众多知识点,目的是快速构建一个SSM多模块工程。

1、项目架构搭建

1、新建一个maven普通项目工程,只留下pom.xml文件(作为管理子模块的父工程文件)。
2、新建三个maven普通模块工程,分别取名为pojo,dao,service(模块路径在项目工程路径之下)
3、新建一个maven-Web模块工程,取名为controller(模块路径在项目工程路径之下)

Maven高级SSM项目多模块开发(开发版)

2、模块工程构建

2.1、实体类模块

实体类模块主要是根据需求编写POJO;需要注意的是“数据库表间关系”,以便于ORM映射。

//pojo类
public class Student {
    private Integer id; 
    private String name;
    private Integer age;
}
//通用返回结果类
public class GeneralResult<T> {
    private T data;
    private Boolean flag;
    private String msg;
    private Integer code;
}
<?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>
        <groupId>com.mine</groupId>
        <artifactId>ssm</artifactId>
        <version>1.0-SNAPSHOT</version>
        <!--父工程的pom文件-->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <!--
    ssm_pojo工程:
    groupId同父工程;version同父工程;
    -->
    <artifactId>ssm_pojo</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

2.2、持久层模块

编写实体接口,映射文件,jdbc.properties,applicationContext-dao.xml文件1

2.2.1、实体接口

@Repository
public interface StudentDao {
    List<Student> findAll();

    int saveStudent(Student student);

    int updateStudent(Student student);

    int deleteById(String id);

    Student findById(String id);
}

2.2.2、映射文件

<?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.mine.dao.StudentDao">
    <!--查询所有-->
    <select id="findAll" resultType="com.mine.domain.Student">
        select * from student
    </select>

    <!--查询单个-->
    <select id="findById" resultType="com.mine.domain.Student">
        select * from student where id=#{id}
    </select>

    <!--保存-->
    <insert id="saveStudent" parameterType="com.mine.domain.Student">
      insert into student (name,age)values (#{name},#{age})
    </insert>

    <!--更新-->
    <update id="updateStudent" parameterType="com.mine.domain.Student">
        update student set name=#{name},age=#{age} where id=#{id}
    </update>

    <!--删除-->
    <delete id="deleteById" parameterType="java.lang.String">
        delete from student where id=#{id}
    </delete>
</mapper>

2.2.3、properties文件

jdbc.driver=${jdbc.driver}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}

2.2.4、applicationContex-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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--开启目录扫描-->
    <context:component-scan base-package="com.mine"/>

    <!--加载perperties配置文件的信息,文件名写死,多文件加载只能单独写出-->
    <context:property-placeholder location="classpath*:jdbc.properties"/>

    <!--加载druid资源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--spring整合mybatis后控制的创建连接用的对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.mine.domain"/>
        <!--分页插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mine.dao"/>
    </bean>
</beans>

2.3、业务层模块

业务接口,业务接口实现类,applicationContext-service.xml文件,测试用例编写

2.3.1、业务接口方法

@Transactional(readOnly = true)
public interface StudentService {
    List<Student> findAll();
 
    @Transactional(readOnly = false)
    int saveStudent(Student student);

    @Transactional(readOnly = false)
    int updateStudent(Student student);

    @Transactional(readOnly = false)
    int deleteById(String id);
 
    Student findById(String id);

    PageInfo findPage(int page, int size);
}

2.3.2、业务接口实现类

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    @Transactional//事务管理注解
    public List<Student> findAll() {
        return studentDao.findAll();
    }

    @Override
    public int saveStudent(Student student) {
        return studentDao.saveStudent(student);
    }

    @Override
    public int updateStudent(Student student) {
        return studentDao.updateStudent(student);
    }

    @Override
    public int deleteById(String id) {
        return studentDao.deleteById(id);
    }

    @Override
    public Student findById(String id) {
        return studentDao.findById(id);

    }

    @Override
    public PageInfo findPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Student> students = studentDao.findAll();
        PageInfo pageInfo = new PageInfo(students);
        return pageInfo;
    }
}

2.3.3、applicationContext-service.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:tx="http://www.springframework.org/schema/tx"
       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/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">

    <!--开启bean注解扫描-->
    <context:component-scan base-package="com.mine"/>

    <!--开启注解式事务-->
    <tx:annotation-driven transaction-manager="txManager"/>

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

2.3.4、测试用例编写

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-service.xml", "classpath:applicationContext-dao.xml"})
public class StudentServiceTest {

    @Autowired
    private StudentService studentService;

    @Test
    public void testFindAll() {
        List<Student> all = studentService.findAll();
        System.out.println(all);
    }
}

2.4、表现层模块

处理器方法,spring-mvc文件,web.xml

2.4.1、处理器方法

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<Student> findAll() {

        return studentService.findAll();
    }

    @RequestMapping("saveStudent")
    @ResponseBody
    public GeneralResult saveStudent(@RequestBody Student student) {
        studentService.saveStudent(student);
        GeneralResult generalResult = new GeneralResult();
        generalResult.setFlag(true);
        generalResult.setMsg("保存成功");
        return generalResult;
    }

    @RequestMapping("updateStudent")
    @ResponseBody
    public GeneralResult updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        GeneralResult generalResult = new GeneralResult();
        generalResult.setFlag(true);
        generalResult.setMsg("更新成功");
        return generalResult;
    }

    @RequestMapping("deleteStudent")
    @ResponseBody
    public GeneralResult delteById(String id) {
        int i = studentService.deleteById(id);
        GeneralResult generalResult = new GeneralResult();
        generalResult.setFlag(true);
        generalResult.setMsg("删除成功");
        generalResult.setData(i);
        return generalResult;
    }

    @RequestMapping("findStudent")
    @ResponseBody
    public GeneralResult findById(String id) {
        Student student = studentService.findById(id);
        GeneralResult<Student> result = new GeneralResult<>();
        result.setData(student);
        result.setFlag(true);
        return result;
    }

    @RequestMapping("findPage")
    @ResponseBody
    public GeneralResult findPage() {
        PageInfo page = studentService.findPage(1, 2);
        GeneralResult generalResult = new GeneralResult();
        generalResult.setData(page);
        generalResult.setFlag(true);
        return generalResult;
    }
}

2.4.2、spring-mvc文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">
    <!--开启注解驱动-->
    <mvc:annotation-driven/>
    <!--开启目录扫描-->
    <context:component-scan base-package="com.mine.controller"/>
    <!--放开静态资源-->
    <mvc:default-servlet-handler />

</beans>

2.4.3、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--监听器,加载spring核心配置文件-->
    <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>

    <!--过滤器,中文乱码处理-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--*处理器,加载spring-mvc核心配置文件-->
    <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*:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

3、模块工程管理

通过项目工程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.mine</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--聚合:模块依赖关系管理-->
    <packaging>pom</packaging>


    <!--模块管理-->
    <modules>
        <!--模块名称-->
        <module>ssm_pojo</module>
        <module>ssm_dao</module>
        <module>ssm_service</module>
        <module>ssm_controller</module>
    </modules>

    <!--多环境开发管理-->
    <profiles>
        <!--生产环境-->
        <profile>
            <id>prod</id>
            <!--生产环境专用属性值-->
            <properties>
                <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
                <jdbc.url>jdbc:mysql://localhost:3306/db001</jdbc.url>
                <jdbc.username>root</jdbc.username>
                <jdbc.password>root</jdbc.password>
            </properties>
            <!--设置默认启动-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!--开发环境-->
        <profile>
            <id>dept</id>
            <properties>
                <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
                <jdbc.url>jdbc:mysql://localhost:3306/db002</jdbc.url>
                <jdbc.username>root</jdbc.username>
                <jdbc.password>root</jdbc.password>
            </properties>
            <!--设置默认启动-->
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

    <!--模块依赖管理-->
    <dependencyManagement>
        <dependencies>
            <!--1.自有模块管理-->
            <!--导入资源文件POJO-->
            <dependency>
                <groupId>com.mine</groupId>
                <artifactId>ssm_pojo</artifactId>
                <version>${project.version}</version>
            </dependency>

            <!--导入资源文件dao-->
            <dependency>
                <groupId>com.mine</groupId>
                <artifactId>ssm_dao</artifactId>
                <version>${project.version}</version>
            </dependency>

            <!--导入资源文件ssm_service-->
            <dependency>
                <groupId>com.mine</groupId>
                <artifactId>service</artifactId>
                <version>${project.version}</version>
            </dependency>

            <!--2.第三依赖管理-->
            <!--spring环境-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>

            <!--mybatis环境-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.3</version>
            </dependency>

            <!--mysql环境-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>

            <!--druid连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.21</version>
            </dependency>

            <!--spring整合jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>


            <!--spring整合mybatis-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.3</version>
            </dependency>

            <!--分页插件-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>

            <!--junit单元测试-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>

            <!--spring整合junit-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>

            <!--springmvc环境-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>

            <!--jackson相关坐标3个-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.0</version>
            </dependency>

            <!--servlet环境-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--构件管理-->
    <build>
        <!--插件管理-->
        <pluginManagement>
            <plugins>
                <!--单元测试-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <skipTests>false</skipTests>
                    </configuration>
                </plugin>

                <!--tomcat7-->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <port>80</port>
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

      <resources>
          <resource>
              <directory>${project.basedir}/src/main/resources</directory>
              <filtering>true</filtering>
          </resource>
      </resources>
    </build>
</project>
相关标签: Spring spring