专业的编程技术博客社区

网站首页 > 博客文章 正文

IDEA搭建SSM(spring+springmvc+mybatis)Maven项目

baijin 2024-10-20 04:13:06 博客文章 225 ℃ 0 评论

IDEA搭建项目所需准备:idea开发工具、jdk1.7、mysql、Navicat、maven

一、创建maven项目

1、 file -----> new -----> project


2、点击 next



3、点击 next


4、点击 next


5、点击 finish ,静静的等待项目的创建,控制台出现夏天标志,则创建maven项目成功


二、创建对应的文件夹

1、在main下创建java、resources文件夹并且设置为Sources Root 和Resources Root


2、在java下建常用的包,如下图所示:


3、在resources 下建文件夹,如下图所示


4、创建jsp文件存放的文件夹



三、编写对应的配置文件

1、pom.xml,里面包含了所需要的jar包


版本控制,方便修改

<?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:aop="http://www.springframework.org/schema/aop"       xmlns:c="http://www.springframework.org/schema/c"       xmlns:cache="http://www.springframework.org/schema/cache"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:jee="http://www.springframework.org/schema/jee"       xmlns:lang="http://www.springframework.org/schema/lang"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:task="http://www.springframework.org/schema/task"       xmlns:util="http://www.springframework.org/schema/util"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd      http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-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/task http://www.springframework.org/schema/task/spring-task-4.3.xsd      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">    <!--使用注解驱动-->    <mvc:annotation-driven />    <!--配置视图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--重定向时,是否加上上下文路径-->        <property name="redirectContextRelative" value="true"/>        <!--配置解析前后缀-->        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean>    <!--扫描所有controller-->    <context:component-scan base-package="cn.doyoulove.controller"/></beans>

2、编写spring-mvc.xml


driver=com.mysql.jdbc.Driver#driver=com.mysql.cj.jdbc.Driver#mytest为我本地的数据库名url=jdbc:mysql://localhost:3306/test#url=jdbc:mysql://192.168.220.139:3306/teaching_manageusername=root#下面输入自己数据库的密码password=123456#定义初始连接数initialSize=1#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000

3、编写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:p="http://www.springframework.org/schema/p"       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/context                        http://www.springframework.org/schema/context/spring-context-4.0.xsd                        http://www.springframework.org/schema/mvc                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <!--自动扫描-->    <context:component-scan base-package="cn.doyoulove"/>    <!--引入properties文件-->    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="location" value="classpath:jdbc.properties"></property>    </bean>    <!--配置数据源-->    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">        <!--数据库连接池-->        <property name="driverClassName" value="${driver}"/>        <property name="url" value="${url}"/>        <property name="username" value="${username}"/>        <property name="password" value="${password}"/>        <!-- 初始化连接大小 -->        <property name="initialSize" value="${initialSize}"></property>        <!-- 连接池最大数量 -->        <property name="maxActive" value="${maxActive}"></property>        <!-- 连接池最大空闲 -->        <property name="maxIdle" value="${maxIdle}"></property>        <!-- 连接池最小空闲 -->        <property name="minIdle" value="${minIdle}"></property>        <!-- 获取连接最大等待时间 -->        <property name="maxWait" value="${maxWait}"></property>    </bean>    <!--配置sqlsession工厂-->    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <!-- 自动扫描mapping.xml文件 -->        <property name="mapperLocations" value="classpath:mapper/*.xml"/>    </bean>    <!--配置DAO所在spring会自动查找下面的类-->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="cn.doyoulove.dao"/>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>    </bean></beans>

4、方便管理数据库的相关配置,所有会单独的用一个文件来存放。jdbc.properties文件


driver=com.mysql.jdbc.Driver#driver=com.mysql.cj.jdbc.Driver#mytest为我本地的数据库名url=jdbc:mysql://localhost:3306/test#url=jdbc:mysql://192.168.220.139:3306/teaching_manageusername=root#下面输入自己数据库的密码password=123456#定义初始连接数initialSize=1#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000

5、由于编写spring-mybatis.xml时,扫描mapper下的所有xml会报错,因为下面没有xml文件,所有建一个xml文件。


在dao下新建一个接口

6、编写wen.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="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"        version="3.0"        metadata-complete="true">  <display-name>Archetype Created Web Application</display-name>  <!--设置默认的字符集-->  <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>  <!--启动spring容器-->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:spring/spring-mybatis.xml</param-value>  </context-param>  <!--初始化spring-mvc容器-->  <servlet>    <servlet-name>springDispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring/spring-mvc.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springDispatcherServlet</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping></web-app>

到此为止,所有的配置文件编写完成

四、配置tomcat,启动项目

1、

2、


3、

4、


5、


6、

7、


8、启动成功


9、访问成功



自此,ssm整合完成,下一篇开始编写案例

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表