什么是 MyBatis ?
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
引入依赖
在pom文件引入mybatis-spring-boot-starter的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
引入数据库连接依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
引入数据源
spring.datasource.url=jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&connectTimeout=5000&socketTimeout=5000&characterEncoding=UTF-8&useConfigs=maxPerformance&prepStmtCacheSqlLimit=1024 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
创建数据库表
建表语句:
CREATE TABLE `t_user` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '25');
INSERT INTO `account` VALUES ('2', 'bbb', '25');
INSERT INTO `account` VALUES ('3', 'ccc', '25');
具体实现
这篇文篇通过注解的形式实现。
创建实体:
package org.springboot.sample.domain; public class User { private Long id; private String name; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
dao层
package org.springboot.sample.domain; import java.util.List; import org.apache.ibatis.annotations.*; @Mapper public interface UserMapper { @Insert("insert into t_user(name, age) values(#{name}, #{age})") int add(@Param("name") String name, @Param("age") int age); @Update("update t_user set name = #{name}, age = #{age} where id = #{id}") int update(@Param("name") String name, @Param("age") int age, @Param("id") int id); @Delete("delete from t_user where id = #{id}") int delete(int id); @Select("select id, name as name, age as age from t_user where id = #{id}") User findUser(@Param("id") int id); @Select("select id, name as name, age as age from t_user") List<User> findUserList(); }
service层
package org.springboot.sample.service; import java.util.List; import org.springboot.sample.domain.User; import org.springboot.sample.domain.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public int add(String name, int age) { return userMapper.add(name, age); } public int update(String name, int age, int id) { return userMapper.update(name, age, id); } public int delete(int id) { return userMapper.delete(id); } public User findUser(int id) { return userMapper.findUser(id); } public List<User> findUserList() { return userMapper.findUserList(); } }
controller层,构建restful API
package org.springboot.sample.web; import java.util.List; import org.springboot.sample.domain.User; import org.springboot.sample.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @Autowired UserService userService; @RequestMapping(value = "/list", method = RequestMethod.GET) public List<User> getUsers() { return userService.findUserList(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User getUserById(@PathVariable("id") int id) { return userService.findUser(id); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String updateUser(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "age", required = true) int age) { int t = userService.update(name, age, id); if (t == 1) { return "success"; } else { return "fail"; } } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable(value = "id") int id) { int t = userService.delete(id); if (t == 1) { return "success"; } else { return "fail"; } } @RequestMapping(value = "", method = RequestMethod.POST) public String postUser(@RequestParam(value = "name") String name, @RequestParam(value = "age") int age) { int t = userService.add(name, age); if (t == 1) { return "success"; } else { return "fail"; } } }
通过postman测试通过。
源码下载:https://gitee.com/zhmal/spring-boot-samples/tree/master/spring-boot-sample-mybatis