IDEA SSM框架整合配置及步驟詳解
參考
狂神說SpringMVC05:整合SSM框架
前言
根據(jù)自己的環(huán)境參考狂神的視頻進(jìn)行了SSM框架整合,用于備忘
SSM框架整合步驟
1. 創(chuàng)建數(shù)據(jù)庫
2. IDEA創(chuàng)建maven項目.在pom.xml中設(shè)設(shè)置java conpiler版本(jdk13)
3. 導(dǎo)入依賴 jnuit 數(shù)據(jù)庫驅(qū)動 數(shù)據(jù)庫連接 Severlet JSp
4. 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> <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> <groupId>org.example</groupId> <artifactId>ssm</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>13</maven.compiler.source> <maven.compiler.target>13</maven.compiler.target> </properties> <dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--數(shù)據(jù)庫驅(qū)動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 數(shù)據(jù)庫連接池 --> <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.6</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.4</version> </dependency> </dependencies> </project>
5. 創(chuàng)建兩個項目的目錄結(jié)構(gòu)以及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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
<?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> </configuration>
Mybatis
6. 配置數(shù)據(jù)庫屬性
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC jdbc.username=root jdbc.password=root
7. 創(chuàng)建Book實體類(使用lombok插件),編寫Dao層mapper接口,在mybatis配置中注冊mapper
package com.projectname.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Book { private int bookID; private String bookName; private int bookCounts; private String detail; }
package com.projectname.dao; import com.projectname.pojo.Book; import java.util.List; public interface DaoBook { //增加一個Book int addBook(Book book); //根據(jù)id刪除一個Book int deleteBookById(int id); //更新Book int updateBook(Book books); //根據(jù)id查詢,返回一個Book Book queryBookById(int id); //查詢?nèi)緽ook,返回list集合 List<Book> queryAllBook(); }
<?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"> <mapper namespace="com.kuang.dao.BookMapper"> <!--增加一個Book--> <insert id="addBook" parameterType="Books"> insert into ssmbuild.books(bookName,bookCounts,detail) values (#{bookName}, #{bookCounts}, #{detail}) </insert> <!--根據(jù)id刪除一個Book--> <delete id="deleteBookById" parameterType="int"> delete from ssmbuild.books where bookID=#{bookID} </delete> <!--更新Book--> <update id="updateBook" parameterType="Books"> update ssmbuild.books set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail} where bookID = #{bookID} </update> <!--根據(jù)id查詢,返回一個Book--> <select id="queryBookById" resultType="Books"> select * from ssmbuild.books where bookID = #{bookID} </select> <!--查詢?nèi)緽ook--> <select id="queryAllBook" resultType="Books"> SELECT * from ssmbuild.books </select> </mapper>
<?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> <mappers> <mapper resource="com/projectname/dao/BookMapper.xml"/> </mappers> </configuration>
8. 編寫service層的接口和實現(xiàn)類代碼
package com.projectname.service; import com.projectname.pojo.Book; import java.util.List; public interface BookService { //增加一個Book int addBook(Book book); //根據(jù)id刪除一個Book int deleteBookById(int id); //更新Book int updateBook(Book books); //根據(jù)id查詢,返回一個Book Book queryBookById(int id); //查詢?nèi)緽ook,返回list集合 List<Book> queryAllBook(); }
package com.projectname.service; import com.projectname.dao.BookMapper; import com.projectname.pojo.Book; import java.util.List; public class BookServiceImpl implements BookService { //調(diào)用dao層的操作,設(shè)置一個set接口,方便Spring管理 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } public int addBook(Book book) { return bookMapper.addBook(book); } public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } public int updateBook(Book books) { return bookMapper.updateBook(books); } public Book queryBookById(int id) { return bookMapper.queryBookById(id); } public List<Book> queryAllBook() { return bookMapper.queryAllBook(); } }
Spring
9. 使用Spring編寫Mybatis配置文件:spring-dao.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" 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"> <context:property-placeholder location="classpath:database.properties"/> <!-- 2.數(shù)據(jù)庫連接池 --> <!--數(shù)據(jù)庫連接池 dbcp 半自動化操作 不能自動連接 c3p0 自動化操作(自動的加載配置文件 并且設(shè)置到對象里面) --> <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)閉連接后不自動commit --> <property name="autoCommitOnClose" value="false"/> <!-- 獲取連接超時時間 --> <property name="checkoutTimeout" value="10000"/> <!-- 當(dāng)獲取連接失敗重試次數(shù) --> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- 3.配置SqlSessionFactory對象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入數(shù)據(jù)庫連接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-configuration.xml"/> </bean> <!-- 4.配置掃描Dao接口包,動態(tài)實現(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.projectname.dao"/> </bean> </beans>
10. Spring 整合service層
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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 掃描service相關(guān)的bean --> <context:component-scan base-package="com.projectname.service" /> <!--BookServiceImpl注入到IOC容器中--> <bean id="BookServiceImpl" class="com.projectname.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數(shù)據(jù)庫連接池 --> <property name="dataSource" ref="dataSource" /> </bean> </beans>
Spring MVC層
11. configuate the 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"> <!-- 配置SpringMVC --> <!-- 1.開啟SpringMVC注解驅(qū)動 --> <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過期時間--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
12. 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:mvc="http://www.springframework.org/schema/mvc" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置SpringMVC --> <!-- 1.開啟SpringMVC注解驅(qū)動 --> <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" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 4.掃描web相關(guān)的bean --> <context:component-scan base-package="com.projectname.controller" /> </beans>
13. configuate applicationContext.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans>
一些雜七雜八的配置
14. 添加日志功能
<settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
15. add lib
File -> Project Structure->Artifacts
Type -> Bundle
new directory lib
到此這篇關(guān)于IDEA SSM框架整合配置及步驟詳解的文章就介紹到這了,更多相關(guān)IDEA SSM框架整合配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springBoot中的CORS跨域注解@CrossOrigin詳解
這篇文章主要介紹了springBoot中的CORS跨域注解@CrossOrigin詳解,通常,服務(wù)于?JS?的主機(jī)(例如?example.com)與服務(wù)于數(shù)據(jù)的主機(jī)(例如?api.example.com)是不同的,在這種情況下,CORS?可以實現(xiàn)跨域通信,需要的朋友可以參考下2023-12-12SpringBoot?整合Mybatis-Plus并輸出SQL日志示例詳解
這篇文章主要介紹了SpringBoot整合Mybatis-Plus并輸出SQL日志,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Java中生成隨機(jī)數(shù)的4種方式與區(qū)別詳解
生成隨機(jī)數(shù)是我們?nèi)粘i_發(fā)經(jīng)常會遇到的一個功能,這篇文章主要給大家介紹了關(guān)于Java中生成隨機(jī)數(shù)的4種方式與區(qū)別、應(yīng)用場景的相關(guān)資料,4個方式分別是Random、ThreadLocalRandom、SecureRandom以及Math,需要的朋友可以參考下2021-06-06java中利用反射調(diào)用另一類的private方法的簡單實例
下面小編就為大家?guī)硪黄猨ava中利用反射調(diào)用另一類的private方法的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06簡單談?wù)凷truts動態(tài)表單(DynamicForm)
下面小編就為大家?guī)硪黄唵握務(wù)凷truts動態(tài)表單(DynamicForm)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08SpringBoot集成?JWT實現(xiàn)用戶登錄認(rèn)證的項目實踐
當(dāng)今前后端分離時代,基于Token的會話保持機(jī)制比傳統(tǒng)的Session/Cookie機(jī)制更加方便,本文主要介紹了SpringBoot集成?JWT實現(xiàn)用戶登錄認(rèn)證的項目實踐,感興趣的可以了解一下2023-08-08