properties元素由两种方式实现。
第一种方式是通过外部指定的方式
在这里我创建了一个database.properties内容为
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/*** 数据库名称
username = *** 账号
password = *** 密码
核心配置文件配置 properties获取了我上面创建的database.properties内容
<?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="database.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/ElectronicMapper.xml"/>
</mappers>
</configuration>
第二种方式直接配置成为XML
<?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>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/***"/> //数据库名称
<property name="username" value="****"/> //数据库登陆账号
<property name="password" value="****"/> //数据库登陆密码
</properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></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>
<mappers>
<mapper resource="dao/UserMapper.xml"/>
</mappers>
</configuration>
typeAliases元素的作用是配置类型别名
这样说很模糊不是很明白,举个梨子来说明一下。
//Mapper文件示例:
<?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="dao.ElectronicMapper">
<select id="getElectronic" resultType="entity.Electronic">
SELECT * FROM `electronic` WHERE `factoryDate` > "2015-01-01"
</select>
</mapper>
这段代码表示我要从数据库中查询一条 类型为Electronic的数据。所以resultType为entity.Electronic类型
这时我在核心配置文件中添加了typeAliases元素为
<properties resource="database.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<!-- 给指定的类添加别名 -->
<typeAlias alias="abcd" type="entity.Electronic"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/ElectronicMapper.xml"/>
</mappers>
</configuration>
这就是说所有类型为entity.Electronic的数据我都可以使用abcd来代替,当然这个abcd是可以随意写的最好是见名知意命名
<?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="dao.ElectronicMapper">
<select id="getElectronic" resultType="abcd">
SELECT * FROM `electronic` WHERE `factoryDate` > "2015-01-01"
</select>
</mapper>
我的Mapper中把resultType修改成 abcd。运行了代码还是一样可以执行的。
还有一种更简易的方法就是直接指定包名,MyBatis会自动扫描指定包下的JavaBean,并默认设置一个别名。并且它们都是大小写不敏感的。
<properties resource="database.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
//这里package的属性name值是你定义的包名
<package name="entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/ElectronicMapper.xml"/>
</mappers>
</configuration>
通过package name=“entity” Mybatis会自动扫描我这个包下的所有JavaBean,以Electronic为例
这个时候我只需要在我的Mapper文件中把resultType修改为Electronic就可以了。
<?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="dao.ElectronicMapper">
<select id="getElectronic" resultType="Electronic">
SELECT * FROM `electronic` WHERE `factoryDate` > "2015-01-01"
</select>
</mapper>
MyBatis中的Mapper元素表示的映射器,用来定义SQL的映射语句,我们只需要告诉MyBatis去哪里找到这些SQL语句。
<properties resource="database.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
//这里package的属性name值是你定义的包名
<package name="entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
//方式一:使用类资源路径获取资源,也就是说你的包名和XML名称
<mapper resource="dao/ElectronicMapper.xml"/>
//方式二:使用URL获取资源,也就是说通过你Mapper.XML文件放置的磁盘路径
<mapper url="file:///E:/sqlmappers/ElectronicMapper.xml"/>
</mappers>
</configuration>