IDEA 整合SSM入门

IDEA整合SSM,有很多坑,这里记录一下,新建项目采用的普通Java Enterprise项目,不是Maven项目。

创建SSM项目前期搭建

(1)先创建一个Java Enterprise项目,一般是选择Spring来创建,并且会自动导包,这里使用另外一种方式来实现。

(2)创建完成后需要设置,进入Project Structure界面,跟其他IDE可以直接使用不同,IDEA的还需要设置source目录和resource目录,代表代码和资源文件存放的地方。如图所示在src目录下建立main目录,main目录下建立java和resource目录,分别存放量产代码和配置文件,test文件夹也可以设置,这里省略了。上面Mark as标签可以看出来,可以分别设置量产代码文件夹(Sources)、测试代码文件夹(Tests),量产资源文件夹(Resources)、测试资源文件夹(Test Resources)和排它文件夹(不被使用)。

(3)设置Tomcat部署路径,这里选择部署在Tomcat的Webapp下。

(4)设置Tomcat服务器,选择菜单Edit Configurations进入设置界面,可以将After lauch勾选掉,另外更新和鼠标离焦选项选择Update classes and resources,这样就支持热部署,即无需重启tomcat也可以支持更新。

另外切换到Deployment,将Application Context修改,这样访问资源是通过如下web虚拟路径名可以访问到部署到tomcat后的真正资源。

以上就完成前期的基本配置,后面需要配置SpringMVC、Spring和MyBatis的核心配置文件了。

SSM项目核心文件配置

SpringMVC配置

(1)web.xml中配置启动前端控制器,需要读取SpringMVC核心配置文件。注意spring-mvc.xml是放在前面的resource文件夹下。

 1 <!--配置SpringMVC前端控制器-->
 2     <servlet>
 3         <servlet-name>springmvc</servlet-name>
 4         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5         <init-param>
 6             <param-name>contextConfigLocation</param-name>
 7             <param-value>classpath:spring-mvc.xml</param-value>
 8         </init-param>
 9         <!--设置启动tomcat就启动-->
10         <load-on-startup>1</load-on-startup>
11     </servlet>
12     <servlet-mapping>
13         <servlet-name>springmvc</servlet-name>
14         <url-pattern>*.action</url-pattern>
15     </servlet-mapping>
16
17     <!--配置全站式乱码解决,只针对post有效-->
18     <filter>
19         <filter-name>filtercoding</filter-name>
20         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
21         <init-param>
22             <param-name>encoding</param-name>
23             <param-value>utf-8</param-value>
24         </init-param>
25     </filter>
26     <filter-mapping>
27         <filter-name>filtercoding</filter-name>
28         <url-pattern>*.action</url-pattern>
29     </filter-mapping>

(2)配置SpringMVC核心配置文件,这里需要配置前端控制器、需要配置包扫描、mvc注解驱动和视图解析器,通过采坑后发现,后面配置Spring也需要扫描包,如果都配置可能会导致事务失效,这里因为没有配置事务因此将不需要事务的包在这里扫描。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6        xmlns:util="http://www.springframework.org/schema/util"
 7        xmlns:aop="http://www.springframework.org/schema/aop"
 8        xmlns:tx="http://www.springframework.org/schema/tx"
 9        xmlns:mvc="http://www.springframework.org/schema/mvc"
10        xsi:schemaLocation=
11                "http://www.springframework.org/schema/beans
12                http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
13                http://www.springframework.org/schema/context
14                http://www.springframework.org/schema/context/spring-context-4.3.xsd
15                http://www.springframework.org/schema/util
16                http://www.springframework.org/schema/util/spring-util-4.3.xsd
17                http://www.springframework.org/schema/aop
18                http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
19                http://www.springframework.org/schema/tx
20                http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
21                http://www.springframework.org/schema/mvc
22                http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
23      >
24
25      <!--开启包扫描-->
26      <context:component-scan base-package="com.boe.dao"></context:component-scan>
27      <!--注释掉,sevice层上的事务才能生效,将service层的扫描放到spring配置文件中扫描,赋予事务-->
28      <!--<context:component-scan base-package="com.boe.service"></context:component-scan>-->
29      <context:component-scan base-package="com.boe.web"></context:component-scan>
30      <!--开启mvc注解-->
31      <mvc:annotation-driven></mvc:annotation-driven>
32      <!--配置视图解析器-->
33      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
34           <property name="suffix" value=".jsp"></property>
35           <property name="prefix" value="/WEB-INF/jsp/"></property>
36      </bean>
37
38 </beans>

Spring配置

(1)web.xml中配置,使用listener来读取applicationContext.xml配置文件,启动Spring容器。

