IDEA整合 ssm的详细demo(使用maven)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40693171/article/details/84640687

把我idea的模板拿出来,希望能够帮到大家初学者,从myeclipse迁移到idea是以把心酸泪,我把以前的demo拿出来。

  1. 创建maven项目,切记别选错
    在这里插入图片描述
  2. 到这项的时候,如果创建maven项目不全的话,一定要加上最后的archetypeCatalog=internal.
    在这里插入图片描述
  3. 这样项目就创建好,下面添加tomcat,配置的话就不多说了。记住将项目添加进去,还有路径配置(这个配置资料比较多)
    在这里插入图片描述
    添加tomcat
    在这里插入图片描述
  4. 紧接着先创建对应文件夹,使其目录大致与相似一致(大文件夹一致)创建完文件夹要声明作用。
    在这里插入图片描述
  5. 创建完文件夹声明各个文件夹的属性以及用途(和myeclipse有点不一样,因为myeclipse帮你自动配置好了,idea需要自己配置一下)。
    在这里插入图片描述
    结果这样就行:
    在这里插入图片描述
  6. 接着开始配置maven环境。里面包含几乎初学者的所有jar包。ssm的初始配置应该是够的,我刚开始找maven完整版也找了很久,自己也凑了一点。我的maven配置为:
<?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>date</groupId>
  <artifactId>date</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

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

  <properties>
    <!-- java ee jar包 -->
    <javaee.version>7.0</javaee.version>
    <!-- jstl jar包 -->
    <jstl.version>1.2</jstl.version>
    <!-- mysql-connector包 -->
    <mysql.version>5.1.46</mysql.version>
    <!-- commons-dbcp -->
    <dbcp.version>1.4</dbcp.version>
    <!-- mybatis-spring整合包 -->
    <mybatis-spring.version>1.3.0</mybatis-spring.version>
    <!-- spring版本号 -->
    <spring.version>4.2.5.RELEASE</spring.version>
    <!-- mybatis版本号 -->
    <mybatis.version>3.4.0</mybatis.version>
    <!-- jackson版本号 -->
    <jackson.verson>2.8.9</jackson.verson>
    <!-- junit -->
    <junit-version>4.12</junit-version>
    <!-- cglib -->
    <cglib-version>3.2.2</cglib-version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.7.21</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
  </properties>

  <dependencies>
    <!-- 导入java ee jar 包 -->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>${javaee.version}</version>
    </dependency>
    <!-- jstl包 -->
    <!-- JSTL标签类 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>
    <!-- 导入Mysql数据库链接jar包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <!-- dbcp数据库连接池包 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>${dbcp.version}</version>
    </dependency>
    <!--mybatis-spring整合包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis-spring.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.7</version>
    </dependency>

    <!-- mybatis-核心包 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>


    <!--spring-核心包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</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-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--<dependency>-->
    <!--<groupId>org.springframework</groupId>-->
    <!--<artifactId>spring-context-supportmvn </artifactId>-->
    <!--<version>${spring.version}</version>-->
    <!--</dependency>-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- jackson包 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.verson}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.verson}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.verson}</version>
    </dependency>

    <!-- 测试包 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <!-- cglib包 -->
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>${cglib-version}</version>
    </dependency>
    <!--日志记录的jar包 -->
    <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>
    <!-- aspectjweaver包 -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.10</version>
    </dependency>
    <!-- aspectjrt 包-->
    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.5.3</version>
    </dependency>
  </dependencies>

    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <!--<dependency>-->
    <!--<groupId>org.mybatis.generator</groupId>-->
    <!--<artifactId>mybatis-generator-core</artifactId>-->
    <!--<version>1.3.6</version>-->
    <!--</dependency>-->

  <build>
    <finalName>date</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

  1. 紧接着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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <filter>
  <filter-name>cors2</filter-name>
  <filter-class>com.date.fitter.filter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>cors2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:date-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

  1. 接着在resource目录下的xml配置如下:
  • application.xml spring核心配置
