IDEA使用Maven搭建SSM框架

2019-03-13 23:34:43来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

搭建环境:Intellij IDEA 2017

JDK 1.8

Tomcat 8.5

MySQL 5.7

Spring 4.x

Mybatis 3.x

 

这个过程确实太麻烦了,我用了两个小时

所以建议用SpringBoot,简单很多很多

 

第一步:创建项目

创建一个空项目

 

第二步:创建父工程

File-->New-->Module

选中Maven,不勾选Create from Archetype,直接Next

输入组ID:通常是com.xxx.项目名

输入ArtifactID:common-parent(父工程)

点击Next,这时候刚才写的-被去掉了,可以自己加上

Finish完成

接下来会跳出来一个对话框:Non Project File Protection

点击OK

 

pom.xml必须要加入打包方式: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>org.dreamtech.edu</groupId>
    <artifactId>common-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--父工程-->
    <packaging>pom</packaging>
</project>
View Code

由于父工程不是用来写代码的,把src目录删除了

 

第三步:配置IDEA的Maven

File-->Setttings

配置自己的Maven目录和配置文件

当然,也可以采用Bundle的Maven,但是必须要修改配置文件,因为默认目录没有settings.xml

 

第四步:创建子工程

选中common-parent父工程,右键New-->Mudule

 

首先创建的是Model模块:

具体的选项我截图了:

 

点击Next:

输入ArtifacID:xxx-model

Next-->Next

注意要把子工程放在父工程的下面,要不然无法成功创建

点击Finish即可

 

然后Maven会自动下载一些东西,我们等待它完成,最多三分钟时间

打开子工程,我们在src-->main-->java目录下写Java代码

我简单地创建一个User:

package org.dreamtech.edu.model;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
View Code

同目录下有一个App.java文件是自动生产的,没有什么用,删除了就行

 

然后用同样的方式创建子工程:Service层DAO层

命名方式通常是:xxx-service、xxx-dao

 

接下来创建Web层,需要选择的就不是quickstart了

 

然后创建API层,也就是后台提供的接口:和上边Web层一样

 

第五步:具体配置

子工程已经创建完成了,下面是具体的配置

首先,model层、service层、dao层需要在pom.xml配置packging

    <packaging>jar</packaging>

 

然后在web层和api层需要配置成war,但是已经默认配置好了,所以不需要自行配置

 

在父工程中配置公共的依赖:

删除在每一个子工程中创建的依赖,找到每个pom.xml,剪切这一部分,复制到父工程的pom.xml中:

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
View Code

 

在父工程的pom.xml中可以进行版本号的配置:

    <!--公共版本号管理-->
    <properties>
        <junit.version>4.11</junit.version>
        <spring.version>4.3.14.RELEASE</spring.version>
        <mybatis.version>3.4.1</mybatis.version>
        <mybatis-spring.version>1.3.1</mybatis-spring.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.12</slf4j.version>
        <jstl.version>1.2</jstl.version>
        <taglibs.version>1.1.2</taglibs.version>
        <c3p0.version>0.9.5.2</c3p0.version>
        <mysql.version>5.1.35</mysql.version>
    </properties>
    <!--公共依赖-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
View Code

 

Web层的配置:

pom.xml进行SpringMVC的配置

    <!--添加依赖-->
    <dependencies>
        <!--SpringMVC-->
        <!--aop-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--aspects-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--beans-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--expression-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--orm-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--tx-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--webmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
View Code

注意:IDEA右下角有一个提示,点击允许Maven自动导包

 

配置Tomcat:

右上角启动箭头左边有个下拉框,点击编辑配置:

 

先导入需要运行的工程,点击绿色加号部署

这里先部署web层的war exploded,因为api部分还没有完成

 

然后配置热更新:

 

然后就可以点击绿色的开始箭头开启Tomcat了

如果出现了下面这种情况,说明,之前的配置全都正确了!

 

 

SpringMVC的配置文件:

说到配置文件,那么它们应该放在哪里呢?

打开web层子工程,然后在main下新建java目录,右键Mark Dir As-->Sources Root

java文件夹就是我们放java源码的地方

再新建一个目录,resources作为存放配置文件的地方

右键resources,mark dir as-->resources root

 

如果都配置正确,目录结构应该是下面这样:

 

然后我们看看具体的SpringMVC配置文件:

写一个最简单的Controller(在刚才新建的java文件夹中):

package org.dreamtech.edu.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping("login")
    public String login(){
        System.out.println("Hello");
        return null;
    }
}
View Code

 

配置web.xml:这里需要修改约束,自动生成的版本太低

<?xml version="1.0" encoding="UTF-8"?>
<web-app
        version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>Archetype Created Web Application</display-name>
  <!-- 配置SpringMVC-->
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
View Code

 

然后配置SpringMVC:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 1.注解扫描位置-->
    <context:component-scan base-package="org.dreamtech.edu.web.controller"/>

    <!-- 2.配置映射处理和适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <!-- 3.视图的解析器-->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/Modules/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
View Code

 

跑起来项目:

访问http://localhost:8080/user/login.do

发现在控制台打印了hello,配置成功

 

其实SpringMVC的配置还没有结束

但我们先来看DAO层的配置

 

DAO层的配置:

pom.xml配置

首先需要依赖model层

    <dependencies>
        <dependency>
            <groupId>org.dreamtech.edu</groupId>
            <artifactId>edu-model</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
