pom.xml引入mybstis:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
两种方式使MyBatis解析Mapper类
1.使用@Mapper标签,使其扫描当前定义Mapper
2.在Application的启动类中使用@MapperScan=(value=“com.pc.myfisrtspringboot.mapper”)
注解版
Mapper类:
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
// 允许jdbc支持自动生成主键
@Options(useGeneratedKeys = true,keyProperty = "id")
// @Insert("insert into department(id,departmentName) values (#{id},#{departmentName})")
// id自增长,不需要传入
@Insert("insert into department(departmentName) values (#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
Controller中直接调用Mapper方法,在实际开发中应使用MVC三层模式
@Autowired
DepartmentMapper departmentMapper;
@GetMapping("/dept/{id}")
public Department getDepartment(@PathVariable("id") Integer id) {
return departmentMapper.getDeptById(id);
}
@GetMapping("/dept/insert")
public Department insertDepartment(Department department) {
departmentMapper.insertDept(department);
return department;
}
http请求URL:
查询:http://localhost:8888/dept/1
新增:http://localhost:8888/dept/insert?departmentName=C%20dept
更新:http://localhost:8888/dept/update?id=4&departmentName=D%20dept
删除:http://localhost:8888/dept/delete?id=7
注解版配置数据库表字段和自定义类字段映射(department_name到departmentName):
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
在编写过程中,还不得不复习了sql语句的写法…
ALTER TABLE department ADD PRIMARY KEY (id);
ALTER TABLE department MODIFY id INTEGER AUTO_INCREMENT;
ALTER TABLE department RENAME COLUMN departmentName TO department_name;
xml配置版
yml文件配置:
mybatis:
// MyBatis中各项配置所在位置
config-location: classpath:mybatis/mybatis-config.xml
// MyBatis中Mapper.xml所在位置
mapper-locations:
- classpath:mybatis/mapper/*.xml
上面使用的@Mapper标签,xml使用的是@MapperScan:
@MapperScan(value="com.pc.myfisrtspringboot.mapper")
@SpringBootApplication
public class MyfisrtspringbootApplication {
......
}
Mapper类:
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public int insertEmp(Employee employee);
}
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.pc.myfisrtspringboot.mapper.EmployeeMapper">
<!-- namespace与Mapper类的全路径对应 -->
<select id="getEmpById" resultType="com.pc.myfisrtspringboot.entity.Employee">
select * from employee where id = #{id}
</select>
<!-- id与Mapper类放入方法名对应 -->
<insert id="insertEmp">
insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{dId})
</insert>
</mapper>
Q:这里测试时犯了一个错误,inserEmp方法在xml中的标签开始时使用的是 select 标签,在执行时是没有返回值的,但是Mapper类中inserEmp方法返回类型是int,最后进行数据插入时,数据保存成功,但是返回结果时报错。
A:将标签改为 insert 标签 ,或将Mapper类中inserEmp方法返回类型改为void
同理,Employee的Controller类:
@Autowired
EmployeeMapper employeeMapper;
@GetMapping("/emp/{id}")
public Employee getEmployee(@PathVariable("id") Integer id) {
return employeeMapper.getEmpById(id);
}
@GetMapping("/emp/insert")
public Employee getDepartment(Employee employee) {
employeeMapper.insertEmp(employee);
return employee;
}
http请求URL:
查询:http://localhost:8888/emp/1
新增:http://localhost:8888/emp/insert?email=123@qq.com&lastName=san&dId=1
xml版配置数据库表字段和自定义类字段映射(d_id到dId):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
详细配置内容可以查看MyBatis的官方文档:https://mybatis.org/mybatis-3/zh/configuration.html