Spring Boot Mybatis使用
Posted @carefree
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot Mybatis使用相关的知识,希望对你有一定的参考价值。
Mybatis
MyBatis的前身是Apache社区的一个开源项目iBatis,于2010年更名为MyBatis。MyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,避免了几乎所有的JDBC代码和手动设置参数以及获取结果集,使得开发人员更加关注SQL本身和业务逻辑,不用再去花费时间关注整个复杂的JDBC操作过程。
pom.xml 整合
<!--MyBatis配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
配置项
mybatis.config-location
mybatis.mapper-locations
mybatis.type-aliases-package
# mybatis config
mybatis.mapper-locations=classpath:mapper/*Dao.xml
注解
@Mapper
@MapperScan
@MapperScan("com.example.mybatis_demo.dao")
application.properties
server.port=9999
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/yanjing?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
# mybatis config
mybatis.mapper-locations=classpath:mapper/*Dao.xml
entity(bean)-> User.java
package com.example.mybatis_demo.entity;
import lombok.Data;
@Data
public class User
private Integer id;
private String name;
private String password;
dao(mapper)->UserDao.java
package com.example.mybatis_demo.dao;
import com.example.mybatis_demo.entity.User;
import java.util.List;
public interface UserDao
/**
* 查,返回数据列表
* @return
*/
List<User> findAllusers();
/**
* 增
* @return
*/
int insertUser(User user);
/**
* 改
* @return
*/
int updateUser(User user);
/**
* 删
* @return
*/
int delUser(Integer id);
编写sql语句
resources->mapper->UserDao.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.mybatis_demo.dao.UserDao">
<resultMap id="UserResult" type="com.example.mybatis_demo.entity.User">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
</resultMap>
<select id="findAllusers" resultMap="UserResult">
select * from user
</select>
<insert id="insertUser" parameterType="com.example.mybatis_demo.entity.User">
insert into user(name,password)
values (#name,#password)
</insert>
<update id="updateUser" parameterType="com.example.mybatis_demo.entity.User">
update user
set
name=#name,password=#password
where id = #id
</update>
<delete id="delUser" parameterType="int">
delete from user where id = #id
</delete>
</mapper>
控制器
controller->MybatisController.java
package com.example.mybatis_demo.controller;
import com.example.mybatis_demo.dao.UserDao;
import com.example.mybatis_demo.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class MybatisController
@Resource
UserDao userDao;
@GetMapping("/users/mybatis/queryall") //http://localhost:9999/users/mybatis/queryall
public List<User> queryAll ()
return userDao.findAllusers();
//新增一条数据 http://localhost:9999/users/mybatis/insert?name=assd&password=assd
@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 ;
//修改一条数据 http://localhost:9999/users/mybatis/update?id=2&name=update&password=update
@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.updateUser(user) > 0 ;
//删除一条数据 localhost:9999/users/mybatis/delete?id=3
@GetMapping("/users/mybatis/delete")
public Boolean delete(Integer id)
if(id == null || id < 1 )
return false;
return userDao.delUser(id) > 0;
启动类
package com.example.mybatis_demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.mybatis_demo.dao") //扫描dao层接口
@SpringBootApplication
public class MybatisDemoApplication
public static void main(String[] args)
SpringApplication.run(MybatisDemoApplication.class, args);
controller层拦截用户请求,entity(bean)层定义对象,dao(mapper)层声明方法,UserDao.xml实现SQL语句,完成操作数据库的目的。
使用mybatis实现数据库的增删改查!
参考:https://edu.csdn.net/learn/26258/328801
以上是关于Spring Boot Mybatis使用的主要内容,如果未能解决你的问题,请参考以下文章