Kite的学习历程之SpringBoot之数据访问整合MyBatis
2020-06-26 00:18
525 查看
Kite学习框架的第十九天
1 使用注解整合MyBatis
1.1 首先创建一个要进行操作的数据库
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for department -- ---------------------------- DROP TABLE IF EXISTS `department`; CREATE TABLE `department` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `departmentName` VARCHAR(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; SET FOREIGN_KEY_CHECKS=0;
1.2 创建一个数据库对应的实体类
package cn.kitey.mybatis.bean; public class Department { private Integer id; private String departmentName; public Department() { } public Department(Integer id, String departmentName) { this.id = id; this.departmentName = departmentName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department{" + "id=" + id + ", departmentName='" + departmentName + '\'' + '}'; } }
1.3 创建一个Mapper包,并创建一个DepartmentMapper接口,并指定为mapper
在类上添加一个
@Mapper
指定扫面这一个类
package cn.kitey.mybatis.mapper; import cn.kitey.mybatis.bean.Department; import org.apache.ibatis.annotations.*; /** * 指定这是一个操作的据库的mapper */ //@Mapper public interface DepartmentMapper { @Select("select * from department where id=#{id}") public Department getDeptById(Integer id); @Select("delete from department where id#{id}") public int deleteDeptById(Integer id); @Options(useGeneratedKeys = true, keyProperty = "id") @Insert("insert into department(departmentName) values(#{departmentName})") public int insertDept(Department department); @Update("update department set departmentName=#{departmentName} where id={id }") public int updateDept(Department department); }
在主程序上加一个
@MapperScan(value = “cn.kitey.mybatis.mapper”)
表示扫描整个mapper包下
package cn.kitey.mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan(value = "cn.kitey.mybatis.mapper") @SpringBootApplication public class Demo06BootDataMybatisApplication { public static void main(String[] args) { SpringApplication.run(Demo06BootDataMybatisApplication.class, args); } }
1.4 创建一个控制类
通过自动注入的方法注入
@Autowired
DepartmentMapper departmentMapper;
package cn.kitey.mybatis.controller; import cn.kitey.mybatis.bean.Department; import cn.kitey.mybatis.bean.Employee; import cn.kitey.mybatis.mapper.DepartmentMapper; import cn.kitey.mybatis.mapper.EmployeeMapper; 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; @RestController public class DeptController { @Autowired DepartmentMapper departmentMapper; @GetMapping("/dept/{id}") public Department getDepartment(@PathVariable("id") Integer id){ return departmentMapper.getDeptById(id); } @GetMapping("/dept") public Department insertDept(Department department){ departmentMapper.insertDept(department); return department; }
1.5 在网页地址栏进行访问
这个是进行数据的插入
还测试了一个进行数据查询
我们再看一下我数据库的内容
也只有之两条数据
以上就是基于注解的SpringBoot整合Mybatis
相关文章推荐
- Kite的学习历程之SpringBoot之数据访问整合SpringData JPA
- 学习springboot笔记(四)数据访问之mybatis
- Spring boot之数据访问及mybatis整合(三)
- SpringBoot (九) :数据访问springboot整合mybatis_一点课堂(多岸学院)
- spring boot 数据访问2 整合mybatis,pagehelper分页,多数据源事务处理
- BabyLog SpringBoot+MyBatis+Thymeleaf+Amaze UI 整合学习
- BabyLog SpringBoot+MyBatis+Thymeleaf+Amaze UI 整合学习
- 【SpringBoot学习记录】SpringBoot+Dubbo项目,整合Mybatis、Mongo、Redis。
- Spring Boot 数据访问集成 MyBatis 与事物配置
- SpringBoot框架学习(六)——数据访问
- 【个人学习】使用idea搭建SpringBoot,整合Mybatis、Thymeleaf,连接数据库,实现具有前端界面项目:主要功能登录,注册,个人信息查看、更改,不定时更新中...
- SpringBoot学习笔记 - 数据访问(Spring Data JPA)
- 数据可视化 三步走(二):springboot整合mybatis 搭建Java Web
- spring boot 整合mybatis单数据配置和多数据配置
- springboot整合mybatis和mysql实现数据的增删改查
- springboot学习(6)springboot整合Mybatis例子
- springboot学习(6)springboot整合Mybatis例子
- SpringBoot2.0完美整合Mybatis并使用thymeleaf展示数据
- SpringBoot 2.0 学习(五)整合Mybatis+CURD
- SpringBoot (八) :数据访问springboot整合JPA_一点课堂(多岸学院)