博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot整合mybatis(注解而且能看明白版本)
阅读量:4036 次
发布时间:2019-05-24

本文共 5106 字,大约阅读时间需要 17 分钟。

这篇文章主要讲解Springboot整合Mybatis实现一个最基本的增删改查功能,整合的方式有两种一种是注解形式的,也就是没有Mapper.xml文件,还有一种是XML形式的,我推荐的是使用注解形式,为什么呢?因为更加的简介,减少不必要的错误。

一、环境配置

对于环境配置我是用了一张表来展示,版本之间差异不大,你可以基于其他版本进行测试。Idea我已经破解了,破解码是我群里的一个朋友提供的,亲测可用。而且在2019的版本也可以永久破解。需要的可以私聊我。因为我之前写过破解的文章,因为某些原因,被平台删了。

名称 版本
Idea 2018专业版(已破解)
Maven 3.6.0
SpringBoot 2.2.2
Mybatis 5.1.44(版本高点比较好)
Navicat(可视化工具) 12(已破解)
jdk 1.8

这就是我的基本的环境。下一步我们一步一步来整合一波

二、整合Mybatis

第一步:数据库新建Person表

SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for person-- ----------------------------DROP TABLE IF EXISTS `person`;CREATE TABLE `person`  (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(255) CHARACTER SET utf8     COLLATE utf8_general_ci NULL DEFAULT NULL,  `age` int(11) NULL DEFAULT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;

这个表结构很简单,也就是三个字段id、name、age。并以id为主键且递增。

第二步:新建Springboot项目

这个比较简单,这里先给出一个最终的目录结构:

在这里插入图片描述

第三步:导入相关依赖

org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
mysql
mysql-connector-java
5.1.44
junit
junit
4.12

OK,我们只需要加上这些依赖即可。在我们的pom文件。

第四步:更改application.yml配置文件

我们只需要把application.properties文件改为yml格式即可。此时添加相关配置

#配置服务器信息server:  port: 8082spring:  #mysql数据库相关配置  datasource:    url: jdbc:mysql://127.0.0.1:3306/uav?characterEncoding=utf8&useSSL=false    username: root    password: root    driver-class-name: com.mysql.jdbc.Driver#mybatis依赖mybatis:  type-aliases-package: com.fdd.mybatis.dao

这里的配置有点多,不过还是一个最基本的配置都在这。

第五步:新建dao包,在dao包下新建Person类

public class Person {
private int id ; private String name; private int age; public Person() {
} public Person(int id, String name, int age) {
this.id = id; this.name = name; this.age = age; } //getter和setter方法 //toString方法}

这个类是和我们数据库中的Person类一一对应的。

第六步:新建mapper包,在mapper新建PersonMapper类

在这个类中,我们实现基本的增删改查功能接口:

@Mapperpublic interface PersonMapper {
//增加一个Person @Insert("insert into person(id,name,age)values(#{id},#{name},#{age})") int insert(Person person); //删除一个Person @Delete("delete from person where id = #{id}") int deleteByPrimaryKey(Integer id); //更改一个Person @Update("update person set name =#{name},age=#{age} where id=#{id}") int updateByPrimaryKey(Integer id); //查询一个Person @Select("select id,name ,age from person where id = #{id}") Person selectByPrimaryKey(Integer id); //查询所有的Person @Select("select id,name,age from person") List
selectAllPerson();}

这就是最基本的一个增删改查操作的接口。

第七步:新建service包,在service包创建PersonService接口

public interface PersonService {
//增加一个Person int insertPerson(Person person); //删除一个Person int deleteByPersonId(Integer id); //更改一个Person int updateByPersonId(Person record); //查询一个Person Person selectByPersonId(Integer id); //查询所有的Person List
selectAllPerson();}

第八步:在service包下创建PersonServiceImpl接口实现类

@Servicepublic class PersonServiceImpl implements  PersonService {
@Autowired private PersonMapper personMapper; @Override public int insertPerson(Person person) {
return personMapper.insert(person); } @Override public int deleteByPersonId(Integer id) {
return personMapper.deleteByPrimaryKey(id); } @Override public int updateByPersonId(Person record) {
return personMapper.updateByPrimaryKey(record); } @Override public Person selectByPersonId(Integer id) {
return personMapper.selectByPrimaryKey(id); } @Override public List
selectAllPerson() {
return personMapper.selectAllPerson(); }}

第九步:编写controller层

@RestControllerpublic class PersonController {
@Autowired private PersonService personService; @RequestMapping(value = "/add") public String students () {
Person person = new Person(); person.setId(1); person.setName("java的架构师技术栈"); person.setAge(18); int result = personService.insertPerson(person); System.out.println("插入的结果是:"+result); return result+""; } @RequestMapping(value = "/findAll") public String findAll () {
List
people = personService.selectAllPerson(); people.stream().forEach(System.out::println); return people.toString()+""; }}

第十步:在启动主类添加扫描器

@SpringBootApplication@MapperScan("com.fdd.mybatis.mapper")public class SpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisApplication.class, args); }}

第十一步:测试

在浏览器输入相应的路径即可。OK。大功告成。只要你按照上面的步骤一步一步来,就一定OK。

在这里插入图片描述

转载地址:http://vsbdi.baihongyu.com/

你可能感兴趣的文章
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>
如何构建高扩展性网站
查看>>
微服务架构的设计模式
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>
c++类的操作符重载注意事项
查看>>
c++模板与泛型编程
查看>>
STL::deque以及由其实现的queue和stack
查看>>
WAV文件解析
查看>>
DAC输出音乐2-解决pu pu 声
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
WPF UI&控件免费开源库
查看>>
QT打开项目提示no valid settings file could be found
查看>>