springboot-5-持久层技术
整合mybatis
流程:
导入依赖:
除了mybaits还有mysql和jdbc依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
配置数据源信息(两个选择),前者为jdbc数据源,后者为druid数据源
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/ssm_crud
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
---------------------------
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/ssm_crud
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
还要配置一些mybaits的基本配置,比如:
- 别名配置
- mapper的xml文件位置
- 其他
mybatis.type-aliases-package=com.wang.pojo
# mybatis.mapper-locations=classpath:mapper/*.xml
写好pojo类,后面些数据访问层需要用
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Integer employeeId;
private String employeeName;
private String gender;
private String email;
private Department department;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer departmentId;
private String departmentName;
}
在dao包下写好mapper接口
@Mapper //或者在主程序下添加@MapperScan("包位置")
@Repository
public interface EmployeeMapper {
//查询全部员工
List<Employee> getAllEmployee();
//通过id查找员工
Employee getEmployeeById(Integer id);
//添加员工
Integer addEmployee(Employee employee);
//更新员工
Integer updateEmployee(Employee employee);
//删除员工
Integer deleteEmployee(Integer id);
}
mapper接口对应的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="com.wang.dao.DepartmentMapper">
<resultMap id="departmentMap" type="department">
<id column="department_id" property="departmentId"/>
<result column="department_name" property="departmentName"/>
</resultMap>
<!--查询全部department-->
<!--List<Department> getAllDepartment();-->
<select id="getAllDepartment" resultMap="departmentMap">
select * from tbl_department
</select>
<!--查询特定id的department-->
<!--Department getDepartmentById(Integer id);-->
<select id="getDepartmentById" resultMap="departmentMap">
select * from tbl_department where department_id = #{id}
</select>
</mapper>
测试使用:
@SpringBootTest
public class Test {
@Autowired
DepartmentMapper departmentMapper;
@org.junit.jupiter.api.Test
public void test(){
System.out.println(departmentMapper.selectByPrimaryKey(1));
}
}
多数据源开发:
怎么让springboot知道这个是一个mybatis下的mapper接口?
-
在mapper上加上@Mapper
@Mapper public interface UseMapper { }
-
在主程序上加上@MapperScan("mapper接口放置的包")
在这样spring就会直接去扫描这个包下的接口作为mapper
mapper接口需要放到IOC容器中去,故需要加上@Repository
测试数据库连接是否成功
@SpringBootTest
class DemoApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getConnection());
}
}