标签:config pen from mybatis div local 一个 learn type
<select id="selectPerson" parameterType="int" parameterMap="deprecated" resultType="hashmap" resultMap="personResultMap" flushCache="false" useCache="true" timeout="10000" fetchSize="256" statementType="PREPARED" resultSetType="FORWARD_ONLY" databaseId="mysql">
下面的解释中有一个databaseId属性: 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。新增,修改和删除都有这个属性
<databaseIdProvider type="DB_VENDOR"> <property name="MySQL" value="mysql" /> <property name="Oracle" value="oracle" /> </databaseIdProvider>
public interface DatabaseIdProviderMapper { String selectTime(); }
<mapper namespace="com.yihaomen.mybatis.dao.DatabaseIdProviderMapper"> <select id="selectTime" resultType="String" databaseId="mysql"> SELECT NOW() FROM dual </select> <select id="selectTime" resultType="String" databaseId="oracle"> SELECT ‘oralce‘||to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) FROM dual </select> </mapper>
<mappers> <mapper resource="com/yihaomen/mybatis/model/DatabaseIdProvider.xml"/> </mappers>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis-learn?characterEncoding=utf8 jdbc.username=root jdbc.password=tiger #jdbc.driver=oracle.jdbc.driver.OracleDriver #jdbc.url=jdbc:oracle:thin:@localhost:1521:mybatis #jdbc.username=mybatis #jdbc.password=mybatis maxActive= 50
import com.yihaomen.mybatis.dao.DatabaseIdProviderMapper; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import user.BaseTest; public class DatabaseIdProviderTest extends BaseTest{ public static void main(String[] args) { SqlSessionFactory factory = getSession(); SqlSession session = factory.openSession(); DatabaseIdProviderMapper mapper = session.getMapper(DatabaseIdProviderMapper.class); System.out.println(mapper.selectTime()); } }
[1] 杨开振 著,《深入浅出MyBatis技术原理与实战》, 电子工业出版社,2016.09
[2]火柴盒zhang博客,http://blog.csdn.net/likewindy/article/details/51396576
mybatis-databaseIdProvider多数据库支持
标签:config pen from mybatis div local 一个 learn type
原文地址:http://www.cnblogs.com/happyflyingpig/p/7689289.html