Java 進(jìn)階必備之ssm框架全面整合
1.導(dǎo)入依賴
<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>5.1.47</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> <!--spring jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!-- spring的核心依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> <!--json的依賴--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> </dependencies> <!-- <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.0</version> </dependency>--> <!--靜態(tài)資源問題--> <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>
2.創(chuàng)建實(shí)體類
package pojo; public class books { private int bookId; private String bookName; private int bookCounts; private String detail; public books(int bookId, String bookName, int bookCounts, String detail) { this.bookId = bookId; this.bookName = bookName; this.bookCounts = bookCounts; this.detail = detail; } public books() { } public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookCounts() { return bookCounts; } public void setBookCounts(int bookCounts) { this.bookCounts = bookCounts; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } }
3.寫dao層接口
這里暫時(shí)只有一個(gè)方法
package Dao; import pojo.books; import java.util.List; public interface BooksMapper { List<books> selectbooks(); }
4.寫mybatis核心配置文件和接口配置文件
這個(gè)是接口配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--把剛剛我們寫的接口配置進(jìn)去--> <mapper namespace="Dao.BooksMapper"> <select id="BooksMapper" resultType="pojo.books"> select * from books </select> </mapper>
mybatis核心配置文件
<?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> <!--把那個(gè)接口導(dǎo)進(jìn)來(lái)--> <mappers> <package name="Dao"/> </mappers> </configuration>
5.用spring整合Mybatis層也就是Dao層
這個(gè)是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: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> <!--解釋 :https://www.cnblogs.com/jpfss/p/7799806.html--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 4.配置掃描Dao接口包,動(dòng)態(tài)實(shí)現(xiàn)Dao接口注入到spring容器中 --> <property name="basePackage" value="Dao"/> </bean> </beans>
數(shù)據(jù)庫(kù)配置文件,這里你們改一下數(shù)據(jù)庫(kù)就OK
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild jdbc.username=root jdbc.password=root
6.spring整合Service層
寫接口和實(shí)現(xiàn)類
package Service; import org.springframework.stereotype.Service; import pojo.books; import java.util.List; public interface BooksService { List<books> selectbooks(); }
實(shí)現(xiàn)類
@Service public class BooksServicelmpl implements BooksService{ /*這里是把Dao的接口引進(jìn)來(lái)了因?yàn)镾ervice層調(diào)用Dao層*/ @Autowired private BooksMapper booksMapper; public void setBooksMapper(BooksMapper booksMapper) { this.booksMapper = booksMapper; } @Override public List<books> selectbooks() { return booksMapper.selectbooks(); } }
寫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" 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:component-scan base-package="Service"/> <!-- <bean id="bookServicelmpl" class="service.bookServicelmpl"> <property name="bookMapper" ref="bookMapper"/> </bean>--> <!--因?yàn)閟erv層要調(diào)Dao層我們把數(shù)據(jù)源拿過來(lái),如果這里報(bào)紅就是因?yàn)闆]有引入Dao的那個(gè)數(shù)據(jù)源--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
關(guān)聯(lián)spring配置文件,我們寫個(gè)總的配置文件到進(jìn)去就ok
<?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"> <import resource="classpath:spring-Service.xml"/> <import resource="classpath:spring-Dao.xml"/> </beans>
7.spring整合Conteoller層
<?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"> <!-- 掃描web相關(guān)的bean --> <context:component-scan base-package="Controller"/> <!-- 1.開啟SpringMVC注解驅(qū)動(dòng) --> <mvc:annotation-driven/> <!-- 2.靜態(tài)資源默認(rèn)servlet配置--> <mvc:default-servlet-handler/> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 后綴 --> <property name="suffix" value=".jsp"/> </bean> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="failOnEmptyBeans" value="false"/> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
寫Controller的類
@RestController public class bookController { @Autowired private BooksService booksService; public void setBooksService(BooksService booksService) { this.booksService = booksService; } @RequestMapping("/books") public List<books> selectbooks() { List<books> list = booksService.selectbooks(); return list; } }
把三個(gè)配置文件關(guān)聯(lián)
<?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"> <import resource="classpath:spring-Service.xml"/> <import resource="classpath:spring-Dao.xml"/> <import resource="spring-mvc.xml"/> </beans>
8.添加web支持
寫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>springmvc</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>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--亂碼解決--> <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過期時(shí)間--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
創(chuàng)建一個(gè)lib包把依賴導(dǎo)進(jìn)去
添加工件測(cè)試
9.完整的目錄結(jié)構(gòu)
到此這篇關(guān)于Java 進(jìn)階必備之ssm框架全面整合的文章就介紹到這了,更多相關(guān)Java ssm框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)
這篇文章主要介紹了Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Mybatis-plus多租戶項(xiàng)目實(shí)戰(zhàn)進(jìn)階指南
多租戶是一種軟件架構(gòu)技術(shù),在多用戶的環(huán)境下共有同一套系統(tǒng),并且要注意數(shù)據(jù)之間的隔離性,下面這篇文章主要給大家介紹了關(guān)于Mybatis-plus多租戶項(xiàng)目實(shí)戰(zhàn)進(jìn)階的相關(guān)資料,需要的朋友可以參考下2022-02-02Java中String.split()的最詳細(xì)源碼解讀及注意事項(xiàng)
以前經(jīng)常使用String.split()方法,但是從來(lái)沒有注意,下面這篇文章主要給大家介紹了關(guān)于Java中String.split()最詳細(xì)源碼解讀及注意事項(xiàng)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07springboot+thymeleaf+layui的實(shí)現(xiàn)示例
本文主要介紹了springboot+thymeleaf+layui的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn)
這篇文章主要為大家介紹了Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java爬蟲框架之WebMagic實(shí)戰(zhàn)
這篇文章主要介紹了Java爬蟲框架之WebMagic實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringMVC表單提交參數(shù)400錯(cuò)誤解決方案
這篇文章主要介紹了SpringMVC表單提交參數(shù)400錯(cuò)誤解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10