[ Spring實用功能--Profile、WebService、緩存、消息、ORM](http://blog.csdn.net/puma_dong/article/details/38036667)
[TOC=1,3]
本篇介紹一些Spring與其他框架結合的實用功能,包括:Apache CXF WebService框架、Redis緩存、RabbitMQ消息、MyBatis框架。
另外對于Profile,也是Spring3.0開始新加的功能,對于開發測試環境、和生產環境分別采用不同的配置,有一定用處。
# Profile
Spring3.1新屬性管理API:PropertySource、Environment、Profile。
Environment:環境,本身是一個PropertyResolver,但是提供了Profile特性,即可以根據環境得到相應數據(即激活不同的Profile,可以得到不同的屬性數據,比如用于多環境場景的配置(正式機、測試機、開發機DataSource配置))。
Profile:剖面,只有激活的剖面的組件/配置才會注冊到Spring容器,類似于maven中profile,Spring 3.1增加了一個在不同環境之間簡單切換的profile概念, 可以在不修改任何文件的情況下讓工程分別在 dev/test/production 等環境下運行。
為了減小部署維護,可以讓工程會默認運行在dev模式,而測試環境和生產環境通過增加jvm參數激活 production的profile.
比如,對于如下的一個例子,由于測試環境和生產環境,連接數據庫的方式不一樣,可以有如下的解決辦法:
1、首先ApplicationContext.xml中,xsi:schemaLocation需要引用3.2的xsd
2、ApplicationContext.xml配置如下:
**[html]**?[view plain](http://blog.csdn.net/puma_dong/article/details/38036667# "view plain")?[copy](http://blog.csdn.net/puma_dong/article/details/38036667# "copy")?[print](http://blog.csdn.net/puma_dong/article/details/38036667# "print")[?](http://blog.csdn.net/puma_dong/article/details/38036667# "?")[](https://code.csdn.net/snippets/432712 "在CODE上查看代碼片")[](https://code.csdn.net/snippets/432712/fork "派生到我的代碼片")
~~~
1. beans?profile="production">??
2. ????bean?id="dataSourcejdbc"?class="org.springframework.jndi.JndiObjectFactoryBean">??
3. ????????property?name="jndiName"?value="java:/MySqlDS_JDBC"?/>??
4. ????bean>??
5. beans>??
6. beans?profile="dev">??
7. ????bean?id="dataSourcejdbc"?class="org.springframework.jdbc.datasource.DriverManagerDataSource">??
8. ????????property?name="driverClassName"?value="com.mysql.jdbc.Driver"/>??
9. ????????property?name="url"?value="jdbc:mysql://IP:3306/db?characterEncoding=utf-8"/>??
10. ????????property?name="username"?value="root"/>??
11. ????????property?name="password"?value="root"/>??
12. ????bean>??
13. beans>??
~~~
3、開發環境配置,在web.xml中,如下配置:
**[html]**?[view plain](http://blog.csdn.net/puma_dong/article/details/38036667# "view plain")?[copy](http://blog.csdn.net/puma_dong/article/details/38036667# "copy")?[print](http://blog.csdn.net/puma_dong/article/details/38036667# "print")[?](http://blog.csdn.net/puma_dong/article/details/38036667# "?")[](https://code.csdn.net/snippets/432712 "在CODE上查看代碼片")[](https://code.csdn.net/snippets/432712/fork "派生到我的代碼片")
~~~
1. context-param>????
2. ????param-name>spring.profiles.defaultparam-name>????
3. ????param-value>devparam-value>????
4. context-param>??
~~~
4、生產環境配置
比如,對于Jboss,在bin/run.conf里面,增加啟動參數:-Dspring.profiles.active=production
`JAVA_OPTS="-Xms2048m -Xmx2048m -XX:MaxPermSize=1024m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dsun.lang.ClassLoader.allowArraySyntax=true -Dorg.terracotta.quartz.skipUpdateCheck=true -Dspring.profiles.active=production"`
以上是對于Web項目中如何利用profile的一種演示,如果是maven項目,也可以在maven打包時采用不同的profile,命令如下:
mvn clean package -Dmaven.test.skip=true -Ponline
通過P參數采用不同的profile,這樣可以實現為開發、測試、生產打出不同的包。
不過,不推薦這種打包方式,應該是對于開發、測試、生產打出一樣的包,然后根據機器本身的環境,來決定程序是按照那種環境來運行。
如果公司有根據環境變量的自動化部署方式(比如dev/test/stage/online),則這個profile是非常管用的。
# WebService
Java生態下的WebService框架非常多,apache cxf 是與spring結合最好的一種。配置步驟如下:
1、pom.xml,增加依賴:
~~~
1. dependency>??
2. ????groupId>org.apache.cxfgroupId>??
3. ????artifactId>cxf-rt-frontend-jaxwsartifactId>??
4. ????version>2.7.5version>??
5. dependency>??
6. dependency>??
7. ????groupId>org.apache.cxfgroupId>??
8. ????artifactId>cxf-rt-transports-httpartifactId>??
9. ????version>2.7.5version>??
10. dependency>??
~~~
2、web.xml,增加servlet: ??
~~~
1. ??
2. <servlet>??
3. ????servlet-name>cxfservlet-name>??
4. ????servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>??
5. ????load-on-startup>2load-on-startup>??
6. </servlet>??
7. <servlet-mapping>??
8. ????servlet-name>cxfservlet-name>??
9. ????url-pattern>/*url-pattern>??
10. </servlet-mapping>??
11. ??
~~~
3、resources目錄下,增加applicationContext-cxf.xml,內容如下:
~~~
1. <xml?version="1.0"?encoding="UTF-8"?>??
2. beans?xmlns="http://www.springframework.org/schema/beans"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
3. ?xmlns:jaxws="http://cxf.apache.org/jaxws"??
4. ?xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
5. ????http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd">??
6. ??
7. ?import?resource="classpath:META-INF/cxf/cxf.xml"?/>??
8. ?import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?/>?-->??
9. ??
10. ?jaxws:endpoint?implementor="#basicWebService"?address="/BasicWebService"?/>??
11. beans>??
~~~
4、BasicWebService來的內容大致如下:
~~~
1. @WebService(name?=?"BasicWebService",?serviceName?=?"BasicWebService",?portName?=?"BasicWebServicePort",?targetNamespace?=?"http://api.domain.com/ws")??
2. @Service??
3. public?class?BasicWebService?{??
4. ?@WebMethod??
5. ?public?void?sendHtmlMail(@WebParam(name?=?"headName")?String?headName,??
6. ???@WebParam(name?=?"sendHtml")?String?sendHtml)?{??
7. ??sendMail.doSendHtmlEmail(headName,?sendHtml);??
8. ?}??
9. }??
~~~
使用Apache CXF框架,是被Spring容器管理的,也就是說,BasicWebService本身可以設置@Service標記,也可以在BasicWebService中使用@Autowired進行注入。
而其他框架的WebService,比如Jboss直接通過Servlet方式暴露的WebService就不能這樣,只能通過一個SpringContextHolder手動從Spring容器中拿,大致如下:
1、首先在web.xml中增加WebService類的servlet,如下:
~~~
1. ??
2. servlet>??
3. ????servlet-name>BasicWebServiceservlet-name>??
4. ????servlet-class>com.xx.BasisWebServiceservlet-class>??
5. servlet>??
6. servlet-mapping>??
7. ????servlet-name>BasicWebServiceservlet-name>??
8. ????url-pattern>/BasicWebServiceurl-pattern>??
9. servlet-mapping>??
10. ????
~~~
2、BasicWebService的內容大致如下:
~~~
1. @WebService(name?=?"BasicWebService",?serviceName?=?"BasicWebService",?portName?=?"BasicWebServicePort",?targetNamespace?=?"http://api.sina.com/ws")??
2. public?class?BasicWebService?{??
3. ??
4. ????//這是從Spring容器中拿對象,SpringContextHolder是一個實現了org.springframework.context.ApplicationContextAware的類??
5. ????private?ISystemConfigService?systemConfigService?=?SpringContextHolder.getBean(ISystemConfigService.class);??
6. ??
7. ????@WebMethod??
8. ????public?String?test(@WebParam(name?=?"inputpara")?String?inputpara)?{??
9. ????????return?inputpara?+?"_100";??
10. ????}??
11. }??
~~~
# Redis
Spring可以簡化調用Redis的操作,配置大致如下:
1、pom.xml增加依賴:
~~~
1. groupId>org.springframework.datagroupId>??
2. artifactId>spring-data-redisartifactId>??
3. version>1.0.6.RELEASEversion>??
4. lt;/dependency>??
~~~
2、resources目錄下,增加applicationContext-redis.xml,內容如下:
~~~
1. xml?version="1.0"?encoding="UTF-8"?>??
2. beans?xmlns="http://www.springframework.org/schema/beans"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
3. ???????xmlns:context="http://www.springframework.org/schema/context"??
4. ???????xmlns:cache="http://www.springframework.org/schema/cache"??
5. ???????xmlns:p="http://www.springframework.org/schema/p"??
6. ???????xsi:schemaLocation="http://www.springframework.org/schema/beans??http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
7. ?http://www.springframework.org/schema/cache?http://www.springframework.org/schema/cache/spring-cache.xsd">??????
8. ????description>Spring-cachedescription>??
9. ????cache:annotation-driven/>??
10. ????bean?id="cacheManager"?class="org.springframework.data.redis.cache.RedisCacheManager">??
11. ????????constructor-arg?name="template"?index="0"?ref="redisTemplate"/>??
12. ????bean>??
13. ????bean?id="jedisPoolConfig"?class="redis.clients.jedis.JedisPoolConfig">??
14. ????????property?name="maxActive"?value="${redis.pool.maxActive}"/>??
15. ????????property?name="maxIdle"?value="${redis.pool.maxIdle}"/>??
16. ????????property?name="maxWait"?value="${redis.pool.maxWait}"/>??
17. ????????property?name="testOnBorrow"?value="${redis.pool.testOnBorrow}"/>??
18. ????bean>??
19. ??????
20. ????bean?id="jedisConnectionFactory"?class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">??
21. ????????property?name="hostName"?value="${redis.ip}"/>??
22. ????????property?name="port"?value="${redis.port}"/>??
23. ????????property?name="poolConfig"?ref="jedisPoolConfig"/>??
24. ????bean>??
25. ??????
26. ????bean?id="redisTemplate"?class="org.springframework.data.redis.core.RedisTemplate"?p:connection-factory-ref="jedisConnectionFactory">??
27. ????????property?name="keySerializer"?ref="stringRedisSerializer"/>??
28. ????bean>??
29. ????bean?id="stringRedisSerializer"?class="org.springframework.data.redis.serializer.StringRedisSerializer"/>??
30. beans>??
~~~
3、緩存寫入參考實現:
~~~
1. @Service??
2. public?class?BrandBaseServiceImpl?implements?IBrandBaseService?{??
3. ????@Override??
4. ????@Cacheable(value?=?CacheClientConstant.COMMODITY_BRAND_REDIS_CACHE,?key?=?"'commodity:webservice:all:brand:list'")??
5. ????public?List?getAllBrands()?{??
6. ?????try??
7. ?????{??
8. ??????List?brands?=?brandMapper.getAllBrands();??
9. ??????return?brands;??
10. ?????}?catch?(Exception?ex)??
11. ?????{??
12. ??????logger.error(ex.toString());??
13. ??????return?null;???????
14. ?????}??
15. ????}??
16. ????@Override??
17. ????@Cacheable(value?=?CacheClientConstant.COMMODITY_BRAND_REDIS_CACHE,?key?=?"'commodity:webservice:brand:no:'+#brandNo")??
18. ????public?Brand?getBrandByNo(String?brandNo)?{??
19. ????????if?(StringUtils.isBlank(brandNo))??
20. ????????????return?null;??
21. ????????return?brandMapper.getBrandByNo(brandNo);??
22. ????}??
23. }??
~~~
4、緩存清除參考實現:
~~~
1. @Service??
2. public?class?RedisCacheUtil?{??
3. ???
4. ?private?final?Logger?logger?=?LoggerFactory.getLogger(this.getClass());??
5. ????@Autowired??
6. ????private?RedisTemplate?redisTemplate;??
7. ????@Autowired??
8. ????private?JedisConnectionFactory?jedisConnectionFactory;??????
9. ????@CacheEvict(value?=?CacheClientConstant.COMMODITY_CATEGORY_REDIS_CACHE,?key?=?"'commodity:webservice:category:no:'+#categoryNo")??
10. ????public?void?cleanCatCacheByNo(String?categoryNo)??
11. ????{??
12. ?????List?keys?=?new?ArrayList();??
13. ????????logger.info("[商品服務端]清理分類categoryNo:{}緩存,REDIS?SERVER地址:{}",?categoryNo,?jedisConnectionFactory.getHostName()?+?":"?+?jedisConnectionFactory.getPort());??
14. ????????if?(StringUtils.hasText(categoryNo))?{??
15. ?????????keys.add("commodity:webservice:category:no:"?+?categoryNo);??
16. ????????????cleanAgain(keys);??
17. ????????}??
18. ????}??????
19. ????@CacheEvict(value?=?CacheClientConstant.COMMODITY_SYSTEMCONFIG_REDIS_CACHE,?allEntries?=?true)??
20. ????public?void?cleanSystemConfigAll()??
21. ????{??
22. ?????logger.info("[商品服務端]清楚SystemConfig緩存");??
23. ????}??????
24. ????/**?
25. ?????*?考慮到主從延遲可能會導致緩存更新失效,延遲再清理一次緩存?
26. ?????*?@param?keys?需要清除緩存的KEY?
27. ?????*/??
28. ????private?void?cleanAgain(List?keys)?{??
29. ????????if?(CollectionUtils.isEmpty(keys))?{??
30. ????????????return;??
31. ????????}??
32. ????????for?(String?key?:?keys)?{??
33. ????????????logger.info("清理緩存,KEY:{}",?key);??
34. ????????????redisTemplate.delete(key);??
35. ????????}??
36. ????}??
37. }????
~~~
# RabbitMQ
Spring也可以簡化使用RabbitMQ的操作,配置大致如下:
1、pom.xml增加依賴:
~~~
1. <dependency>??
2. ????groupId>org.springframework.amqpgroupId>??
3. ????artifactId>spring-amqpartifactId>??
4. ????version>${spring.amqp.version}version>??
5. </dependency>??
6. <dependency>??
7. ????groupId>org.springframework.amqpgroupId>??
8. ????artifactId>spring-rabbitartifactId>??
9. ????version>${spring.amqp.version}version>??
10. </dependency>??
~~~
2、發送消息代碼例子:
~~~
1. @Service??
2. public?class?MessageSendServiceImpl?implements?IMessageSendService?{???
3. ?private?static?final?String?EXCHANGE?=?"amq.topic";???
4. ?@Autowired??
5. ?private?volatile?RabbitTemplate?rabbitTemplate;???
6. ?private?final?Logger?logger?=?LoggerFactory.getLogger(this.getClass());??
7. ?@Override??
8. ?public?Boolean?sendMessage(String?commodityNo)?{??
9. ??Commodity?c?=?getCommodity(commodityNo);??
10. ??//?發送rabbitMQ消息(topic)??
11. ??rabbitTemplate.convertAndSend(EXCHANGE,?"commodity.update.topic",?c);??
12. ??logger.info("發送消息成功(topic):商品編號:"?+?commodityNo);??
13. ??return?true;??
14. ?}???
15. }??
~~~
3、resources目錄下,增加applicationContext-rabbitmq.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:rabbit="http://www.springframework.org/schema/rabbit" xmlns:task="http://www.springframework.org/schema/task"
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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定義rabbitmq連接工廠,生產環境使用集群配置,支持failover,rabbitmq.host=192.168.211.230:5672 -->
<rabbit:connection-factory id="connectionFactory" addresses="${rabbitmq.host}" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" channel-transacted="true"
message-converter="jsonMessageConverter" />
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter">
<property name="classMapper">
<bean class="org.springframework.amqp.support.converter.DefaultClassMapper">
</bean>
</property>
</bean>
<!--
兩種業務需求:
1. 同一個服務部署在多臺服務器上,如果想消息被一個服務收取,則要配置name,<rabbit:listener 里的queues=這里的name
2. 同一個服務部署在多臺服務器上,如果想消息被所有的服務收取,剛不要配置name,用rabbitmq自動創建的匿名name,這時要去掉這里的name屬性, 并且<rabbit:listener里的queues=這里的id
一般來說,都是第一種業務需求較多
-->
<rabbit:queue id="queue的id,可以和name一樣" name="queue的名字,在rabbitmq控制臺可以看到,例如commodity.update.topic.queue">
<rabbit:queue-arguments>
<entry key="x-ha-policy" value="all" />
</rabbit:queue-arguments>
</rabbit:queue>
<!-- CONSUMER -->
<!-- 這里的error-handler最好都配置,因為rabbitmq報的異常默認是不被捕獲的,如果這里沒有error-handler,log級別又沒指定到amqp的包,那么錯誤將不會被察覺 -->
<rabbit:listener-container connection-factory="connectionFactory" message-converter="jsonMessageConverter"
channel-transacted="true" error-handler="rabbitMqErrorHandler" concurrency="10"
auto-startup="true">
<rabbit:listener queues="rabbit:queue中定義的name或者id" ref="commodityUpdateListener" method="handleMessage" />
</rabbit:listener-container>
<rabbit:topic-exchange name="amq.topic" >
<rabbit:bindings>
<!-- 這里的queue是<rabbit:queue 里的ID -->
<rabbit:binding pattern="發送方的routingKey,對于上面的發送就是commodity.update.topic" queue="queue的名字,在rabbitmq控制臺可以看到,例如commodity.update.topic.queue"/>
</rabbit:bindings>
</rabbit:topic-exchange>
</beans>
~~~
4、接收消息代碼例子:
~~~
1. @Component??
2. public?class?CommodityUpdateListener?{??
3. ?public?void?handleMessage(Commodity?commodity)?{??
4. ??if(commodity==null)??
5. ??{??
6. ???logger.info("XXX");??
7. ???return;??
8. ??}??
9. ??//處理邏輯??
10. ?}??
11. }??
~~~
5、處理消息錯誤代碼例子:
~~~
1. @Component??
2. public?class?RabbitMqErrorHandler?implements?ErrorHandler?{??
3. ???
4. ?private?static?Logger?logger?=?LoggerFactory.getLogger(RabbitMqErrorHandler.class);??
5. ???
6. ?@Override??
7. ?public?void?handleError(Throwable?t)?{??
8. ??logger.error("Receive?rabbitmq?message?error:{}",?t);???
9. ?}??
10. }??
~~~
# MyBatis
Spring可以大大簡化使用MyBatis這種ORM框架,定義出接口和Mapper文件之后,Spring可以自動幫我們生成實現類。我曾經在DotNet框架下使用過MyBatis.Net,所有的Mapper的實現類都需要手工寫代碼,而Spring幫我節省了很多編碼工作量。
大致配置步驟如下:
1、pom.xml增加依賴:
~~~
1. dependency>??
2. ????groupId>org.mybatisgroupId>??
3. ????artifactId>mybatis-springartifactId>??
4. ????version>1.1.1version>??
5. dependency>??
6. dependency>??
7. ????groupId>org.mybatis.cachesgroupId>??
8. ????artifactId>mybatis-ehcacheartifactId>??
9. ????version>1.0.1version>??
10. dependency>??
~~~
2、resources目錄下,applicationContext.xml中,一般放置關于mybatis的配置,內容如下:
~~~
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<description>Spring公共配置</description>
<!--開啟注解 -->
<context:annotation-config />
<!-- 開啟自動切面代理 -->
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.xx">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 定義受環境影響易變的變量 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 標準配置 -->
<value>classpath*:/application.properties</value>
<value>classpath*:/config.properties</value>
<!-- 本地開發環境配置 -->
<value>file:/d:/conf/pcconf/*.properties</value>
<!-- 服務器生產環境配置 -->
<value>file:/etc/conf/pcconf/*.properties</value>
</list>
</property>
<!--property name="ignoreUnresolvablePlaceholders" value="true" / -->
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourcejdbc"/>
</bean>
<!-- 強烈建議用JdbcTemplate代替JdbcUtils -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourcejdbc" />
</bean>
<bean id="sqlSessionFactoryWrite" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourcejdbc" />
</bean>
<!-- 會自動將basePackage中配置的包路徑下的所有帶有@Mapper標注的Dao層的接口生成代理,替代原來我們的Dao實現。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactoryWrite" />
<property name="basePackage" value="com/xx/pc/template" />
</bean>
<beans profile="production">
<bean id="dataSourcejdbc" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/MySqlDS_JDBC" />
</bean>
</beans>
<beans profile="dev">
<bean id="dataSourcejdbc" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://ip:3306/dbname?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
</beans>
~~~
3、定義接口,及在src/main/resource對應接口的包路徑下定義同名的xml配置文件即可。
Spring初始化完畢后,會自動幫我們生成Mapper的實現類。