IDEA多模块SSM框架搭建(IDEA+tomcat+maven+mysql+spring+spring mvc+mybatis)总结

Tomcat版本:8.5.27

 jdk版本:1.8

Idea版本:2017.3.5

1.创建一个根模块

不勾选任何值,直接next


    

根模块创建成功如下:

src目录不需要可以删除

2.创建javadao模块


src目录不需要可以删除

3.与上步相同再创建两个模块javaservice javaweb,最终目录为

4.点击右上角搜索按钮左边的按钮

添加一个web模块

Facets->web->javaweb->OK

添加一个war包

Artiffacts->web Application:EXploded->From Modules->javaweb->OK


可以看到javaweb模块下多了一个web目录

5.向根模块下的pom文件中添加依赖

<?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>cn.edu.zut.ssm</groupId>
    <artifactId>JavaSSM</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>javadao</module>
        <module>javaservice</module>
        <module>javaweb</module>
    </modules>
    <!--项目统一字符集编码-->
    <properties>
        <!--添加依赖-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.0.6.RELEASE</spring.version>
        <mysql.connector.version>5.1.30</mysql.connector.version>
        <mybatis.version>3.2.7</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <log4j.version>1.2.12</log4j.version>
        <jstl.version>1.2</jstl.version>
        <aspectj.version>1.6.12</aspectj.version>
        <commons-dbcp.version>1.4</commons-dbcp.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Bean注入 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 包扫描 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 切面编程 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 事务管理 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- ORM 框架 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 加入spring测试依赖包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <!-- mybatis包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- spring mybatis整合包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <!-- mysql-connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${commons-dbcp.version}</version>
        </dependency>
        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <!-- servlet api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>
Javaservice模块下pom文件配置如下
                        <?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>JavaSSM</artifactId>
        <groupId>cn.edu.zut.ssm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>javaservice</artifactId>
    <packaging>jar</packaging>
    <dependencies>
        <!--添加对dao层模块的依赖-->
         <dependency>
            <groupId>cn.edu.zut.ssm</groupId>
            <artifactId>javadao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--单元测试-->
         <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

javadao层模块配置如下

<?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>JavaSSM</artifactId>
        <groupId>cn.edu.zut.ssm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>javadao</artifactId>
    <packaging>jar</packaging>

</project>

javaweb层pom文件配置

<?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>JavaSSM</artifactId>
        <groupId>cn.edu.zut.ssm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>javaweb</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <!--对dao层的依赖-->
        <dependency>
            <groupId>cn.edu.zut.ssm</groupId>
            <artifactId>javadao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--对service层的依赖-->
        <dependency>
            <groupId>cn.edu.zut.ssm</groupId>
            <artifactId>javaservice</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

6.在dao层模块中创建如下文件


数据库的连接

view->tool windows->database->加号->data Source->mysql

填写Database:数据库名

root:用户名

password:密码


1.创建与数据库表对应的实体类

dome.java

package com.smart.pojo;