<?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:p="http://www.springframework.org/schema/p"
	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-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
 
	<!-- 1. 数据源 : DriverManagerDataSource -->
	<bean id="dataSource"
		  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://127.0.0.1/db?useSSL=false" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>
 
	<!--
		2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
 
		MyBatis定义数据源,同意加载配置
	-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-conf.xml" />
		<property name="mapperLocations" value="classpath:mapping/*.xml"></property>
	</bean>
 
	<!--
		3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
 
		basePackage:指定sql映射文件/接口所在的包(自动扫描)
	-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.date.dao"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
<!-- 	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> -->
<!--         注入sqlSessionFactory -->
<!--         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<!--         给出需要扫描Dao接口包 -->
<!--         <property name="basePackage" value="com.ssm.dao" /> -->
<!--     </bean> -->

 
 
	<!--
		4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
	-->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
 
	<!-- 5. 使用声明式事务
		 transaction-manager:引用上面定义的事务管理器
	 -->
	<tx:annotation-driven transaction-manager="txManager" />
 
</beans>


  • date-servlet.xml配置 springmvc的配置,名称具体参考自己的,包括映射jsp等等
<?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.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-3.0.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <!-- 简化配置: 
        (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 
        (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 
    -->
    <mvc:annotation-driven />

    <!-- 2.静态资源默认servlet配置
        (1)加入对静态资源的处理:js,gif,png
        (2)允许使用"/"做整体映射
     -->
     <mvc:default-servlet-handler/>

     <!-- 3.配置jsp 显示ViewResolver -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
     </bean>

     <!-- 4.扫描web相关的bean -->
     <context:component-scan base-package="com.date.controller" />
</beans>

  • mybatis-conf.xml配置: 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>
 
	<!-- 实体类,简称 -设置别名 -->
	<typeAliases>
		<typeAlias alias="sheng" type="com.date.bean.sheng" />
	</typeAliases>
	<!-- 实体接口映射资源 -->
	<!--
		说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
	-->
<!-- 	<mappers> -->
<!-- 		<mapper resource="com/ssm/dao/citylistMapper.xml" /> -->
<!-- 		<mapper resource="com/ssm/dao/shengMapper.xml" /> -->
<!-- 	</mappers> -->
 
</configuration>  

  • 还剩log4j.properties:这是输出日志的配置
# 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

上面就是基本的配置,下面我把我简单的结合的demo展示出来控制层controller,控制器是springmvc的主要部分,里面主要关于url请求地址的配置,以及处理。和servlet很像。它是网站和服务器通信的枢纽。

  1. controller:
    在这里插入图片描述
  2. dao层叫数据访问层。其实就是声明一个接口,这个接口有很多方法集合,关于数据的增删该查,返回的类型可以为自定义map,list,或者bean对象等等。
    dao层为:
    在这里插入图片描述
  3. mapper,mapper就是dao的方法具体实现,在xml中写sql,这是mybatis部分。在myeclipse中我的dao和mapper是放到一块的,但是idea的maven编译方式和myeclipse不同,如果写在一块需要配置,可以直接在resources中新建mapping文件夹放到里面。这样就能够被编译
    在这里插入图片描述
  4. bean层,至于bean层就不多说了,就是对象,要和数据库对象的字段一一对应。如果不一致则需要自定义返回类型在其中配置。
    另外,还有一些遇到过的小问题,比如没有找打监听器报错,
    在这里插入图片描述
    还有就是一些jar包版本可能会冲突,比如我之前用的mysql就出错了。后来换了版本。注意要在dependece中先将jar包移除,然后maven clean jar包,不然可能会出错。还有就是刚开始配置可能有的xml或者class没找到,可以在target文件下查看classes那些东西已经编译
    在这里插入图片描述
    剩下的如果以后遇到会补充,上面有些文件名可能写blog时候会打错,各位具体名称还请对照自己的名称。另外,如果有错误还请大神指出。

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