1. 程式人生 > >ssm整合application.xml檔案設定總結

ssm整合application.xml檔案設定總結

application.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
     
        <context:annotation-config />
	<context:component-scan base-package="com.sjq.service" />

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
	  <property name="driverClassName">  
	      <value>com.mysql.jdbc.Driver</value>  
	  </property>  
	  <property name="url">  
	      <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>  
	
	  </property>  
	  <property name="username">  
	      <value>root</value>  
	  </property>  
	  <property name="password">  
	      <value>admin</value>  
	  </property>  	
	</bean>
	
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="typeAliasesPackage" value="com.sjq.pojo" />
		<property name="dataSource" ref="dataSource"/>
		<property name="mapperLocations" value="classpath:com/sjq/mapper/*.xml"/>
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sjq.mapper"/>
	</bean>
	
</beans>

<context:annotation-config >是用於啟用那些已經在spring容器裡註冊過的bean(無論是通過xml的方式還是通過package sanning的方式)上面的註解。
<context:component-scan>除了具有context:annotation-config的功能之外,還可以在指定的package下掃描以及註冊javabean 。具體地址https://www.cnblogs.com/doudouxiaoye/p/5681518.html和
https://www.cnblogs.com/leiOOlei/p/3713989.html

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

用來配置資料庫相關的東西

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">

在 MyBatis 中,使用 SqlSessionFactoryBuilder建立SqlSessionFactory ,進而來建立 SqlSession。一旦你獲得一個 session 之後,你可以使用它來執行對映語句,所以這邊是設定sqlSession 具體看別人的連結http://www.mybatis.org/spring/zh/factorybean.html
和https://www.baidu.com/s?ie=UTF-8&wd=SqlSessionFactoryBean
mapperLocations

:它表示我們的Mapper檔案存放的位置,當我們的Mapper檔案跟對應的Mapper介面處於同一位置的時候可以不用指定該屬性的值
configLocation:用於指定Mybatis的配置檔案位置。如果指定了該屬性,那麼會以該配置檔案的內容作為配置資訊構建對應的SqlSessionFactoryBuilder,但是後續屬性指定的內容會覆蓋該配置檔案裡面指定的對應內容
typeAliasesPackage:它一般對應我們的實體類所在的包,這個時候會自動取對應包中不包括包名的簡單類名作為包括包名的別名。多個package之間可以用逗號或者分號等來進行分隔(value的值一定要是包的全)
typeAliases:陣列型別,用來指定別名的。指定了這個屬性後,Mybatis會把這個型別的短名稱作為這個型別的別名,前提是該類上沒有標註@Alias註解,否則將使用該註解對應的值作為此種類型的別名(value的值一定要是類的完全限定名)別名的作用是
在這裡插入圖片描述
paramterType後的型別可以寫類名而不需要全名。參考這個部落格https://blog.csdn.net/hxb_hexiaobo/article/details/77184983

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.sjq.mapper"/>
    	</bean>

通過掃描器的方式自動載入mapper