mybatis深入
在mybatis-config中配置 properties标签 加载properties配置文件
<!--指定properties路径 加载properties文件-->
<properties resource="db.properties"></properties>
开启驼峰式命名法的自动映射(注意位置一定不能在properties配置文件之上)
<settings>
<!--开启下划线到驼峰式命名法的自动映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--是否启用缓存 默认为true-->
<setting name="cachEnabled" value="true"></setting>
<!--是否启用懒加载(延迟加载) 多对象关联-->
<setting name="lazyLoadingEnabled" value="false"></setting>
<!--是否积极加载所有属性-->
<setting name="aggresiveLazyLoading" value="true"></setting>
<!--是否使用自动生成主键-->
<setting name="useGeneratedKeys" value="true"></setting>
<!--配置mybatis日志输出-->
<setting name="logImpl" value="LOG4J"></setting>
</settings>
类型起别名(位置不能出现在setting配置文件之上)
这个别名一般用在mapper映射文件上的返回值类型过长时
<!--类型起别名-->
<typeAliases>
<typeAlias type="com.xpc.pojo.ProductType" alias="pt"></typeAlias>
</typeAliases>
实体类上面加 @Alias(value=“别名”) 类单独起别名
单独起的别名优先级高于统一起的别名
@Alias(value = "productType")
public class ProductType {
private int id;
private String typeName;
private String status;
是否积极加载所有属性(前提 log4j日志输出)
配置日志输出
1.引入jar包 log4j.jar
log4j.rootLogger=TRACE, stdout
log4j.logger.com.xpc.mapper=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p
3.修改mapper映射文件位置(注意文件的层级)
4.修改mybatis-config中的mappers
<!--通过加载核心配置 加载所有相关的sql文件-->
<mappers>
<mapper resource="com/xpc/mapper/ProductTypeMapper.xml"/>
</mappers>
当参数为两个时 需要用@Param(“typeName”)起别名
//通过typeName 和 status查询
public List<ProductType> findProductType(@Param("typeName") String typeName, @Param("status") int status);
sqlMapper映射文件
返回结构为resultType="map"时 timeout(超时间)
<!--返回map结构 一个对象Map 多个对象List<Map>-->
<select id="find" resultType="map" timeout="6000">
select * from product_type,product where product_type.id =product.type_id
</select>
动态sql
//动态where标签 if标签
<select id="findProductType" resultType="ProductType">
select * from product_type
<where>
<if test="typeName != null">type_name=#{typeName}</if>
and status=#{status}
</where>
</select>
//动态set标签 动态if标签
<update id="updateProductType">
update product_type
<set>
<if test="typeName != null">type_name =#{typeName},</if>
<if test="status != 0">status=#{status}</if>
</set>
where id=#{id}
</update>
//批量新增
<insert id="saveBatch">
insert into product_type(type_name,status)
values
<foreach collection="list" item="type" separator=",">
(#{type.typeName},#{type.status})
</foreach>
</insert>
insert update delete 一般不用resultType 一般都用paramenterType(参数类型) separator(以逗号分隔)
databaseId 数据库厂商标识 flishCache =“true” 清除缓存
statementType
//只能在insert里面使用 返回自动生成的主键
useGeneratedKeys="true" keyProperty="id"
mybatis常见的核心类
1.Resources
2.SqlSessionFactoryBuilder
3.SqlSessionFactory
4.SqlSession
jdbc操作:
Statement : 拼接sql
PreparedStatement : 预编译 ?参数占位符 防止sql注入攻击,提高效率
Mybatis对jdbc封装:
#{obj.status} 取数据 ====> 预编译模式PreparedStatement
statementType="PREPARED" 一般情况下都是用预编译 #{}
statementType="STATEMENT" 当表名或者列名需要动态传入时,选择STATEMENT 拼接sql ${}
statementType="CALLABLE" 调用存储过程时使用
statementType="PREPARED" //一般选择预编译 拼接sql #{}
statementType="STATEMENT"//当表名或者列名需要动态传入时选择STATEMENT 拼接sql ${}
statementType="CALLABLE" //调用存储过程是使用
通过注解实现sql
在接口中进行实现
查询时使用@Select 如:
添加时使用 @Insert
更新是使用@Update
删除时是@Delete
//查找所有的
@Select("select * from product_type")
public List<ProductType> findAll();
除了简单sql语句 一般不会使用注解模式
对象关联关系
多对一
//多对一 一对一
private ProductType type;
mapper接口
public interface ProductMapper {
public List<Product> findList();
}
mapper映射文件(多对一使用association,autoMapping="true"属性自动映射)
<?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">
<!--sql mapper 映射文件-->
<!--namespace:命名空间 唯一的,不能重复并且要求接口的全名(包名.类名)保持一致-->
<mapper namespace="com.xpc.mapper.ProductMapper">
<select id="findList" resultMap="productMap">
select p.*,t.id as tid,t.type_name,t.status
from product p join product_type t
</select>
<!--autoMapping="true" 列的属性的自动匹配映射-->
<resultMap id="productMap" type="Product" autoMapping="true">
<id column="tid" property="id"></id>
<!--多对一映射-->
<association property="type" javaType="ProductType" column="type_id">
<id property="id" column="id"></id>
</association>
</resultMap>
</mapper>
一对多
//一对多
private List<Product> productList;
<!--一对多关联查询 查找产品分类信息(关联产品)-->
<select id="findProductList" resultMap="typeMap">
select t.id as tid, t.type_name,t.status,p.* from
product_type t left outer join product p
on t.id = p.type_id
</select>
<!--id:唯一 type:返回对象的类型-->
<resultMap id="typeMap" type="ProductType">
<!--id 定义主键匹配关系 column:列名 property:类型属性名-->
<id column="tid" property="id"></id>
<!---->
<result column="type_name" property="typeName"></result>
<result column="status" property="status"></result>
<!--集合 一对多关系-->
<collection property="productList" column="type_id" ofType="Product">
<id column="id" property="id"></id>
<result column="title" property="title"></result>
<result column="intro" property="intro"></result>
<result column="type_id" property="typeId"></result>
</collection>
</resultMap>
当一对多时
<collection property="productList" column="type_id" ofType="Product">
//column指的是外键列
//productList中的数据来自于Product 所有使用ofType
不管是一对多还是多对一都是相对来说的
比如:一个产品类型可以对应多种产品
多种产品属于同一种产品类型
lection>
[外链图片转存中...(img-mL00xxMO-1591365497355)]
当一对多时
```xml
<collection property="productList" column="type_id" ofType="Product">
//column指的是外键列
//productList中的数据来自于Product 所有使用ofType
不管是一对多还是多对一都是相对来说的
比如:一个产品类型可以对应多种产品
多种产品属于同一种产品类型