public class Dome {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "dome{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2.创建接口

DomeMapper.java

接口创建了两个方法名

用于插入和查询

package com.smart.dao;

import com.smart.pojo.Dome;

import java.util.List;

public interface DomeMapper {
    List<Dome> selectAll();
    int insertdome(Dome dome);
}

3.创建映射文件

DomeMapper.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.smart.dao.DomeMapper">
    <resultMap id="BaseResultMap" type="com.smart.pojo.Dome">
        <id column="t_username" property="userName" jdbcType="VARCHAR" />
        <result column="t_password" property="password" jdbcType="VARCHAR" />
    </resultMap>
    <select id="selectAll" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select * FROM t_dome
    </select>
    <insert id="insertdome" parameterType="com.smart.pojo.Dome">
        insert into t_dome(t_username, t_password) VALUES (#{userName,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})
    </insert>
</mapper>

4.创建mybatis-config.xml配置文件

里面可以不写内容,但一定要有。

mybatis与spring整合后配置信息全都写在了spring的配置文件中

mybatis配置文件只做一些别名配置

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

</configuration>

7.创建Spring-mybatis.xml文件配置

   1.在service层新建一个spring配置文件

spring-mybatis.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:component-scan base-package="com.smart.service"/>
    <!-- dbcp数据源 -->
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="com.mysql.jdbc.Driver"
          p:url="jdbc:mysql://localhost:3306/你的数据库名"
          p:username="用户名"
          p:password="密码"
          p:maxActive="20"
          p:initialSize="1"
          p:maxWait="60000"
          p:maxOpenPreparedStatements="20"
          p:minIdle="3"
          p:removeAbandoned="true"
          p:removeAbandonedTimeout="180"
          p:connectionProperties="clientEncoding=UTF-8"/>
    <!-- mybatis的配置文件 -->
    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configLocation="classpath:mybatis-config.xml"
          p:mapperLocations="classpath*:mapper/*Mapper.xml"/>
    <!-- spring与mybatis整合配置,扫描所有dao 和所有mapper文件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
          p:basePackage="com.smart.dao"
          p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource" />
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod"
                      expression="(execution(* com.smart.service..*(..))) and (@annotation(org.springframework.transaction.annotation.Transactional))" />
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
</beans>

8.对dao层进行测试

在service 层下的Test文件夹下创建DomeTest.java

package com.smart.test;

import com.smart.dao.DomeMapper;
import com.smart.pojo.Dome;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
public class DometTest {
    @Autowired
    private DomeMapper domeMapper;
    @Test
    public void InsertDomeTest(){
        Dome dome=new Dome();
        dome.setUserName("Test");
        dome.setPassword("123456");
       System.out.println(domeMapper.insertdome(dome));
    }
    @Test
    public void SelectAllTest(){
        List list=domeMapper.selectAll();
        for(Object dome:list){
            System.out.println(dome.toString());
        }
    }
}

1.首先测试插入操作,运行成功后查看数据库中是否新增数据

然后测试查询方法

如下结果即成功

9.web层配置

文件目录如下


1.在resources下创建Spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--扫描web包应用Spring注解-->
    <context:component-scan base-package="com.smart.web"/>
    <!--启动注解-->
    <mvc:annotation-driven/>
    <!--Spring3.1开始的注解 HandlerMapping --> <!--3.1之后必须存在, 不解-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="2"/>
    </bean>
</beans>
web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <welcome-file-list>
        <welcome-file>jsp/index.jsp</welcome-file>
    </welcome-file-list>

    <!--对业务层的依赖-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml
        </param-value>
    </context-param>
    <!-- 著名 Character Encoding filter -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!--配置Servlet-->
    <servlet>
        <servlet-name>smart</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>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>smart</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

1.sevice层java目录下创建DomeService.java

代码如下

package com.smart.service;

import com.smart.dao.DomeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DomeService {
    @Autowired
    private DomeMapper domeMapper;
    public List index(){
        return domeMapper.selectAll();
    }
}

2.在web包下创建DomeController.java

package com.smart.web;

import com.smart.service.DomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class DomeController {
    @Autowired
    private DomeService domeService;
    @RequestMapping(value ="/index.html")
    public ModelAndView index(){
        List list=domeService.index();
        ModelAndView mav =new ModelAndView();
        mav.setViewName("index");
        mav.addObject("userList",list);
        return mav;
    }
}

3.jsp文件夹下创建index.jsp文件

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/5/15 0015
  Time: 22:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>Hello</h1>
    <a href="index.html">获取表格数据</a><br>
    <table>
        <tread>
            <tr style="color: red">
                <td>用户名</td>
                <td>密码</td>
            </tr>
        </tread>
        <tbody>
        <c:forEach items="${userList}" var="user">
            <tr class="table-body">
                <td>${user.userName}</td>
                <td>${user.password}</td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</body>
</html>

10.将配置文件部署到项目中

11.配置Tomcat

12.测试运行

点击获取表格数据出现如下信息即成功


阅读更多
个人分类: java
想对作者说点什么? 我来说一句

IDEA下搭建maven项目(SSM框架)

2017年12月11日 13.19MB 下载

没有更多推荐了,返回首页

不良信息举报

IDEA多模块SSM框架搭建(IDEA+tomcat+maven+mysql+spring+spring mvc+mybatis)总结

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