使用IDEA搭建SSM框架的詳細(xì)教程(spring + springMVC +MyBatis)
Spring
SpringMVC
MyBatis
2 所需工具Mysql 8.0.15
數(shù)據(jù)庫(kù)管理系統(tǒng),創(chuàng)建數(shù)據(jù)庫(kù)
Tomcat 8.5.51
用于部署web項(xiàng)目
Maven 3.6.1
項(xiàng)目構(gòu)建、項(xiàng)目依賴(lài)管理
lombok 1.18.10(可用可不用工具)
用于類(lèi)注解創(chuàng)建setter、getter、無(wú)參構(gòu)造、全參構(gòu)造、toString等函數(shù)
注:只導(dǎo)入依賴(lài),不安裝插件是不起作用的
3 搭建步驟3.1 新建一個(gè)空Maven項(xiàng)目,填寫(xiě)項(xiàng)目相關(guān)信息,完成
3.2 添加web框架支持
選擇現(xiàn)有框架支持
3.3 pom.xml導(dǎo)入依賴(lài),設(shè)置Maven資源過(guò)濾
<!--導(dǎo)入依賴(lài)--><dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--數(shù)據(jù)庫(kù)驅(qū)動(dòng)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!-- 數(shù)據(jù)庫(kù)連接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet - JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency></dependencies><!--靜態(tài)資源導(dǎo)出問(wèn)題--><build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources></build>
3.4 編寫(xiě)MyBatis-config.xml(核心配置文件)
<?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> <!--設(shè)置運(yùn)行日志--> <settings> <setting name='logImpl' value='STDOUT_LOGGING'/> </settings> <!--取別名--> <typeAliases> <package name='com.pojo'/> </typeAliases> <!--綁定mapper,根據(jù)自己的項(xiàng)目設(shè)置--> <mappers> <mapper resource='com/dao/Mapper.xml'/> </mappers></configuration>
3.5 編寫(xiě)database.properties(數(shù)據(jù)庫(kù)配置文件)
jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名?useSSL=true&useUnicode=true&characterEncoding=utf8jdbc.username=數(shù)據(jù)庫(kù)用戶(hù)名jdbc.password=數(shù)據(jù)庫(kù)密碼
根據(jù)自己的MySQL以及項(xiàng)目實(shí)際使用的數(shù)據(jù)庫(kù)來(lái)修改設(shè)置
注:MySQL8.0以上驅(qū)動(dòng)得使用com.mysql.cj.jdbc.Driver
3.6 編寫(xiě)Spring-dao.xml(Spring整合MyBatis配置文件)
<?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' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd'> <!-- 配置整合mybatis --> <!-- 1.關(guān)聯(lián)數(shù)據(jù)庫(kù)文件 --> <context:property-placeholder location='classpath:database.properties'/> <!-- 2.數(shù)據(jù)庫(kù)連接池 --> <!--數(shù)據(jù)庫(kù)連接池 dbcp 半自動(dòng)化操作 不能自動(dòng)連接 c3p0 自動(dòng)化操作(自動(dòng)的加載配置文件 并且設(shè)置到對(duì)象里面) --> <bean 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}'/> <!-- c3p0連接池的私有屬性 --> <property name='maxPoolSize' value='30'/> <property name='minPoolSize' value='10'/> <!-- 關(guān)閉連接后不自動(dòng)commit --> <property name='autoCommitOnClose' value='false'/> <!-- 獲取連接超時(shí)時(shí)間 --> <property name='checkoutTimeout' value='10000'/> <!-- 當(dāng)獲取連接失敗重試次數(shù) --> <property name='acquireRetryAttempts' value='2'/> </bean> <!-- 3.配置SqlSessionFactory對(duì)象 --> <bean class='org.mybatis.spring.SqlSessionFactoryBean'> <!-- 注入數(shù)據(jù)庫(kù)連接池 --> <property name='dataSource' ref='dataSource'/> <!-- 配置MyBaties全局配置文件:MyBatis-config.xml --> <property name='configLocation' value='classpath:MyBatis-config.xml'/> </bean> <!-- 4.配置掃描Dao接口包,動(dòng)態(tài)實(shí)現(xiàn)Dao接口注入到spring容器中 --> <!--解釋 :https://www.cnblogs.com/jpfss/p/7799806.html--> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> <!-- 注入sqlSessionFactory --> <property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'/> <!-- 給出需要掃描Dao接口包 --> <property name='basePackage' value='com.dao'/> </bean></beans>
3.7 編寫(xiě)Spring-service.xml(Spring整合service層)
<?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' 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'> <!-- 掃描service相關(guān)的bean --> <context:component-scan base-package='com.service' /> <!--ServiceImpl注入到IOC容器中,此處需要修改成自己的--> <bean class='com.service.ServiceImpl'> <property name='Mapper' ref='Mapper'/> </bean> <!-- 配置事務(wù)管理器 --> <bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <!-- 注入數(shù)據(jù)庫(kù)連接池 --> <property name='dataSource' ref='dataSource' /> </bean></beans>
3.8 修改web.xml文件
<?xml version='1.0' encoding='UTF-8'?><web-app xmlns='http://xmlns.jcp.org/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd' version='4.0'> <!--DispatcherServlet--> <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:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--encodingFilter--> <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> <!--Session過(guò)期時(shí)間--> <session-config> <session-timeout>15</session-timeout> </session-config></web-app>
3.9 編寫(xiě)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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd'> <!-- 配置SpringMVC --> <!-- 1.開(kāi)啟SpringMVC注解驅(qū)動(dòng) --> <mvc:annotation-driven /> <!-- 2.靜態(tài)資源默認(rèn)servlet配置--> <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' /> <!--此處注意路徑問(wèn)題,/WEB-INF/jsp/--> <property name='prefix' value='/WEB-INF/jsp/' /> <property name='suffix' value='.jsp' /> </bean> <!-- 4.掃描web相關(guān)的bean --> <context:component-scan base-package='com.controller' /></beans>
3.10 編寫(xiě)applicationContext.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' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'> <!--將Spring其他配置文件整合到一個(gè)總的配置文件,用的時(shí)候使用這個(gè)配置文件--> <import resource='classpath:Spring-service.xml'/> <import resource='classpath:Spring-dao.xml'/> <import resource='classpath:Spring-mvc.xml'/></beans>
3.11 配置Tomcat
3.12 檢查項(xiàng)目結(jié)構(gòu)(左上角 文件 -> 項(xiàng)目結(jié)構(gòu))
3.13 最后的項(xiàng)目文件結(jié)構(gòu)
到了這里,框架已經(jīng)搭建完成
4 接口對(duì)應(yīng)的Mapper.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'><!--綁定對(duì)應(yīng)的接口--><mapper namespace='com.dao.Mapper'> <!--此處寫(xiě)對(duì)應(yīng)的SQL操作--></mapper>5 功能添加步驟 先編寫(xiě)實(shí)體類(lèi)(pojo) dao層:編寫(xiě)接口,接口對(duì)應(yīng)mapper.xml(建議同名) service層:編寫(xiě)接口,編寫(xiě)接口實(shí)現(xiàn)類(lèi)(創(chuàng)建dao層對(duì)象,返回調(diào)用dao層的操作) controller層:負(fù)責(zé)具體的業(yè)務(wù)模塊流程的控制,在此層要調(diào)用service層的接口來(lái)控制業(yè)務(wù)流程 編寫(xiě)相應(yīng)的jsp文件6 建議
框架搭建完成后應(yīng)寫(xiě)個(gè)簡(jiǎn)單的功能測(cè)試框架環(huán)境有無(wú)問(wèn)題
7 SSM框架項(xiàng)目文件
http://xiazai.jb51.net/202005/yuanma/ssm_kuangjia_jb51.rar
總結(jié)
到此這篇關(guān)于使用IDEA搭建SSM框架的詳細(xì)教程 spring + springMVC +MyBatis的文章就介紹到這了,更多相關(guān)IDEA搭建SSM框架內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼2. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)3. jsp EL表達(dá)式詳解4. jsp中sitemesh修改tagRule技術(shù)分享5. JavaWeb Servlet中url-pattern的使用6. 爬取今日頭條Ajax請(qǐng)求7. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁(yè)中的JS 文件8. ASP基礎(chǔ)知識(shí)VBScript基本元素講解9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
