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>