使用IDEA搭建SSM框架的詳細(xì)教程(spring + springMVC +MyBatis)
1 框架組成
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)目依賴管理
lombok 1.18.10(可用可不用工具)
用于類(lèi)注解創(chuàng)建setter、getter、無(wú)參構(gòu)造、全參構(gòu)造、toString等函數(shù)
注:只導(dǎo)入依賴,不安裝插件是不起作用的
3 搭建步驟
3.1 新建一個(gè)空Maven項(xiàng)目,填寫(xiě)項(xiàng)目相關(guān)信息,完成
3.2 添加web框架支持
選擇現(xiàn)有框架支持
3.3 pom.xml導(dǎo)入依賴,設(shè)置Maven資源過(guò)濾
<!--導(dǎo)入依賴--> <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.Driver jdbc.url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=數(shù)據(jù)庫(kù)用戶名 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 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}"/> <!-- 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 id="sqlSessionFactory" 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 id="ServiceImpl" class="com.service.ServiceImpl"> <property name="Mapper" ref="Mapper"/> </bean> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" 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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解idea搭建springboot+mybatis框架的教程
- Spring+Mybatis+Mysql搭建分布式數(shù)據(jù)庫(kù)訪問(wèn)框架的方法
- 詳解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級(jí)詳細(xì)版)
- Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(圖文)
- 詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
- Spring MVC 4.1.3 + MyBatis零基礎(chǔ)搭建Web開(kāi)發(fā)框架(注解模式)
- Java的MyBatis框架項(xiàng)目搭建與hellow world示例
- Windows下Java+MyBatis框架+MySQL的開(kāi)發(fā)環(huán)境搭建教程
- MyBatis框架搭建與代碼解讀分析
相關(guān)文章
Java 基礎(chǔ)--Arrays工具類(lèi)詳解
這篇文章主要介紹了Java Arrays工具類(lèi)用法,結(jié)合實(shí)例形式分析了java Arrays工具類(lèi)針對(duì)數(shù)組元素修改、復(fù)制、排序等操作使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2021-09-09Mybatis 返回值類(lèi)型和參數(shù)傳遞的配置方法
在 MyBatis 中,返回值類(lèi)型和參數(shù)傳遞是 Mapper 接口中至關(guān)重要的兩個(gè)方面,正確理解和使用它們可以幫助我們高效、準(zhǔn)確地進(jìn)行數(shù)據(jù)庫(kù)操作,接下來(lái)通過(guò)本文給大家介紹Mybatis 返回值類(lèi)型和參數(shù)傳遞的配置方法,感興趣的朋友跟隨小編一起看看吧2024-08-08Pulsar源碼徹底解決重復(fù)消費(fèi)問(wèn)題
這篇文章主要為大家介紹了Pulsar源碼徹底解決重復(fù)消費(fèi)問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05SpringBoot學(xué)習(xí)之全局異常處理設(shè)置(返回JSON)
本篇文章主要介紹了SpringBoot學(xué)習(xí)之全局異常處理設(shè)置(返回JSON),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02SpringBoot--Banner的定制和關(guān)閉操作
這篇文章主要介紹了SpringBoot--Banner的定制和關(guān)閉操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2018-05-05基于線程的wait和notify使用,生產(chǎn)消費(fèi)案例
這篇文章主要介紹了基于線程的wait和notify使用,生產(chǎn)消費(fèi)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot權(quán)限認(rèn)證-Sa-Token的使用詳解
Sa-Token是一款輕量級(jí)Java權(quán)限認(rèn)證框架,它簡(jiǎn)化了權(quán)限管理,提高了開(kāi)發(fā)效率,本文通過(guò)實(shí)例介紹了Sa-Token的基本概念、與其他框架的比較、基本語(yǔ)法和高級(jí)用法,并探討了其核心原理和實(shí)際應(yīng)用場(chǎng)景,感興趣的朋友一起看看吧2024-09-09