springmvc整合ssm配置的詳細代碼
更新時間:2021年11月25日 09:40:35 作者:江南0o0
今天通過實例代碼給大家介紹了springmvc整合ssm配置的詳細方法,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
導(dǎo)入依賴
<!-- 常用的依賴導(dǎo)入--> <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.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> <!-- 導(dǎo)入lombock--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.12</version> <scope>provided</scope> </dependency> </dependencies> <!-- 靜態(tài)資源導(dǎo)出--> <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>
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-service.xml"></import> <import resource="classpath:spring-dao.xml"></import> <import resource="classpath:spring-mvc.xml"></import> </beans>
spring-service.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"> <!-- 1.掃描service下的包--> <context:component-scan base-package="com.kuang.service"/> <!-- 2.將業(yè)務(wù)類注入到spring--> <bean id="BookServiceImpl" class="com.kuang.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"></property> </bean> <!-- 3.申明事物--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數(shù)據(jù)源--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 5.注入aop事物支持--> </beans>
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"> <!-- 1.注解驅(qū)動--> <mvc:annotation-driven/> <!-- 2.靜態(tài)資源過濾--> <mvc:default-servlet-handler/> <!-- 3.掃描包:controller--> <context:component-scan base-package="com.kuang.controller"/> <!-- 4.視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
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"> <!-- 1.關(guān)聯(lián)數(shù)據(jù)庫配置文件;倒入配置文件--> <context:property-placeholder location="classpath:database.properties"></context:property-placeholder> <!-- 2.連接池--> <!-- dpcp:半自動化連接,不能自動連接 c3p0:自動化操作(可以自己加載配置文件,并且可以自動設(shè)置到對象中 druid --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <!-- 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.創(chuàng)建sqlsessionfaction--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 綁定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 4.配置接口掃描包--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory類--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.kuang.dao"></property> </bean> </beans>
mybatis-comfig.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> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 配置數(shù)據(jù)源,交給spring去管理--> <typeAliases> <package name="com.kuang.pojo"/> </typeAliases> <mappers> <mapper class="com.kuang.dao.BookMapper"></mapper> </mappers> </configuration>
database.propertes
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
到此這篇關(guān)于springmvc整合ssm配置的文章就介紹到這了,更多相關(guān)springmvc整合ssm配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序
這篇文章主要介紹了聊聊Spring AOP @Before @Around @After等advice的執(zhí)行順序,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Spring?Boot集成JavaMailSender發(fā)送郵件功能的實現(xiàn)
spring提供了發(fā)送郵件的接口JavaMailSender,通過JavaMailSender可以實現(xiàn)后端發(fā)送郵件,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot集成JavaMailSender發(fā)送郵件功能的相關(guān)資料,需要的朋友可以參考下2022-05-05Java實現(xiàn)提取不重復(fù)的整數(shù)實例
這篇文章主要介紹了Java實現(xiàn)提取不重復(fù)的整數(shù)實例,具有一定借鑒價值,需要的朋友可以參考下2017-12-12SpringCloud Feign配置應(yīng)用詳細介紹
這篇文章主要介紹了SpringCloud Feign配置應(yīng)用,feign是netflix提供的服務(wù)間基于http的rpc調(diào)用框架,在spring cloud得到廣泛應(yīng)用2022-09-09SpringBoot Jpa 自定義查詢實現(xiàn)代碼詳解
這篇文章主要介紹了SpringBoot Jpa 自定義查詢實現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02