//SqlMapConfig.xml的配置内容和顺序如下(顺序不能乱):
Properties(属性)
Settings(全局参数设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境信息集合)
environment(单个环境信息)
transactionManager(事物)
dataSource(数据源)
mappers(映射器)
//SqlMapConfig配置
## Mapper映射文件
* CRUD标签
``` java
//select – 书写查询sql语句
select中的几个属性说明:
id属性:当前名称空间下的statement的唯一标识(必须属性);
resultType:将结果集映射为java的对象类型。必须(和 resultMap 二选一)
parameterType:传入参数类型。可以省略
//insert
insert 的几个属性说明:
id:当前名称空间下的statement的唯一标识(必须属性);
parameterType:参数的类型,使用动态代理之后和方法的参数类型一致
useGeneratedKeys:开启主键回写
keyColumn:指定数据库的主键
keyProperty:主键对应的pojo属性名
标签内部:具体的sql语句
//update
id属性:当前名称空间下的statement的唯一标识(必须属性);
parameterType:传入的参数类型,可以省略。
标签内部:具体的sql语句
//delete
delete 的几个属性说明:
id属性:当前名称空间下的statement的唯一标识(必须属性);
parameterType:传入的参数类型,可以省略。
标签内部:具体的sql语句
//2、pojo类型 User login(User user);
//3、List
int insertForeach(List
//4、Map //遍历
//直接取key,value User login(Map<String,String> user); //username和password为map的key
* 输出映射
* 1)resultType:类型的全限定名,不要使用基本类型,用对应的包装类型代替
* 需要查询出的列名和映射的对象的属性名一致,才能映射成功,可用sql列别名解决字段与列名不一致问题
* 2)resultMap:
``` java
<resultMap id="BaseResultMap" type="work.icql.sample.pojo.User">
<id column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
</resultMap>
//使用 resultMap 代替 resultType
<select id="login" resultMap="BaseResultMap">
select * from tb_user where user_name = #{user.username,jdbcType=VARCHAR} and password = #{user.password,jdbcType=VARCHAR}
</select>
//if标签
//where标签:主要是用来简化sql语句中where条件判断的书写的,会自动将其后第一个条件的and或者是or给忽略掉
//foreach标签
* 高级结果映射:https://www.cnblogs.com/selene/p/4627446.html
``` java
//一对一,一对多
public class Order {
private List<OrderDetail> detailList;
}
//一对一association,一对多collection
<resultMap id="OrderUserDetailResultMap" type="Order" autoMapping="true">
<id column="id" property="id"/>
<association property="user" javaType="com.zpc.mybatis.pojo.User">
<id column="user_id" property="id"/>
<result column="password" jdbcType="VARCHAR" property="password" />
</association>
<collection property="detailList" javaType="List" ofType="OrderDetail">
<id column="id" property="id"/>
<result column="password" jdbcType="VARCHAR" property="password" />
</collection>
</resultMap>
<select id="queryOrderWithUserAndDetailByOrderNumber" resultMap="OrderUserDetailResultMap">
select * from tb_order o
left join tb_user u on o.user_id=u.id
left join tb_orderdetail od on o.id=od.order_id
where o.order_number = #{number}
</select>
//多对多,省略
```
一级缓存,默认开启,sqlsession级别
二级缓存,默认不开启(不建议使用),mapper级别