mybatis原生用法
sqlSession.selectOne(sqlId,參數(shù));
sqlSession.update(sqlId,參數(shù));
sqlSession.insert(sqlId,參數(shù));
sqlSession.delete(sqlId,參數(shù));
mybatis接口用法
1 namespace與接口全限定名一致
2 id和抽象函數(shù)保持一致
3 參數(shù)類型與返回類型保持一致
4 java類名與xml文件名保持一致
public interface GoodsMapper {
Goods selectGoodsById();
}
<select id="selectGoodsById" resultType="com.study.entity.Goods">
select * from goods where gid=3
</select>
GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
Goods goods = mapper.selectGoodsById();
mybatis-config.xml配置
properties
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///taobao?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456
<!-- 加載配置文件 -->
<properties resource="db.properties"></properties>
<!-- 通過${key}來使用 -->
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
settings配置
下面是配置log4j日志
<settings>
<!-- 日志的實現(xiàn)類,可以不寫,會自動匹配 -->
<setting name="logImpl" value="LOG4J"/>
<!-- 將數(shù)據(jù)庫字段的下劃線自動轉為駝峰命名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 自動映射,F(xiàn)ULL表示無論是否關聯(lián)都進行自動映射 -->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.gxy.mapper=TRACE //這里寫你自己的包
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
別名
<!-- 指定包下面的所有實體類都會將類名作為別名 -->
<typeAliases>
<package name="com.study.entity"/>
</typeAliases>
<!-- 使用的使用直接使用別名Goods,不用寫全限定名 -->
<select id="selectGoodsById" resultType="Goods">
select * from goods where gid=3
</select>
關聯(lián)
關聯(lián)結果resultMap與resultMap嵌套
public class Goods {
private GoodType goodsType;//屬性是pojo 普通的java對象
}
一對一
<select id="selectGoodsById" resultMap="goodsResultMap">
select * from goods g inner join data_dictionary d on g.good_type=d.value_id where d.type_code='good_type' and gid=3
</select>
<resultMap id="goodsResultMap" type="Goods">
<!-- 建議主鍵寫上,可以提高mybatis的效率 -->
<id property="gid" column="gid"/>
<!--關聯(lián)結果 association,只能關聯(lián)一個bean javaType:返回的結果類型 -->
<association property="goodsType" resultMap="goodTypeResultMap"/>
</resultMap>
<!-- 注意:這里要配置好自動映射autoMappingBehavior為FULL,配制后駝峰命名mapUnderscoreToCamelCase為true -->
<resultMap id="goodTypeResultMap" type="GoodType"></resultMap>
關聯(lián)resultMap相互嵌套
一對多
<select id="selectGorderByUid" resultMap="gorderResultMap">
SELECT * from gorder g inner join detail d on g.oid=d.oid where uid=#{value}
</select>
<resultMap id="gorderResultMap" type="Gorder">
<id property="oid" column="oid"/>
<collection property="detailList" ofType="Detail" >
<id property="did" column="did"/>
</collection>
</resultMap>
關聯(lián)查詢resultMap與select嵌套
一對一
<resultMap id="goodsResultMap" type="Goods">
<id property="gid" column="gid"/>
<association property="goodsType" select="selectGoodType" column="good_type" javaType="GoodsType"/>
</resultMap>
<select id="selectGoodsById" resultMap="goodsResultMap">
select * from goods where gid=3
</select>
<select id="selectGoodType" resultType="GoodsType">
select * from data_dictionary where type_code="good_type" and value_id=#{value}
</select>
關聯(lián)resultMap與select相互嵌套
一對多
<select id="selectGorderByUid" resultMap="gorderResultMap">
select * from gorder where uid=#{value}
</select>
<resultMap id="gorderResultMap" type="Gorder">
<id property="oid" column="oid"/>
<!-- select:調用另外一個查詢語句 column:傳遞給另外一個查詢語句使用的字段-->
<collection property="detailList" select="com.study.mapper.DetailMapper.selectDetailByOid" column="oid"/>
</resultMap>
動態(tài)sql
<select id="searchGoods" resultMap="goodsResultMap" parameterType="Goods">
select * from goods
<!-- where會自動去掉第一個and -->
<where>
<if test="gname!=null">
gname=#{gname}
</if>
<if test="goodsType.valueId!=null">
and good_type=#{goodsType.valueId}
</if>
</where>
</select>
set與if組合
update goods
<set>
<if test="gname!=null and gname!=''">
gname=#{gname}
</if>
<if test="goodsType.valueId!=null">
, good_type=#{goodsType.valueId}
</if>
</set>
where gid=#{gid}
trim與if的組合,可以代替where和set
update goods
<trim prefix="set" suffixOverrides=",">
<if test="gname!=null and gname!=''">
gname=#{gname},
</if>
<if test="goodsType.valueId!=null">
good_type=#{goodsType.valueId},
</if>
</trim>
select * from goods
<trim prefix="where" prefixOverrides="and | or">
<if test="gname!=null">
and gname=#{gname}
</if>
<if test="goodsType.valueId!=null">
and good_type=#{goodsType.valueId}
</if>
</trim>
foreach,一般用在in語句
select * from goods where gid in
<foreach collection="list" item="goods" open="(" close=")" separator=",">
#{goods}
</foreach>