一.整合mybatis
和springBoot
步骤:
创建模块
配置application.xml
不用在写mybatis-config.xml
文件,将数据的配置信息写在application.xml
文件里面。
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/manager
username: root
password: root
mybatis:
//允许使用别名
type-aliases-package: com.lzh.mybatis_springboot.model
configuration:
//开启驼峰命名
map-underscore-to-camel-case: true
// 映射文件路径
mapper-locations: classpath:mapper/*.xml
实体类
@Data
public class Dept implements Serializable {
private Integer deptNo;
private String dName;
private String loc;
private List<Emp> empList;
private static final long serialVersionUID = 1L;
}
映射接口
package com.lzh.mybatis_springboot.mapper;
import com.lzh.mybatis_springboot.model.Dept;
import com.lzh.mybatis_springboot.model.Emp;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptMapper {
int deleteByPrimaryKey(Long id);
int insert(Dept record);
int insertSelective(Dept record);
Dept selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(Dept record);
int updateByPrimaryKey(Dept record);
List<Dept> selectAll2();
}
方法一:
在接口上加上@Mapper注解
(1).标识接口:通过接口上使用@Mapper
注解,告诉Mybatis
这个接口是一个Mapper
接口,它定义了与数据库表交互的方法。
(2).自动生成实现:Mybatis
会自动生成接口方法的实现,这些实现包括了与数据库的交互逻辑,如SQL
语句的生成,参数的传递,结果的映射等。这样,就不需要手动编写Mapper
接口的实现。
(3).当使用SpringBoot
时,@Mapper
注解也会告诉Spring
容器将Mapper
接口注册为Spring
的bean
,使其可以通过依赖注入(@Autowired)
来在应用程序中使用。
方法二:
在启动类上添加@MapperScan
注解
@SpringBootApplication
@MapperScan("com.kfm.mybatis_springboot.mapper")
public class MybatisSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisSpringBootApplication.class, args);
}
}
@MapperScan
:
(1).扫描指定包:通过@MapperScan
注解中指定要扫描的包名,Spring
会自动查找该包下的所有Mapper
接口.
(2).注册Mapper
接口:当找到Mapper接口时,Spring
会将注册为Spring
的bean
,以便可以在应用程序的其他组件中通过依赖注入(@Autowires)
来使用。
(3).避免手动配置:使用 @MapperScan
注解可以避免手动在Spring的配置类中一个一个地注册Mapper
接口。它能够减少配置的繁琐性,提高开发效率。
service
在service层加上注解@Component
和@Autowired
@Component
:Springboot
启动就可以创建该注解标注的类的对象
@Autowired
:将自动创建的对象赋值给标注的属性值
import com.lzh.mybatis_springboot.mapper.DeptMapper;
import com.lzh.mybatis_springboot.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class DeptService {
@Autowired
DeptMapper deptMapper;
public List<Dept> findAll2(){
return deptMapper.selectAll2();
}
}
测试类
加上注解@SpringBootTest
和@Autowired
二.关联查询
1.一对一查询
设置.xml
文件
<resultMap id="BaseResultMap" type="com.lzh.mybatis_springboot.model.Emp">
<result property="empNo" column="emp_no" jdbcType="INTEGER"/>
<result property="eName" column="e_name" jdbcType="VARCHAR"/>
<result property="job" column="job" jdbcType="VARCHAR"/>
<result property="mgr" column="mgr" jdbcType="INTEGER"/>
<result property="hirdate" column="hirdate" jdbcType="TIMESTAMP"/>
<result property="sal" column="sal" jdbcType="DOUBLE"/>
<result property="comm" column="comm" jdbcType="DOUBLE"/>
<result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
<association property="dept" javaType="dept">
<result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
<result property="dName" column="d_name" jdbcType="VARCHAR"/>
<result property="loc" column="loc" jdbcType="VARCHAR"/>
</association>
</resultMap>
例:一个员工对应一个部门(emp
表除了员工信息外,还有部门的相关信息(dept
表中对应的列))
association
标签表示关联查询
association
标签的property
属性表示resultMap
标签中type
指定的类的属性名称。
association
标签的javaType
属性表示属性中存储的类型
要在emp
表中创建dept
表实体类的对象
2.一对多查询
设置.xml
文件
<resultMap id="BaseResultMap" type="com.lzh.mybatis_springboot.model.Dept">
<result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
<result property="dName" column="d_name" jdbcType="VARCHAR"/>
<result property="loc" column="loc" jdbcType="VARCHAR"/>
<collection property="empList" ofType="emp">
<result property="empNo" column="emp_no" jdbcType="INTEGER"/>
<result property="eName" column="e_name" jdbcType="VARCHAR"/>
<result property="job" column="job" jdbcType="VARCHAR"/>
<result property="mgr" column="mgr" jdbcType="INTEGER"/>
<result property="hirdate" column="hirdate" jdbcType="TIMESTAMP"/>
<result property="sal" column="sal" jdbcType="DOUBLE"/>
<result property="comm" column="comm" jdbcType="DOUBLE"/>
<result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
</collection>
</resultMap>
collectionn
标签用于建立一对多中集合属性的对应关系,加载关联的集合对象
ofType
属性用于指定集合元素的数据类型
property
属性指定属性名
例:一个部门有多个员工
在dept
实体类中创建emp
集合,把部门里面的员工封装成集合