springboot-mybatis整合案例分享
程序员文章站
2022-03-26 16:44:13
示例笔记CSDN帮助Markdown一、安装IDEA插件1、安装lombok插件Lombok插件可以在编译代码的时候,为封装类生成set/get方法,这可以让封装类的代码保持工整和简洁 (通俗点说就是简化实体开发)2、 安装Free MyBatis plugin插件这个插件可以根据数据表自动生成DAO和XML文件,提高工作效率。另外,它还可以把XML文件和DAO关联在一起,你写代码的时候,点击DAO方法前面的图标,就会自动跳转到这个DAO方法对应的XML标签。二、创建......
一、安装IDEA插件
1、安装lombok插件
Lombok插件可以在编译代码的时候,为封装类生成set/get方法,这可以让封装类的代码保持工整和
简洁 (通俗点说就是简化实体开发)
2、 安装Free MyBatis plugin插件
这个插件可以根据数据表自动生成DAO和XML文件,提高工作效率。另外,它还可以把XML文件和
DAO关联在一起,你写代码的时候,点击DAO方法前面的图标,就会自动跳转到这个DAO方法对应的
XML标签。
二、创建SpringBoot项目
1、 利用图形面板创建SpringBoot项目
2、JDK版本选择8
3、勾选Lombok、SpringWeb、MySQL Driver,以及MyBatis Framework
4、 在pom.xml文件中添加Druid数据库连接池依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency>
5、把application.properties文件的后缀名改成yml,然后添加如下内容:
server: tomcat: uri-encoding: UTF-8 threads: max: 200 min-spare: 30 connection-timeout: 5000ms port: 8080 servlet: context-path: /demo spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.cj.jdbc.Driver #你数据库的URL url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai #你数据库的用户名 username: root #你数据的密码 password: 123456 initial-size: 5 max-active: 10 min-idle: 5 mybatis: #XML文件的地址 mapper-locations: classpath*:mapper/*.xml #映射类文件地址 type-aliases-package: com.example.demo.pojo configuration: #支持驼峰命名法(很重要) map-underscore-to-camel-case: true
三、连接数据库
1、 在IDEA工具上面,创建MySQL数据库连接,如下:
2、建立连接之后,创建demo逻辑库
3、在demo逻辑库上鼠标右键选择运行demo.sql文件,导入数据表
demo.sql
/*
Navicat Premium Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 50721
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50721
File Encoding : 65001
Date: 08/10/2018 12:37:19
*/ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_bonus -- ---------------------------- DROP TABLE IF EXISTS `t_bonus`; CREATE TABLE `t_bonus` ( `empno` int(4) NOT NULL, `job` varchar(20) DEFAULT NULL, `sal` decimal(10,2) DEFAULT NULL, `comm` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`empno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for t_dept -- ---------------------------- DROP TABLE IF EXISTS `t_dept`; CREATE TABLE `t_dept` ( `deptno` int(2) NOT NULL, `dname` varchar(20) DEFAULT NULL, `loc` varchar(20) DEFAULT NULL, PRIMARY KEY (`deptno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_dept -- ---------------------------- BEGIN; INSERT INTO `t_dept` VALUES (10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO `t_dept` VALUES (20, 'RESEARCH', 'DALLAS'); INSERT INTO `t_dept` VALUES (30, 'SALES', 'CHICAGO'); INSERT INTO `t_dept` VALUES (40, 'OPERATIONS', 'BOSTON'); COMMIT; -- ---------------------------- -- Table structure for t_emp -- ---------------------------- DROP TABLE IF EXISTS `t_emp`; CREATE TABLE `t_emp` ( `empno` int(4) NOT NULL, `ename` varchar(20) DEFAULT NULL, `job` varchar(20) DEFAULT NULL, `mgr` int(4) DEFAULT NULL, `hiredate` date DEFAULT NULL, `sal` decimal(10,2) DEFAULT NULL, `comm` decimal(10,2) DEFAULT NULL, `deptno` int(2) DEFAULT NULL, PRIMARY KEY (`empno`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_emp -- ---------------------------- BEGIN; INSERT INTO `t_emp` VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17 00:00:00', 800.00, NULL, 20); INSERT INTO `t_emp` VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20 00:00:00', 1600.00, 300.00, 30); INSERT INTO `t_emp` VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22 00:00:00', 1250.00, 500.00, 30); INSERT INTO `t_emp` VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-04-02 00:00:00', 2975.00, NULL, 20); INSERT INTO `t_emp` VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28 00:00:00', 1250.00, 1400.00, 30); INSERT INTO `t_emp` VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01 00:00:00', 2850.00, NULL, 30); INSERT INTO `t_emp` VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09 00:00:00', 2450.00, NULL, 10); INSERT INTO `t_emp` VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09 00:00:00', 3000.00, NULL, 20); INSERT INTO `t_emp` VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17 00:00:00', 5000.00, NULL, 10); INSERT INTO `t_emp` VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08 00:00:00', 1500.00, 0.00, 30); INSERT INTO `t_emp` VALUES (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12 00:00:00', 1100.00, NULL, 20); INSERT INTO `t_emp` VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03 00:00:00', 950.00, NULL, 30); INSERT INTO `t_emp` VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03 00:00:00', 3000.00, NULL, 20); INSERT INTO `t_emp` VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23 00:00:00', 1300.00, NULL, 10); COMMIT; -- ---------------------------- -- Table structure for t_salgrade -- ---------------------------- DROP TABLE IF EXISTS `t_salgrade`; CREATE TABLE `t_salgrade` ( `grade` int(11) NOT NULL, `losal` decimal(10,2) DEFAULT NULL, `hisal` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`grade`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_salgrade -- ---------------------------- BEGIN; INSERT INTO `t_salgrade` VALUES (1, 700.00, 1200.00); INSERT INTO `t_salgrade` VALUES (2, 1201.00, 1400.00); INSERT INTO `t_salgrade` VALUES (3, 1401.00, 2000.00); INSERT INTO `t_salgrade` VALUES (4, 2001.00, 3000.00); INSERT INTO `t_salgrade` VALUES (5, 3001.00, 9999.00); COMMIT; -- ---------------------------- -- Table structure for t_emp_ip -- ---------------------------- DROP TABLE IF EXISTS `t_emp_ip`; CREATE TABLE `t_emp_ip` ( `id` int(11) NOT NULL, `empno` int(11) NOT NULL, `ip` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE, UNIQUE INDEX `empno`(`empno`) USING BTREE, UNIQUE INDEX `ip`(`ip`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
4、选中t_dept表,鼠标右键,选择mybatis-generator
5、在图形面板中修改映射类名字为Dept,路径为com.example.demo.pojo。DAO接口名字为DeptDao,路径为com.example.demo.dao。XML文件目录为mapper,生成代码
6、给DeptDao接口加上@Mapper注解
@Mapper public interface DeptDao { int deleteByPrimaryKey(Integer deptno); int insert(Dept record); int insertSelective(Dept record); Dept selectByPrimaryKey(Integer deptno); int updateByPrimaryKeySelective(Dept record); int updateByPrimaryKey(Dept record); }
7、创建service文件夹,编写DeptService.java类
package com.example.demo.service; import com.example.demo.dao.DeptDao; import com.example.demo.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /**
* Created by baidou on 2020/8/26.
*/ @Service public class DeptService { @Autowired private DeptDao deptDao; public Dept selectByPrimaryKey(Integer deptNo) { Dept dept = deptDao.selectByPrimaryKey(deptNo); return dept; } }
8、 创建controller文件夹,编写DeptController.java类
package com.example.demo.controller; import com.example.demo.pojo.Dept; import com.example.demo.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /**
* Created by baidou on 2020/8/26.
*/ @RequestMapping("/dept") @RestController public class DeptController { @Autowired private DeptService deptService; @GetMapping("/selectByPrimaryKey") public Dept selectByPrimaryKey(Integer deptno) { Dept dept = deptService.selectByPrimaryKey(deptno); return dept; } }
9、运行DemoApplication类
10、 打开浏览器访问
http://127.0.0.1:8080/demo/dept/selectByPrimaryKey?deptno=10
浏览器会出现根据10编号查询出来的部门数据
{"deptno":10,"dname":"ACCOUNTING","loc":"NEW YORK"}
本文地址:https://blog.csdn.net/qq_46921028/article/details/108244654
上一篇: 安卓案例:动态显示时间
下一篇: JVM性能监控和故障处理的指令、工具