(1)封装了JDBC大部分操作,减少了开发人员的工作量
(2)半自动化的操对于编写SQL语句灵活度更高
(3)Java代码与SQL语句分离,降低维护难度
(4)自动映射结果集,减少重复的编码工作
(5)开源社区十分活跃,文档齐全,学习成本低
在pom.xml文件中添加MyBatis的场景启动器mybatis-spring-boot-starter
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
配置Mapper文件对应的XML文件路径
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
在启动类中添加对Mapper包的扫描@MapperScan,Spring Boot在启动的时候会自动加载包路径下的Mapper接口。代码如下所示:
@SpringBootApplication
@MapperScan("ltd.newbee.mall.dao") //添加@Mapper注解
public class NewbeeMallApplication {
//启动spring应用
public static void main(String[] args) {
SpringApplication.run(NewbeeMallApplication.class, args);
}
}
也可以直接在每个Mapper接口上面添加@Mapper注解。但是如果Mapper接口数量较多,在每个Mapper接口上加注解是比较烦琐的。
SQL语句如下:
DROP TABLE IF EXISTS tb_user;
CREATE TABLE tb_user (
id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',
name varchar(100) NOT NULL DEFAULT '' COMMENT '登录名',
password varchar(100) NOT NULL DEFAULT '' COMMENT '密码',
PRIMARY KEY (id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8;
insert into tb_user (id,name,password)
value (1,'C','123456'),
(2,'C++','123456'),
(3,'Java','123456'),
(4,'Python','123456'),
(5,'R','123456'),
(6,'C#','123456');
根据建的数据库一一对应在entity包下创建一个实体类User中,代码如下:
public class User {
private Integer id;
private String name;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
新建dao包并在dao包中新建UserDao接口,并定义增删改查四个方法,代码如下:
package ltd.newbee.mall.dao;
import ltd.newbee.mall.entity.User;
import java.util.List;
public interface UserDao {
/**
* 返回数据列表
*
* @return
*/
List findALLUsers();
/**
* 添加
*
* @param User
* @return
*/
int insertUser(User user);
/**
* 修改
*
* @param User
* @return
*/
int updUser(User user);
/**
* 删除
*
* @param id
* @return
*/
int delUser(Integer id);
}
在resources目录下新建mapper目录,并在mapper目录下新建Mapper接口的映射文件UserMapper.xml,再进行映射文件的编写。
insert into tb_user(name, password) VALUES (#{name},#{password})
update tb_user set name=#{name},password=#{password} where id=#{id}
delete from tb_user where id=#{id}
package ltd.newbee.mall.controller;
import ltd.newbee.mall.dao.UserDao;
import ltd.newbee.mall.entity.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class MyBatisController {
@Resource
UserDao userDao;
//查询所有记录
@GetMapping("/users/mybatis/queryALL")
public List queryALL(){
return userDao.findALLUsers();
}
//新增一条记录
@GetMapping("/users/mybatis/insert")
public Boolean insert(String name,String password){
if (StringUtils.isEmpty(name)||StringUtils.isEmpty(password)){
return false;
}
User user = new User();
user.setName(name);
user.setPassword(password);
return userDao.insertUser(user) > 0;
}
//修改一条记录
@GetMapping("/users/mybatis/update")
public Boolean update(Integer id,String name,String password){
if (id==null||id<1||StringUtils.isEmpty(name)||StringUtils.isEmpty(password)){
return false;
}
User user = new User();
user.setId(id);
user.setName(name);
user.setPassword(password);
return userDao.updUser(user) >0;
}
//删除一条记录
@GetMapping("/users/mybatis/delete")
public Boolean delete(Integer id){
if (id==null||id<1){
return false;
}
return userDao.delUser(id) >0;
}
}
http://localhost:8080/users/mybatis/insert?name=quiz&password=1234567
返回true
数据库新增成功
http://localhost:8080/users/mybatis/delete?id=7
返回true
数据库删除成功
http://localhost:8080/users/mybatis/update?id=2&name=update&password=654321
返回true
数据库修改成功
http://localhost:8080/users/mybatis/queryALL
返回下图