配置优化
-
将数据源的配置放在properties文件中
在mybatis的配置文件中使用properties引入数据库的配置文件
在数据源指定属性的位置使用类似el表达式替换数据 ${} -
批量注册dao层
在mapper标签中指定package 会自动扫描包下面的所有接口 -
实体类别名
typeAliases中配置实体类的别名
mybatis具有内置的别名
统一起别名 指定某一个包下面的实体类都支持别名
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/emp
username=root
password=123456
mybatis-config.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 resource="db.properties"></properties>
<!--相关的设置-->
<settings>
<!--mybatis log4j的日志 -->
<setting name="logImpl" value="LOG4J" />
</settings>
<!--给实体类定义别名-->
<typeAliases>
<!--别名的规则是类名-->
<!--单独定义别名-->
<!--<typeAlias type="com.igeek.entity.Dept" alias="Dept"></typeAlias>
<typeAlias type="com.igeek.entity.Emp" alias="Emp"></typeAlias>-->
<!--统一定义类名-->
<package name="com.igeek.entity"></package>
</typeAliases>
<!--所有的数据源
default指定默认的数据源 填入数据源的ID
-->
<environments default="mysqlDataSource">
<!--单个数据源-->
<environment id="mysqlDataSource">
<!--事务类型 -->
<transactionManager type="JDBC"></transactionManager>
<!--数据连接池-->
<dataSource type="POOLED">
<!--数据源的4个配置 驱动,地址,用户名,密码-->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 注册dao层 -->
<mappers>
<!--扫描dao层-->
<package name="com.igeek.dao"></package>
</mappers>
</configuration>
高级使用
实体类映射
resultMap
-
配置resultMap
a.id代表当前表的ID(不是必须的)
b.result指定 column代表表的列字段 property代表需要映射的java的属性 -
应用resultMap
指定查询的结果集的resultMap属性,匹配resultMap的ID -
resultMap如果表中的列名和java中的字段名是同名,支持直接映射,可以不写映射,如果不一样一定要写映射
-
resultMap一旦使用了就可以不用resultType
一对多和多对一(二次查询,连表映射)
-
不带外键查询
1.改造javabean,将外键字段改造成实体对象
2.映射集如果bean中是实体对象的话,可以采用 对象.属性 的方式映射到对象内部 -
多对一 使用 association 二次映射
1.改造javabean,将外键字段改造成实体对象
2.使用标签association 标签一定要放在result的后面 collection的前面
3.property 代表javabean中的 复合Bean的名称 column代表二次查询的条件 select代表二次查询语句(全路径) javaType 二次查询返回的结果类型
4.二次查询参数一定要统一,类型一定要匹配(只能要覆盖就可以) -
一对多 一方查询多条记录 使用collection
1.改造bean,在bean中增加多条记录的字段 一定是List
2.collection 放在最后面(association的后面)
3.property 代表javabean中的 List集合的名称 column代表二次查询的条件(无外键,一般都是当前表的主键) select代表二次查询语句(全路径) ofType 二次查询返回的List中的泛型 -
注意点
1.避免递归出现 尽量不要使用association 自身查询别的对象可以使用
2.不能滥用association和collection,会降低查询效率 根据实际情况自己选择(association,left join)
CURD注意点
- DML没有返回值 void 不需要配置resultMap或者resultType
- 多参数 当参数类型无法确定的时候 统一使用map 参数匹配使用 @Param(“匹配参数的key”)
- 设置自动提交事务为true sessionFactory.openSession(true)
- 参数是复杂类型,要保证匹配类型中的字段的get set方法
- mybatis不支持复杂加简单
- 当数据库有自增主键,insert 可以通过设置 useGeneratedKeys=“true” keyProperty=“接收自增主键的名称” 回填自增主键
- mybatis在insert update 的时候如果你插入null值有可能会出现错误 要使用jdbcType指定数据库类型
示例
IEmpDao.xml
<?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">
<!-- 指定当前配置文件的接口文件 namespace来指定 全路径-->
<mapper namespace="com.dao.IEmpDao">
<!--mybatis的映射集-->
<resultMap id="empIDMap" type="Emp">
<result column="HIREDATE" property="hie"></result>
<!--不带二次查询映射实体关系 -->
<result column="MGR" property="mgr.empno"></result>
<result column="DEPTNO" property="dept.deptno"></result>
</resultMap>
<resultMap id="empFKMap" type="Emp">
<result column="HIREDATE" property="hie"></result>
<!--映射二次查询 (映射实体bean) 多对一关系 -->
<association property="dept" column="deptno"
select="com.dao.IDeptDao.findByID" javaType="Dept">
</association>
<association property="mgr" column="mgr"
select="com.dao.IEmpDao.findByID" javaType="Emp">
</association>
</resultMap>
<!--指定我的sql语句
id 代码接口中方法名
resultType 返回值的类型(泛型)
parameterType 参数类型
-->
<select id="findAllEmp" resultType="Emp">
select * from emp
</select>
<select id="findByID" parameterType="int" resultMap="empIDMap">
SELECT * FROM emp where EMPNO = #{empno}
</select>
<select id="findByFK" parameterType="int" resultMap="empFKMap">
SELECT * FROM emp where EMPNO = #{empno}
</select>
<select id="findEmpByDept" parameterType="int" resultMap="empIDMap">
SELECT * FROM emp where DEPTNO = #{deptno}
</select>
</mapper>
IDeptDao.xml
<?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">
<!-- 指定当前配置文件的接口文件 namespace来指定 全路径-->
<mapper namespace="com.dao.IDeptDao">
<resultMap id="deptFKMap" type="Dept">
<collection property="emps" column="deptno"
select="com.dao.IEmpDao.findEmpByDept" ofType="Emp"></collection>
</resultMap>
<resultMap id="deptIDMap" type="Dept">
</resultMap>
<select id="findAllDept" resultType="Dept">
select * from dept
</select>
<select id="findByID" parameterType="int" resultMap="deptFKMap">
select * from dept where DEPTNO = #{deptno}
</select>
<insert id="insert" parameterType="map">
insert into dept (DEPTNO, DNAME, LOC)values (#{deptno,jdbcType=INTEGER},#{dname,jdbcType=VARCHAR},#{loc,jdbcType=VARCHAR})
</insert>
<insert id="insertDept" parameterType="Dept">
insert into dept (DEPTNO, DNAME, LOC)values (#{deptno,jdbcType=INTEGER},#{dname,jdbcType=VARCHAR},#{loc,jdbcType=VARCHAR})
</insert>
</mapper>
IStudentDao.xml
<?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">
<!-- 指定当前配置文件的接口文件 namespace来指定 全路径-->
<mapper namespace="com.dao.IStudentDao">
<insert id="insert" parameterType="com.igeek.entity.Student" useGeneratedKeys="true" keyProperty="id">
INSERT INTO student (name, age)VALUES(#{name},#{age})
</insert>
</mapper>