SSM(CURD小項(xiàng)目)-配置(非注解手動(dòng)配bean的方式)

導(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>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>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>5.1.9.RELEASE</version>

</dependency>

</dependencies>


資源過(guò)濾


<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>


建好需要的包:

需要的配置文件:




配置



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">

? ? ? ? <package name="com.fhl.pojo"/>

? ? ? ? <mapper class="com.fhl.dao.BookMapper"/>

</configuration


database.properties


jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf8

jdbc.username=root

jdbc.password=123456


spring-dao.xml? 整合mybatis以及連接數(shù)據(jù)庫(kù)


<?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"/>

? ? <!-- 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"/>

? ? <!-- 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.fhl.dao"/>

</beans>


spring-service.xml? ? spring注入相關(guā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"

? ? ? 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.fhl.service" />

? ? <!--BookServiceImpl注入到IOC容器中-->

? ? <bean id="BookServiceImpl" class="com.fhl.service.BookServiceImpl">

? ? ? ? <property name="bookMapper" ref="bookMapper"/>

? ? <!-- 配置事務(wù)管理器 -->

? ? <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

? ? ? ? <!-- 注入數(shù)據(jù)庫(kù)連接池 -->

? ? ? ? <property name="dataSource" ref="dataSource" />

</beans>


web.xml和spring-mvc.xml


?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">


? ?

? ? ? ? <servlet-name>DispatcherServlet

? ? ? ? <servlet-class>org.springframework.web.servlet.DispatcherServlet

? ? ? ? ? ? <param-name>contextConfigLocation

? ? ? ? ? ? <!--一定要注意:我們這里加載的是總的配置文件,之前被這里坑了!-->

? ? ? ? ? ? <param-value>classpath:applicationContext.xml

? ? ? ? <load-on-startup>1

? ? ? ? <servlet-name>DispatcherServlet

? ? ? ? <url-pattern>/


? ?

? ? ? ? <filter-name>encodingFilter

? ? ? ? ? ? org.springframework.web.filter.CharacterEncodingFilter

? ? ? ? ? ? <param-name>encoding

? ? ? ? ? ? <param-value>utf-8

? ? ? ? <filter-name>encodingFilter

? ? ? ? <url-pattern>/*

? ? <!--Session過(guò)期時(shí)間-->

? ?

? ? ? ? <session-timeout>15

</web-app>


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" />

? ? ? ? <property name="prefix" value="/WEB-INF/jsp/" />

? ? ? ? <property name="suffix" value=".jsp" />

? ? <!-- 4.掃描web相關(guān)的bean -->

? ? <context:component-scan base-package="com.fhl.controller" />

</beans>


applicationContext.xml? 最后把三個(gè)spring相關(guān)導(dǎo)入里面 整合到一起


<?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="spring-dao.xml"/>

? ? <import resource="spring-service.xml"/>

? ? <import resource="spring-mvc.xml"/>

</beans>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容