1     <!--配置监听器,spring在web初始化时自动加载-->
2     <context-param>
3         <param-name>contextConfigLocation</param-name>
4         <param-value>classpath:applicationContext.xml</param-value>
5     </context-param>
6     <listener>
7         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
8     </listener>

(2)spring核心配置文件中,需要配置包扫描、DI注入、事务等。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xmlns:util="http://www.springframework.org/schema/util"
 6        xmlns:aop="http://www.springframework.org/schema/aop"
 7        xmlns:tx="http://www.springframework.org/schema/tx"
 8        xmlns:mvc="http://www.springframework.org/schema/cache"
 9        xsi:schemaLocation=
10                "http://www.springframework.org/schema/beans
11                http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
12                http://www.springframework.org/schema/context
13                http://www.springframework.org/schema/context/spring-context-4.3.xsd
14                http://www.springframework.org/schema/util
15                http://www.springframework.org/schema/util/spring-util-4.3.xsd
16                http://www.springframework.org/schema/aop
17                http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
18                http://www.springframework.org/schema/tx
19                http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
20                http://www.springframework.org/schema/mvc
21                http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
22 >
23
24     <!-- 开启包扫描 -->
25     <!--<context:component-scan base-package="com.boe.web"/>-->
26     <context:component-scan base-package="com.boe.service"/>
27     <!--<context:component-scan base-package="com.boe.dao"/>-->
28     <!--DI依赖注入-->
29     <context:annotation-config></context:annotation-config>
30     <!--引入外部数据库连接配置文件 -->
31     <context:property-placeholder location="classpath:connConf/db.properties"/>
32     <!--配置数据源 -->
33     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
34         <property name="driverClass" value="${driverClass}"/>
35         <property name="jdbcUrl" value="${jdbcUrl}"/>
36         <property name="user" value="${user}"/>
37         <property name="password" value="${password}"/>
38     </bean>
39
40     <!--配置事务管理器-->
41     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
42         <property name="dataSource" ref="dataSource"></property>
43     </bean>
44     <!--开启事务注解-->
45     <tx:annotation-driven></tx:annotation-driven>
46
47 </beans>

Mybatis配置

(1)mybatis的配置需要准备核心配置文件sqlMapConfig和mapper映射文件(这里准备了一个UserMapper.xml)。

sqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6
 7     <!--配置mapper映射文件-->
 8     <!--<mappers>
 9         <mapper resource="UserMapper.xml"></mapper>
10     </mappers>-->
11
12 </configuration>

UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5
 6 <mapper namespace="com.boe.dao.UserMapper">
 7
 8     <!--查询user-->
 9     <select id="findAll" resultType="com.boe.domain.User">
10         SELECT * FROM user;
11     </select>
12
13
14     <!--插入一个user-->
15     <insert id="addUser" parameterType="com.boe.domain.User">
16         INSERT INTO user
17         <trim prefix="(" suffix=")" suffixOverrides=",">
18             id,
19             <if test="name!=null">name,</if>
20             <if test="age!=0">age,</if>
21         </trim>
22         VALUES
23         <trim prefix="(" suffix=")" suffixOverrides=",">
24             null,
25             <if test="name!=null">#{name},</if>
26             <if test="age!=0">#{age},</if>
27         </trim>
28     </insert>
29
30 </mapper>

(2)在spring核心配置文件中添加跟Mybatis相关的配置,完成整合。需要配置Sqlsessionfactorybean,里面配置了数据源、mybatis核心配置文件和mapper映射文件(原来在mybatis核心配置文件中完成数据源和mapper映射文件的配置),最后配置MapperScannerConfigurer,实现对mapper映射器(接口)的扫描(原本需要使用SqlSession的getMapper方法获得)。

 1 <!--整合mybatis,配置sqlsession-->
 2     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 3         <!--引入数据源-->
 4         <property name="dataSource" ref="dataSource"></property>
 5         <!--引入mybatis核心配置文件-->
 6         <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
 7         <!--引入映射文件-->
 8         <property name="mapperLocations" value="classpath:Mapper/UserMapper.xml"></property>
 9     </bean>
10
11     <!--配置包mapper扫描,里面放置接口-->
12     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
13         <property name="basePackage" value="com.boe.dao"></property>
14     </bean>

以上就完成了SSM核心文件的配置,跟数据库连接相关的属性文件这里略去不展示,最后配置完成后的项目结构如下图。

启动SSM测试

配置完成后还没有完成,需要启动tomcat服务器查看是否正常启动,这里就可能开始踩坑了。

(1)java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener

启动后发现部署提示OK,但是继续查看Tomcat Localhost Log发现执行报错,提示说找不到这个类,但是实际上配置web.xml中是可以找到这个类,所以说明包是有的(spring-web相关jar包),后面查看发现部署到tomcat后没有lib目录,因此报错。

解决办法最简单粗暴的方式是重新添加Artifacts,先将当前的删除。

