1.导包.包括Mybatis的lib包,已经Mybatis的核心包,数据库驱动,就可以Mybatis测试,
2.配置文件
1.sqlConfig.xml是Mybatis的核心配置文件.
其中主要定义了对bean包下的类的扫描,起一个别名,别名即为类名,首字母大小写都可以识别.配置了Mappers,用来扫描Mapper.xml的配置文件位置.,
和数据库连接的配置在和Spring整合后可不配置,由Spring来连接数据库,管理事务.
settings属性为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>
<!--和Spring整合可不配置,由Spring来连接数据库 --> <!-- 读取数据库连接配置文件 --> <properties resource="jdbc.properties"></properties>
<settings> <!--所有映射器缓存配置的开关 --> <setting name="cacheEnabled" value="true"/> <!-- 延迟加载的全局开关 --> <setting name="lazyLoadingEnabled" value="false"/> <!-- <setting name="aggressiveLazyLoading" value=""/> --> </settings>
<!-- 配置bean实体类的别名 -->
<typeAliases>
<package name="cn.guanyq.bean"/>
</typeAliases>
<environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.jdbcUrl}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments>
<mappers>
<!--<mapper resource="cn/guanyq/sqlmap/User.xml"/> -->
<!-- <mapper resource="cn/guanyq/mapper/UserMapper.xml"/> -->
<package name="cn/guanyq/mapper"/>
</mappers>
</configuration>
2.使用Mapper接口开发.需要注意的四个要点.
a.Mapper接口必须和*Mapper.xml配置文件在同一包内,也就是同一个路径下.
b.接口内的方法名称必须和xml的配置文件id一致,执行方法时才会找到.
c. 接口的方法参数类型必须要和xml中的参数类型一致.parameterType.一致
d.接口的返回值类型必须和xml中的返回值类型一致. resultMap或resultType.
3.接口开发xml文件的配置.
一般接口的命名规则为类名加Mapper,方便区分.xml的命名为类名加上Mapper,比如UserMapper.java 和UserMapper.xml方便区分.接口不需要继承任何接口.
*Mapper.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">
<mapper namespace="cn.guanyq.mapper.UserMapper">
顶级元素
- cache缓存.cache-ref其他命名空间缓存的引用,已经不太使用
- resultMap 最强大的元素,用来描述如何从数据库结果集中加载对象,
- sql 代码块.可被其他语句引用的代码块,实现重用性
- insert
- delete
- update
- select
基本增删改查. #{ } 和 ${} 不会转义字符串.
<select id="getUserById" parameterType="Integer" resultType="User" statementType="PREPARED" flushCache="true" useCache="true" resultOrdered="true" timeout="1000"> select * from users where uid = #{uid} </select>
这是一个简单的查询语句 ,其中id要求和对应的接口的方法名称一致,parameterType是方法参数的别名,或者完整类名,可以是包装类.数组或者集合.resultType定义和返回的类型.如果是集合应该使用集合内装的类型.resultMap外部reslutMap的引用.这是对结果集的映射.resultMap和resultType不能同时使用.flushCache将其设置为true将会在任何时候只要语句被执行,本地的缓存将被清除.useCache,将其设置为True,将会导致select的结果被二级缓存.timeOut这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的时间.fetchSize这个值是设置驱动程序每次批量返回的结果的行数.statementType ,STATMENT,PREPARED和CALLABED三种,分别让Mybatis使用Statement,PreparedStatement和CallableStatement.
insert,update,delete基本属性相似.
如果主键是数据库自动生成,insert还支持将刚生成的主键返回.
<selectKey keyProperty="id" order="AFTER" resultType="Long"> select last_insert_id() </selectKey>
在insert语句之后插入上述代码.即可返回刚生成主键.
sql片段
<sql id="userColumns"> uid,uname </sql>
<select id="getUserById" parameterType="Integer" resultType="User" statementType="PREPARED" flushCache="true" useCache="true" resultOrdered="true" timeout="1000"> select <include refid="userColumns"></include> from users where uid = #{uid} </select>
可以这样来重复利用代码块
resultMap
resultMap一般用来映射多表查询的结果,如下代码,其中为User和Order两张表.
<resultMap type="User" id="getAllUserAndOrderList">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<collection property="orders" ofType="cn.guanyq.bean.Order">
<id property="oid" column="oid"/>
<result column="number" property="number"/>
<result column="price" property="price"/>
<association property="user" column="uid" javaType="User" resultMap="getUserResult"></association>
</collection>
</resultMap>
<resultMap type="User" id="getUserResult">
<id property="uid" column="uid"/>
<result column="uname" property="uname" />
</resultMap>
<select id="getAllUserAndOrderList" resultMap="getAllUserAndOrderList"> select * from users u left join orders o on u.uid=o.oid </select>
1.shouxianresultMap中的type表示的是返回值的结果类型.id是标识,如上面绿色,来调用这个resultMap
2.resultMap内的元素属性.
a.id和result
映射的基本内容,其中的基本属性property是映射到结果的字段或者属性,也可映射一些复杂的东西比如user.id.column:从数据库中得到的列名.
b.构造方法 constructor ,构造方法映射不太常用.
c.关联查询
<association property="user" javaType="cn.guanyq.bean.User">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
</association>
<resultMap type="User" id="getAllUserAndOrderList" >
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<collection property="orders" ofType="cn.guanyq.bean.Order" >
<id property="oid" column="oid"/>
<result column="number" property="number"/>
<result column="price" property="price"/>
<association property="user" column="uid" javaType="User" resultMap="getUserResult"></association>
</collection>
</resultMap>
<resultMap type="User" id="getUserResult">
<id property="uid" column="uid"/>
<result column="uname" property="uname" />
</resultMap>
适合多对一的映射,其中的javaType是对应的类型.其余一样,第二种可以提高代码的复用性.
d.集合.多对一.上面的collection元素.