MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
DO( Data Object):与数据库表结构一一对应,通过DAO层向上传输数据源对象。
DO 对象和普通的 POJO 类并无不同,唯一要注意的点是属性类型要和数据库类型进行匹配。
创建包。
创建接口(此接口需要添加一个@Mapper
的注解)。
完成 MyBatis DAO 的定义后,Spring 启动的时候会自动加载这个接口并动态创建一个 Spring Bean。
@Mapper
public interface ExampleDAO {
public List<ExampleDO> findAll();
}
findAll方法:
public List<ExampleDO> findAll();
查询多条记录的时候,请使用List
做为返回类型。
@Mapper
public interface ExampleDAO {
@Select("SELECT id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified FROM user")
List<ExampleDO> findAll();
}
@Controller
public class ExampleController {
@Autowired
private ExampleDAO exampleDAO;
@GetMapping("/examples")
@ResponseBody
public List<ExampleDO> getAll() {
return exampleDAO.findAll();
}
}
insert方法:
int insert(ExampleDO exampleDO);
?insert方法介绍:
执行 SQL 插入语句的时候,会返回插入行数,一般成功是返回 1。(判断插入是否成功可以通过看返回值)。
@Mapper
public interface ExampleDAO {
int insert(ExampleDO exampleDO);
@Select("SELECT id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified FROM user")
List<ExampleDO> findAll();
}
@Mapper
public interface ExampleDAO {
@Insert("INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified) VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())")
int insert(ExampleDO exampleDO);
@Select("SELECT id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified FROM user")
List<ExampleDO> findAll();
}
value 值换成了?#{变量名}
?
VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())
??#{userName}
?实际上就是执行userDO.getUserName()
这个方法来获取?userName
?变量值,MyBatis 会自动更新生成正式的 SQL 语句去到数据库里执行。
@PostMapping("/example")
@ResponseBody
public ExampleDO save(@RequestBody ExampleDO exampleDO) {
exampleDAO.insert(exampleDO);
return exampleDO;
}
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
?useGeneratedKeys————>设置为 true,代表允许数据库使用自增主键
keyColumn————>设置表的主键字段名称,一般都是id
keyProperty————>设置DO模型的主键字段
update方法:
int update(ExampleDO exampleDO);
?DAO代码:
@Mapper
public interface ExampleDAO {
@Insert("INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified) " +
"VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
int insert(ExampleDO exampleDO);
@Select("SELECT id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified FROM user")
List<ExampleDO> findAll();
int update(ExampleDO exampleDO);
}
@Update("update user set nick_name=#{nickName},gmt_modified=now() where id=#{id}")
完整代码:
@Mapper
public interface ExampleDAO {
@Insert("INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified) " +
"VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
int insert(ExampleDO exampleDO);
@Select("SELECT id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified FROM user")
List<ExampleDO> findAll();
@Update("update user set nick_name=#{nickName},gmt_modified=now() where id=#{id}")
int update(ExampleDO exampleDO);
}
@PostMapping("/example/update")
@ResponseBody
public ExampleDO update(@RequestBody ExampleDO exampleDO) {
exampleDAO.update(exampleDO);
return exampleDO;
}
delete方法:
int delete(long id);
为了能够在 SQL 语句中完成普通参数解析,还需要对参数增加一个注解@Param
?
public interface ExampleDAO {
int delete(@Param("id") long id);
}
@Delete
?注解?@Delete("delete from user where id=#{id}")
#{id} 中的 id 要和 @Param("id") 中的 id 一样
完整代码:
@Mapper
public interface ExampleDAO {
@Insert("INSERT INTO user (user_name, pwd, nick_name,avatar,gmt_created,gmt_modified) " +
"VALUES(#{userName}, #{pwd}, #{nickName}, #{avatar},now(),now())")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
int insert(ExampleDO exampleDO);
@Select("SELECT id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified FROM user")
List<ExampleDO> findAll();
@Delete("delete from user where id=#{id}")
int delete(@Param("id") long id);
}
@GetMapping("/example/del")
@ResponseBody
public boolean delete(@RequestParam("id") Long id) {
return exampleDAO.delete(id) > 0;
}
返回值大于0,则删除成功 ,反之则无
ExampleDO findByUserName(@Param("exampleName") String name);
public interface ExampleDAO {
@Select("select id,user_name as userName,pwd,nick_name as nickName,avatar,gmt_created as gmtCreated,gmt_modified as gmtModified from user where user_name=#{userName} limit 1")
ExampleDO findByExampleName(@Param("exampleName") String name);
}
@GetMapping("/example/findByExampleName")
@ResponseBody
public ExampleDO findByExampleName(@RequestParam("exampleName") String exampleName) {
return exampleDAO.findByExampleName(exampleName);
}