标签:callsettersonnulls mybatis dtd configurationpropert settings
<?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"> <!-- 如果内网机器报错,请使用下面这种笨办法 --> <!-- <!DOCTYPE configuration PUBLIC "-//www.mybatis.org//DTD Config 3.0//EN" "E:/bao/tomcat/apache-tomcat-6.0.14/webapps/pmsys/WEB-INF/classes/mybatis/mybatis-3-config.dtd"> --> <configuration> <settings> <!-- 只设置需要的,其他使用默认值 --> <!-- 开启缓存,默认就是开启的,2层开关,需要在Mapper文件中也指定 cache 标签才会真正使用缓存 --> <setting name="cacheEnabled" value="true"/> <!-- 在null时也调用 setter,适应于返回Map,3.2版本以上可用 --> <setting name="callSettersOnNulls" value="true"/> </settings> </configuration>
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> { private static final Log logger = LogFactory.getLog(SqlSessionFactoryBean.class); // 这里可以配置configLocation资源 private Resource configLocation; private Resource[] mapperLocations; private DataSource dataSource; private TransactionFactory transactionFactory; // 这里可以配置configurationProperties属性 private Properties configurationProperties; ...... protected SqlSessionFactory buildSqlSessionFactory() throws IOException { Configuration configuration; XMLConfigBuilder xmlConfigBuilder = null; // 先查找 configLocation 属性 if (this.configLocation != null) { xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties); configuration = xmlConfigBuilder.getConfiguration(); } else { if (logger.isDebugEnabled()) { logger.debug("Property ‘configLocation‘ not specified, using default MyBatis Configuration"); } // 如果找不到configLocation,就只使用 configurationProperties configuration = new Configuration(); configuration.setVariables(this.configurationProperties); } ......
<!-- myBatis配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 表示在mybatis.mapping包或以下所有目录中,以 Mapper.xml结尾所有文件 --> <property name="mapperLocations"> <value>classpath:com/cncounter/dao/oracle/**/*Mapper.xml</value> <!-- <list> <value>classpath:com/cncounter/dao/oracle/res/*Mapper.xml</value> <value>classpath:com/cncounter/dao/oracle/user/*Mapper.xml</value> </list> --> </property> <!-- <property name="configLocation"> <value>classpath:mybatis/mybatis-env-setting.xml</value> </property> --> <!-- 切换一种方式,不配置configLocation --> <property name="configurationProperties"> <props> <prop key="cacheEnabled">true</prop> <prop key="callSettersOnNulls">true</prop> </props> </property> </bean>
MyBatis与Spring设置callSettersOnNulls
标签:callsettersonnulls mybatis dtd configurationpropert settings
原文地址:http://blog.csdn.net/renfufei/article/details/39646207