yaml配置文件
- 连库信息
mysql连库信息中,url拼接的参数:useUnicode=true&characterEncoding=utf-8指定字符的编码、解码格式;
例如:MySQL数据中使用的编码是GBK;而项目中使用的是utf-8;
-
存数据时.
数据库中存放项目中输入的数据时,会用UTF-8格式将数据解码成字节码,然后再将解码后的字节码重新使用GBK编码将数据存储到数据库中; -
取数据时:
在从数据库中取数据的时候,数据库会先将数据用GBK解码成字节码,然后再将字节码用UTF-8格式编码数据,最后再将数据返回给客户端;serverTimezone时区=Asia/Shanghai
- 配置mybatis
- 配置mybatis的映射路径mapper-locations
classpath*:支持后面的表达式使用通配符;
**代表多级目录
mapper-locations支持配置数组, - classpath为数组中的一个元素,指定xml文件的位置
mybatis:
mapper-locations:
- classpath:mapper/*.xml
- classpath*:com/**/mapper/*.xml
- 扫描mapper包
@Configuration
@MapperScan("com.example.learning01.mbg.mapper")
public class MybatisConfig {
}
mybatis逆向工程
- 在mybatis-generator配置文件中:
<javaModelGenerator/>、<sqlMapGenerator/>、<javaClientGenerator/>
这三个的targetProject的相对路径配置规则是:
相对于工作空间的相对路径,而在Idea中每个独立项目就是一个工作空间,所以,这里的相对路径要配置成项目的文件所在位置路径。
<!--指定生成model的路径-->
<javaModelGenerator targetPackage="com.example.learning01.mbg.model" targetProject="src\main\java"/>
<!--指定生成mapper.xml的路径-->
<sqlMapGenerator targetPackage="com.example.learning01.mbg.mapper" targetProject="src\main\resources"/>
<!--指定生成mapper接口的的路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.learning01.mbg.mapper"
targetProject="src\main\java"/>
mybatis分页插件
- 在pom文件中引入PageHelper插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
- 配置分页工具类
//常用
private Integer pageNum;
//常用
private Integer pageSize;
private Integer pageTotal;
private Long total;
private List<T> list;
//配置分页对象,查询结果通过调用restPage来实现分页 list为查询结果集
public static <T> CommonPagebak<T> restPage(List<T> list){
CommonPagebak<T> result = new CommonPagebak<T>();
PageInfo<T> pageInfo = new PageInfo<T>(list);
result.setPageTotal(pageInfo.getPages());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setTotal(pageInfo.getTotal());
result.setList(pageInfo.getList());
return result;
}
- service层启用分页插件
@Override
public List<PmsBrand> listBrand(int pageNum, int pageSize) {
//开启分页
PageHelper.startPage(pageNum,pageSize);
return pmsBrandMapper.selectByExample(new PmsBrandExample());
}
- 控制层返回分页结果
@GetMapping(value = "/list")
public CommenResult<CommonPagebak<PmsBrand>> listBrand(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize) {
List<PmsBrand> result = pmsBrandService.listBrand(pageNum,pageSize);
return CommenResult.success(CommonPagebak.restPage(result));
}
- 常见错误
```java
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.learning02.dao.Dao.getAllList
异常信息以及很明确了,是mapper里的接口和xml语句的id没有匹配上,这个时候需要检查
- 接口的方法名和xml语句的id是否一致;
- xml文件的命名空间是否和mapper的包路径一致;
- spring是否扫描了mapper的路径;
Field Mapper in com.macro.mall.tiny.service.impl.ServiceImpl required a bean of type 'com.macro.mall.tiny.mbg.mapper.Mapper' that could not be found.
这个异常时因为没有扫描dao,
@Configuration
@MapperScan({"com.a.mapper","com.b.dao"})
public class MyBatisConfig {
}
方便的idea小插件
为了方便从dao层至今调到xml语句中,我安装了MybatisPlus,非常方便,安装后重启idea就可以看到在左侧有个小图标,点击后就跳到对应的文件(dao或xml)中了
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="dao所在工程路径">
<resultMap id="resultMap(定义的resultmap的id)" type="映射实体的工程路径" autoMapping="true">
autoMapping=true开启结果集映射
主键
<id column="id" jdbcType="BIGINT" property="id" />
一对多
<collection property="(一对多中,多的属性名)" columnPrefix="attr_" ofType="(一对多中,多映射的实体)">
主键
<id column="id" property="id" jdbcType="BIGINT"/>
属性,column对应数据库中的名字(数据库字段名称或者是别名),property对应实体中的名字
<result column="name" property="name"/>
</collection>
</resultMap>
<select id="对应dao中方法的名字" resultMap="resultMap(定义的resultmap的id)">
<resultMap id="resultmap的id" type="实体工程路径" autoMapping="true">
<id column="id" jdbcType="BIGINT" property="id" />
</resultMap>
<select id="getByCollections" resultMap="resultmap的id">
select * from table
where 1=1
collection填写dao层的入参
item为具体参数
<foreach collection="ids" item="id">
<if test="id!=null">
or id = #{id}
</if>
</foreach>
</select>