文章目录
前言
MyBatis 是一个持久层与数据库进行交互的框架。它支持自定义 SQL、存储过程以及高级映射。
一、使用步骤
1.通过Idea工具构建项目
- 在界面可以选择对应的依赖会自动导入。本示例使用的JDK1.8和Maven。
Mybatis也可以手动导入依赖:
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
- 在yml文件添加数据源配置。
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://192.168.0.101/test01?useUnicode=yes&characterEncoding=UTF8
- 创建一个对象实体类。
package com.example.entity;
public class TestObj {
private Integer id;
private String name;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "TestObj{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
- 创建Mapper接口类。
package com.example.mapper;
import com.example.entity.TestObj;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface EmpMapper {
List<TestObj> select();
TestObj selectEmpById(Integer id);
Integer add(TestObj testObj);
Integer update(TestObj testObj);
Integer delete(Integer id);
}
- 在resources下创建mapper目录并创建TextObj.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.example.mapper.TestObjMapper">
<select id="selectObj" resultType="TestObj">
select * from test20
</select>
<select id="selectById" resultType="TestObj">
select * from test20 where id = #{id}
</select>
<insert id="add" parameterType="TestObj">
insert into test20 (name,sex) values (#{name},#{sex})
</insert>
<update id="update" parameterType="TestObj">
update test20 set name=#{name} where id = #{id}
</update>
<delete id="delete" parameterType="int">
delete from test20 where id = #{id}
</delete>
</mapper>
- 在yml文件添加MyBatis配置。
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.entity
- 创建Controller类。
package com.example.controller;
import com.example.entity.TestObj;
import com.example.mapper.TestObjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
private TestObjMapper testObjMapper;
@GetMapping("/list")
public List<TestObj> test20List() {
List<TestObj> maps = testObjMapper.selectObj();
return maps;
}
@GetMapping("/add")
public String addInfo() {
testObjMapper.add(new TestObj("wangwu", "2"));
return "success";
}
@GetMapping("/update/{id}")
public String update(@PathVariable("id") Integer id) {
String name = "list";
testObjMapper.update(new TestObj(id, "dbing", "5"));
return "update success";
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
testObjMapper.delete(id);
return "delete success";
}
}
-
错误信息。
启动报错:
Invalid bound statement (not found): com.example.mapper.TestObjMapper.add] with root cause
要注意这里的 mapper-locations不要搞成了这个config-locations 否则就会报上面错误。
mybatis:
type-aliases-package: com.example.entity
mapper-locations: classpath:mapper/*.xml -
测试结果打印
修改访问:http://localhost:8889/update/3
查询访问:http://localhost:8889/list 打印 [{“id”:3,“name”:“dbing”,“sex”:“2”}]
总结
MyBatis框架有优点灵活性比较高、降低了与代码之间的耦合度、配置比较规范。缺点是配置错了有时候排查起来比较费劲、编写sql比较繁琐。