Spring整合mybatis:
整合目的:不需要繁瑣的建立SqlSessionFactory等,而隻需要能夠将DAO代理對象注入到Service對象調用方法即可。
是以我們需要:編寫spring-mybatis配置類
三個作用:
1:dataSource 配置資料源
2:sqlSessionFactory配置工廠,加載資料源
3:配置MapperScannerConfigurer
MapperScannerConfigurer類似于component-scan,能夠掃描Component等進行注入,但是差別在于,他會将掃描的接口全部建立為MapperFactoryBean。
MapperFactoryBean會結合SqlSessionFactory建立代理類并且實作UserMapper 接口,進而能夠進行資料庫操作
注入和代理
注意:如果是無配置檔案的實作方式,可以直接利用MapperScannerConfigurer掃描
@Repository
public interface ProjectDao {
/**擷取所有項目資訊*/
@Select("select * from tms_projects")
List<Project> findObjects();
而如果是配置檔案型,兩種方法
1:MapperScannerConfigurer掃描:配置檔案和@Repository接口在同一包下且名字必須相同
2:需要在sqlSessionFactory進行配置,利用Factory掃描配置類
<property name="mapperLocations" >
<list>
<value>classpath:cn/tedu/ttms/*/dao/*.xml</value>
</list>
</property>
并且,如果使用maven,會預設忽視所有xml檔案,是以還需要在pom.xml中配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
原理:
WEB-INF/classes:java,resources裡的源碼打包進入和編譯後的class檔案
原來方式:但是僅僅會打包Java下的.java檔案和resources裡的所有檔案,而.xml等檔案會自動忽略。
目的:有時候,比如mybatis的mapper.xml檔案,我們習慣把它和Mapper.java放一起,都在src/main/java下面,這樣利用maven打包時,就需要修改pom.xml檔案,來把mapper.xml檔案一起打包進jar或者war裡了,否則,這些檔案不會被打包的。設定如上
注意點:
1:如果build中設定了打包方式,那麼原來方式就會失效。是以記住要
<resource>
<directory>src/main/resources</directory>
</resource>
2:打包進來的xml檔案不能是空檔案否則無法解析(sax解析失敗),也不能出錯
資料庫配置:
資料庫分為5.0和8.0,差別在于
1:jar包改變。8.0需要使用mysql.connector.mysql8.0的jar包
2:需要使用com.mysql.cj.jdbc.Driver驅動
3:url需要聲明useSSL=false,如果使用true加密協定,可能因為連接配接時間過長導緻報錯
spring-mybatis代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/ttms?useSSL=false"/>
<!-- 改為你的位址即可 -->
<property name="username" value="root"/>
<property name="password" value="123456789"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 自動掃描mapping.xml檔案 -->
<property name="mapperLocations" >
<list>
<value>classpath:mapper/*/*.xml</value>
<value>classpath:cn/tedu/ttms/*/dao/*.xml</value> <!-- 第一個*:任意的一個目錄 第二個*:任意的一個檔案字首-->
</list>
</property>
</bean>
<!-- Mapper接口所在包,Spring會自動查找其下的Mapper并封裝成Bean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tedu.**.dao "/>
</bean>
</beans>
Spring 整合SpringMVC:
目的:使得Spring中的@Service不需要再通過ClassPathXmlApplicationContext進行擷取工廠擷取Bean, 而是通過控制反轉的方式進行注入@Contoller
是以我們需要配置:
一:配置web.xml,進行配置檔案的啟動
這裡的目的是進行加載配置檔案,通過init-param配置classpath:spring-*.xml,加載我們配置的所有xml檔案。并且映射通路位址,相當于對通路位址設定監聽
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ttms1.01</display-name>
<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:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
二:配置spring-mvc.xml,進行配置檔案的啟動
1:配置需要的視圖解析器ViewResolver
2:進行注解掃描component-scan,掃描我們定義的@Controller(包含擴充卡和HandlerMapping注解)
3:開啟注解和類型轉換[非必須]
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
完整的spring-mvc代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<context:component-scan base-package="cn.tedu.ttms" >
</context:component-scan>
<!-- spring mvc 注解及類型轉換 -->
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
</bean>
<!-- spring mvc 視圖解析器 -->
<!-- 定義跳轉的檔案的前字尾 ,視圖模式配置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自動給後面action的方法return的字元串加上字首和字尾,變成一個 可用的url位址 -->
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置檔案上傳的視圖解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 預設編碼 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 檔案大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 記憶體中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
maven項目的結構:
編譯前:
注意webapp=WEB_INF+各種資源如圖檔,js,css
views通常是jsp頁面

編譯後:
classpath=WEB_INF/classes
WEB-INF/classes:java,resources裡的源碼打包進入和編譯後的class檔案
WEB-INF/lib:pom.xml中引入的jar包
META-INF:描述jar檔案中的資訊的一個目錄,了解為所有jar中META-INF目錄的集合
jar檔案:class檔案的zip壓縮存檔,有META-INF目錄,含有manifest.mf的檔案,包含了jar檔案的内容描述,在應用程式運作時向JVM提供應用程式的資訊