View Code

依赖Mybatis包:

        <!-- 依赖mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
View Code

 

写入一些简单的代码:

抽取Mapper:

package org.dreamtech.edu.mapper.base;

public interface BaseMapper<T> {
    public T findById(Integer id);

    public T findByUUID(String uuid);

    public void deleteById(Integer id);

    public void deleteByUUID(String uuid);

    public void update(T t);

    public void insert(T t);
}
View Code

继承:

package org.dreamtech.edu.mapper;

import org.dreamtech.edu.mapper.base.BaseMapper;
import org.dreamtech.edu.model.User;

public interface UserMapper extends BaseMapper<User> {

}
View Code

UserMapper.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="org.dreamtech.edu.mapper.UserMapper">
    <select id="findById" resultType="user" parameterType="int">
        SELECT * FROM t_user WHERE id = #{id}
    </select>
</mapper>
View Code

 

DAO层就先配到这里

 

Service层的配置:

依赖DAO层

    <dependencies>
        <dependency>
            <artifactId>edu-dao</artifactId>
            <groupId>org.dreamtech.edu</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
View Code

依赖Spring:

        <!-- 添加spring依赖 -->
        <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-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
View Code

 

Service层简单代码:

package org.dreamtech.edu.service.base;

public interface IBaseService<T> {
    public T findById(Integer id);

    public T findByUUID(String uuid);

    public void deleteById(Integer id);

    public void deleteByUUID(String uuid);

    public void update(T t);

    public void insert(T t);
}
View Code
package org.dreamtech.edu.service.base;

import org.dreamtech.edu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    protected UserMapper userMapper;
}
View Code
package org.dreamtech.edu.service;

import org.dreamtech.edu.model.User;
import org.dreamtech.edu.service.base.IBaseService;

public interface IUserService extends IBaseService<User>{

    public User login(String username,String password);
}
View Code
package org.dreamtech.edu.service.impl;

import org.dreamtech.edu.model.User;
import org.dreamtech.edu.service.IUserService;
import org.dreamtech.edu.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService{

    @Override
    public User login(String username, String password) {
        return null;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public User findByUUID(String uuid) {
        return null;
    }

    @Override
    public void deleteById(Integer id) {

    }

    @Override
    public void deleteByUUID(String uuid) {

    }

    @Override
    public void update(User user) {

    }

    @Override
    public void insert(User user) {

    }
}
View Code

 

这么多行的代码,其实只实现了一个功能,就是根据ID查找用户

 

继续配置Web层:

添加Service依赖:

        <dependency>
            <artifactId>edu-service</artifactId>
            <groupId>org.dreamtech.edu</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
View Code

导入需要的其他依赖:

       <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--数据连接池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>

        <!--其他-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>${taglibs.version}</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!--导入servlet-api/jsp-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
View Code

数据库建一张简单的表,在后面测试:

CREATE TABLE `t_user` (
  `id` int(32) NOT NULL,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
View Code

添加其他的配置文件,放在resources文件夹

Spring配置文件:applicationContext.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: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-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!-- 配置sessionFactory -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--配置别名-->
        <property name="typeAliasesPackage" value="org.dreamtech.edu.model"/>
        <!--配置加载映射文件 UserMapper.xml-->
        <property name="mapperLocations" value="classpath:org/dreamtech/edu/mapper/*Mapper.xml"/>
    </bean>
    <!-- 自动生成dao,mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.dreamtech.edu.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    </bean>
    <!--自动扫描Service-->
    <context:component-scan base-package="org.dreamtech.edu"/>
    <!-- 配置事务-->
    <!-- 5.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 6.开启事务注解-->
    <tx:annotation-driven/>
</beans>
View Code

properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/edu
jdbc.username=root
jdbc.password=xuyiqing
View Code
# 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
View Code

web.xml加入Spring配置:

  <!-- 配置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>
View Code

UserController:

package org.dreamtech.edu.web.controller;

import org.dreamtech.edu.model.User;
import org.dreamtech.edu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public String login(){
        System.out.println("Hello");
        return null;
    }

    @RequestMapping("find")
    public String find(Integer id){
        User user = userService.findById(id);
        return null;
    }
}
View Code

 

现在就可以启动,访问/user/login.do是没问题的

但是如果访问/user/find会报错

 

这是因为在DAO层:UserMapper.xml在打包的时候不存在

解决:在DAO层的pom.xml的build下面配置:

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
View Code

 

运行!

http://localhost:8080/user/find.do?id=1

不报错,然后我们在数据库随便添加一条信息,在controller层输出

到这里,其实搭建就结束了!

 

但是还有一步:

页面整合:

页面应该放在哪里呢?

放在WEB-INF目录中:

JS、CSS、图片等静态资源放在WEB-INF同级下,页面放在WEB-INF中

可以新建一个目录,放入页面,再把文件夹放到WEB-INF

其实之前SpringMVC就是这么配的

    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/Modules/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
View Code

在Controller中只需要返回JSP的名称字符串就可以了:

    @RequestMapping("login")
    public String login(){
        System.out.println("Hello");
        return "Default";
    }
View Code

 


原文链接:https://www.cnblogs.com/xuyiqing/p/10522339.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java基础:(六)关键字

下一篇:Kafka查看topic、consumer group状态命令