Mybatis 框架总结之一
##1、引入依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
##2、编写配置文件—官方文档查询
<?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="mysql.properties"></properties>
<settings>
<!-- 开启懒加载方式 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--给每个pojo起个别名,在映射文件里就不用写类的全路径了-->
<!-- <typeAliases>-->
<!-- <typeAlias alias="product" type="com.itheima.pojo.Product"></typeAlias>-->
<!-- <typeAlias alias="category" type="com.itheima.pojo.Category"></typeAlias>-->
<!-- </typeAliases>-->
<!-- 批量起别名,该包下的所有类的别名为类名首字母小写-->
<typeAliases>
<package name="com.itheima.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- dataSource: 数据源(数据库连接池)配置
type="POOLED" : 数据源的类型配置
POOLED :使用mybatis的自带数据源配置
UNPOOLED: 不使用数据源配置, 使用Connection操作数据库
JNDI:JNDI服务 数据源配置
-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com/itheima/mappers"/>
<!-- 加载传统Dao开发使用的映射文件-->
<!-- <mapper resource="com/itheima/mappers/ProductMapper.xml"/>-->
<!-- <mapper resource="com/itheima/mappers/CategoryMapper.xml"></mapper>-->
<!-- <mapper resource="com/itheima/com.itheima.mappers/RolesMapper.xml"></mapper>-->
<!-- <mapper resource="com/itheima/com.itheima.mappers/UserMapper.xml"></mapper>-->
<!-- 加载动态代理使用的映射文件*/-->
<!-- <mapper resource="ProductMapper.xml"></mapper>-->
</mappers>
</configuration>
主要以下部分
标签名 | 作用 |
---|---|
properties | 加载数据库配置文件如:mysql.properties |
settings | 设置懒加载或者二级缓存 |
typealis | 起别名,映射文件不用写类全路径 |
environments | 设置数据源,事务管理 |
mappers | 映射文件加载器,加载映射文件 |
##编写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="ProductMapper">
<cache></cache>
<select id="findAll" resultType="product">
select * from product;
</select>
<select id="findOne" resultType="product" parameterType="java.lang.Integer" useCache="true">
select * from product where pid = #{pid}
</select>
<!-- 动态sql语句
<if></if><where></where><foreach></foreach>
-->
<select id="findByCriteria" resultType="product" parameterType="product">
select * from product where 1=1
<if test="#{pdesc}!=null">
and pdesc like "%"#{pdesc}"%"
</if>
<if test="#{is_hot}!=null">
and is_hot = #{is_hot}
</if>
</select>
<select id="findByCriteria2" parameterType="product" resultType="product">
select * from product
<where>
<if test="#{pdesc}!=null">
and pdesc like "%"#{pdesc}"%"
</if>
<if test="#{is_hot}!=null">
and is_hot = #{is_hot}
</if>
</where>
</select>
<select id="findByCid" parameterType="java.lang.Integer" resultType="product">
select * from product where cid = #{cid};
</select>
<update id="updateByPid" parameterType="product">
<!--#{}会被解析成占位符?,update product set pname = ?,is_hot = ? where pid = ? 安全可以防注入,对String型自动加""-->
update product set pname = #{pname},is_hot = #{is_hot} where pid = #{pid}
</update>
<delete id="deleteByPid" parameterType="java.lang.String">
delete from product where pid = #{pid}
</delete>
<delete id="deleteByPid2" parameterType="java.util.List">
delete from product where
<foreach collection="array" open="pid in (" close=")" separator="," item="pid">
#{pid}
</foreach>
</delete>
</mapper>~
主要有以下部分
标签名 | 作用 |
---|---|
if、where、forearch | 拼接动态sql语句 |
resultMap | 结果集映射 |
association | 一对一映射 |
collection | 一对多映射(多对多映射是两个一对多) |
resultype | 返回值类型 |
parameterType | 参数类型:基本类型+String+object+集合/数组(可以不写,也可以写 成java.lang.List) |