然后重新从Module中添加,发现Output Layout里有lib目录,另外tomcat可能需要重新配置一下Deployment,将刚开新的artifacts重新导入一下,重新启动发现不再报错。

(2)无法读取到配置文件信息,如spring-mvc.xml等,这种情况一般就是没有配置sources和resources目录导致,按照上面的方法配置即可,刚开始也采坑多次,才发现IDEA需要配置这个。

最后没问题做测试,可以发送请求正常连接数据库,并返回json数据。

参考博文:

(1)https://www.jianshu.com/p/18d068f47b09

原文地址:https://www.cnblogs.com/youngchaolin/p/11647140.html

时间: 2024-08-28 17:09:01

IDEA 整合SSM入门的相关文章

shiro权限控制(一):shiro介绍以及整合SSM框架

shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是否被允许做某事 会话管理:在任何环境下使用 Session API,即使没有 Web 或EJB 容器. 加密:以更简洁易用的方式使用加密功能,保护或隐藏数据防止被偷窥 Realms:聚集一个或多个用户安全数据的数据源 单点登录(SSO)功能. 为没有关联到登录的用户启用 "Remember Me&q

flex eclipse整合spring入门

最先下载FlashBuilder_4_7_LS10_win64.exe试了几个eclipse安装插件都没成功,包括myeclipse8.5.spring sts2.9.2.eclipse3.5.j2eeeclipse版本4.2.0,后来搞了一个FlashBuilder_4_LS10.exe安装完找不到插件安装文件原来这个是单独版,必须插件版才行,最后下载FlashBuilder_4_Plugin_LS10.exe终于配置成功了,myeclipse8.5不行,spring sts可以了. spri

日常开发系列——Maven+Spring+Spring MVC+MyBatis+MySQL整合SSM框架

进入公司开发已经3个多月了,项目用的是Maven+Spring+Spring MVC+MyBatis+MySQL,趁这个周末有空,仔细研读一下公司项目的基本框架,学习一下这个环境是怎么搭建起来的,经过自己的研究终于是成功地试验出来.自己亲手做的才算是自己学到的,决定将其记录下来,以便日后查询,源码同时也欢迎大家拍砖. 一.数据库的准备 这次整合试验想着做个简单的,就决定做一个普通的用户登陆,就一张表吧 我新建的数据库名字是test,然后新建了一张表 DROP TABLE IF EXISTS `u

SpringBoot整合SSM项目

1.1.1      系统架构图 1.1.2      创建数据库表 p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; text-indent: 21.0pt; font-size: 10.5pt; font-family: Consolas } h1 { margin-top: 17.0pt; margin-right: 0cm; margin-b

SpringBoot整合SSM(代码实现Demo)

SpringBoot整合SSM 如图所示: 一.数据准备: 数据库文件:数据库名:saas-export,表名:ss_company 创建表语句: DROP TABLE IF EXISTS ss_company;CREATE TABLE ss_company ( id varchar(40) NOT NULL COMMENT 'ID', name varchar(255) DEFAULT NULL COMMENT '公司名称', expiration_date datetime DEFAULT

Solr基础知识三(整合SSM)

前两篇讲了solr安装和导入数据,这篇讲如何整合到SSM中. 一.整合SSM 1.1 引入依赖 1.2 初始化solr 1.3 写service 1.4 写控制层 1.5 查询 二.IK分词器 2.1.添加jar包 下载地址:https://search.maven.org/search?q=com.github.magese 源码:https://github.com/magese/ik-analyzer-solr 2.2 添加配置文件(默认) 2.3 添加字段类型 2.4 重新导入数据 原文

SpringBoot整合RabbitMQ入门~~

SpringBoot整合RabbitMQ 入门2020-01-12 创建生产者类,并且在yml配置文件中配置5要素连接MQ yml配置文件 spring: rabbitmq: host: xx.xx.xx.xx port: 5672 virtual-host: / username: 默认guest password: 默认guest 编写生产者代码 使用@Configuration 表名它是配置类 再类中声明三要素 //交换机名称 @Bean("itemTopicExchange")

新手入门——使用maven整合SSm框架

步骤1: 使用webapp骨架创建一个Maven项目. 步骤2:数据库中存放数据,为测试做准备 DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `price` float(10,0) DEFAULT NULL, `pic` varchar(40) DEFAULT NULL, `createtime`

Spring MVC整合Mybatis 入门

本文记录使用Intellij创建Maven Web工程搭建Spring MVC + Mybatis 的一个非常简单的示例.关于Mybatis的入门使用可参考这篇文章,本文在该文的基础上,引入了Spring MVC功能.首先是创建项目: 打开Intellij,File-->new Project--->选中,Maven--->勾上"Create from archetype"--->选择 Maven web project.如下图: 一步步Next,等待工程Bui