mybatis-config.xml 包含的內容如下
注意元素節點的順序!順序不對會報錯
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
其中dataSource 資料來源(共三種內建的資料來源型別)
type="[UNPOOLED|POOLED|JNDI]")
<transactionManager type="[ JDBC | MANAGED ]"/>
主要用於找到sql語句的檔案在哪裡?可以使用不同的方式參照sql語句 具體的參照方式如下
<!-- 使用相對於類路徑的資源參照 -->
<mappers>
<mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
<!-- 使用完全限定資源定位符(URL) -->
<mappers>
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>
<!--
使用對映器介面實現類的完全限定類名需要組態檔名稱和介面名稱一致,並且位於同一目錄下
-->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>
<!--
將包內的對映器介面實現全部註冊為對映器.但是需要組態檔名稱和介面名稱一致,並且位於同一目錄下
-->
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
**mapper組態檔
主要用用關聯dao介面中的方法,並書寫sql語句 相當於實現了介面中的各個方法
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiezhr.dao.UserMapper">
<select id="getUserList" resultType="com.xiezhr.pojo.User">
select * from mybatis.user;
</select>
<insert id="addUser" parameterType="com.xiezhr.pojo.User">
insert into mybatis.user values(#{id},#{name},#{pwd})
</insert>
<update id="updateUserById" parameterType="int">
update mybatis.user set name='小頭爸爸' where id=#{id}
</update>
<delete id="deleteUserById" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
</mapper>
具體步驟如下
(1)編寫db.properties 檔案
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8
username=root
password=123456
(2)在mybatis核心組態檔中加在外部組態檔來連線資料庫
<?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">
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/xiezhr/Dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
(1)自定義javabean別名
<typeAliases>
<typeAlias type="com.xiezhr.pojo.User" alias="user"/>
</typeAliases>
如上設定之後就可以在任何地方用user 代替 com.xiezhr.pojo.User設定別名也可按照下面方式設定
(2)設定所有com.xiezhr.pojo 包下的Javabean別名為小寫的類名
<typeAliases>
<package name="com.xiezhr.pojo"/>
</typeAliases>
通過上述設定之後,以下的xml即等價
<select id="getUserList" resultType="user">
select * from mybatis.user;
</select>
<select id="getUserList" resultType="com.xiezhr.pojo.user">
select * from mybatis.user;
</select>
設定常用的有如下幾個
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>