正在做网络编程课设,打算把mybatis-plus缝合进去,然后使用springboot缝合不能用mybatis-plus,然后我找了很久,找到了mybatis-plus在gitee上放的无spring使用方式:
官方地址:https://gitee.com/baomidou/mybatis-plus-samples/tree/master/mybatis-plus-sample-no-spring
自己平民化之后的通用代码地址:
https://github.com/like-wen/mybatis-plus-only.git
但是官方使用案例用的h2当数据库连接的启动,我的示例在此之上改变了:
1.改成了常用的mysql-connector-java
2.添加了mapper.xml
主要这几个文件:

测试插入结果:

主要代码:

依赖:

 <dependencies><!-- 导入mybatis-plus相关依赖 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.5.2</version></dependency><!-- 引入lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version><scope>provided</scope></dependency><!-- 数据库驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.23</version></dependency></dependencies>

//User之类的是用插件MybatisX生成的,会用的可以直接生成
User

package org.example.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;/*** * @TableName user*/
@TableName(value ="user")
@Data
public class User implements Serializable {/*** */@TableId(type = IdType.AUTO)private Integer id;/*** */private String name;/*** */private String password;/*** */private Date onlineTime;/*** */private Date createTime;@TableField(exist = false)private static final long serialVersionUID = 1L;@Overridepublic boolean equals(Object that) {if (this == that) {return true;}if (that == null) {return false;}if (getClass() != that.getClass()) {return false;}User other = (User) that;return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))&& (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()))&& (this.getOnlineTime() == null ? other.getOnlineTime() == null : this.getOnlineTime().equals(other.getOnlineTime()))&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()));}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((getId() == null) ? 0 : getId().hashCode());result = prime * result + ((getName() == null) ? 0 : getName().hashCode());result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());result = prime * result + ((getOnlineTime() == null) ? 0 : getOnlineTime().hashCode());result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());return result;}@Overridepublic String toString() {StringBuilder sb = new StringBuilder();sb.append(getClass().getSimpleName());sb.append(" [");sb.append("Hash = ").append(hashCode());sb.append(", id=").append(id);sb.append(", name=").append(name);sb.append(", password=").append(password);sb.append(", onlineTime=").append(onlineTime);sb.append(", createTime=").append(createTime);sb.append(", serialVersionUID=").append(serialVersionUID);sb.append("]");return sb.toString();}
}

UserMapper

package org.example.mapper;import org.example.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;/**
* @author 李可文
* @description 针对表【user】的数据库操作Mapper
* @createDate 2022-12-02 20:29:17
* @Entity org.example.entity.User
*/
public interface UserMapper extends BaseMapper<User> {}

NoSpring

package org.example;import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;import javax.sql.DataSource;import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.example.entity.User;
import org.example.mapper.UserMapper;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;public class NoSpring {private static SqlSessionFactory sqlSessionFactory = initSqlSessionFactory();public static void main(String[] args) {//初始化try (SqlSession session = sqlSessionFactory.openSession(true)) {//创建mapper对象UserMapper mapper = session.getMapper(UserMapper.class);//创建user对象,并赋值User user = new User();user.setPassword("123");user.setName("lkw");//插入mapper.insert(user);System.out.println("结果: " + mapper.selectById(user.getId()));}}//工厂方法public static SqlSessionFactory initSqlSessionFactory() {DataSource dataSource = dataSource();TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("Production", transactionFactory, dataSource);MybatisConfiguration configuration = new MybatisConfiguration(environment);//在这里添加Mapperconfiguration.addMapper(UserMapper.class);configuration.setLogImpl(StdOutImpl.class);return new MybatisSqlSessionFactoryBuilder().build(configuration);}//连接进行public static DataSource dataSource() {SimpleDriverDataSource dataSource = new SimpleDriverDataSource();dataSource.setDriverClass(com.mysql.jdbc.Driver.class);dataSource.setUrl("jdbc:mysql://localhost:3306/course?serverTimezone=GMT%2B8");dataSource.setUsername("root");dataSource.setPassword("148963");return dataSource;}}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mapper.UserMapper"><resultMap id="BaseResultMap" type="org.example.entity.User"><id property="id" column="id" jdbcType="INTEGER"/><result property="name" column="name" jdbcType="VARCHAR"/><result property="password" column="password" jdbcType="VARCHAR"/><result property="onlineTime" column="online_time" jdbcType="TIMESTAMP"/><result property="createTime" column="create_time" jdbcType="TIMESTAMP"/></resultMap><sql id="Base_Column_List">id,name,password,online_time,create_time</sql>
</mapper>

sql

/*Navicat Premium Data TransferSource Server         : mysqlSource Server Type    : MySQLSource Server Version : 80029Source Host           : localhost:3306Source Schema         : courseTarget Server Type    : MySQLTarget Server Version : 80029File Encoding         : 65001Date: 02/12/2022 22:05:01
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,`online_time` datetime NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,`create_time` datetime NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'wl', 'wl', '2022-11-09 21:56:23', '2022-11-09 21:56:03');
INSERT INTO `user` VALUES (2, 'gmh', 'gmh', '2022-11-09 21:56:26', '2022-11-09 21:56:08');
INSERT INTO `user` VALUES (3, 'lkw', 'lkw', '2022-11-09 21:56:28', '2022-11-09 21:56:11');
INSERT INTO `user` VALUES (4, 'xy', 'xy', '2022-11-09 21:56:32', '2022-11-09 21:56:14');
INSERT INTO `user` VALUES (6, 'lkw', NULL, NULL, NULL);
INSERT INTO `user` VALUES (7, 'lkw', '123', NULL, NULL);
INSERT INTO `user` VALUES (8, 'lkw', '123', NULL, NULL);SET FOREIGN_KEY_CHECKS = 1;

mybatis-plus无spring框架相关推荐

  1. spring框架测试题

    章节测验 1.spring基本应用 1.[单选题] 以下关于Spring核心容器相关说法错误的是(). A.Spring框架的所有功能都是通过其核心容器来实现的 B.创建BeanFactory实例时, ...

  2. MySQL数据库事务、mybatis框架、spring框架、springmvc框架、永和大王门店管理系统(框架第二部分)

    第十二章 MySQL数据库事务 一. 事务及四大特性 1.什么是事务 数据库事务(Database Transaction),是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行 ...

  3. 【Java】MyBatis与Spring框架整合(一)

    本文将利用 Spring 对 MyBatis 进行整合,在对组件实现解耦的同时,还能使 MyBatis 框架的使用变得更加方便和简单. 整合思路 作为 Bean 容器,Spring 框架提供了 IoC ...

  4. spring+websocket综合(springMVC+spring+MyBatis这是SSM框架和websocket集成技术)

    java-websocket该建筑是easy.儿童无用的框架可以在这里下载主线和个人教学好java-websocket计划: Apach Tomcat 8.0.3+MyEclipse+maven+JD ...

  5. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

  6. (转)在编写Spring框架的配置文件时,标签无提示符的解决办法

    http://blog.csdn.net/yerenyuan_pku/article/details/52831618 问题描述 初学者在学习Spring框架的过程中,大概会碰到这样一个问题:在编写S ...

  7. 紧急:Spring框架被爆出存在0day级别远程命令执行漏洞。漏洞危害程度不亚于log4j漏洞根据目前掌握的信息,JDK版本在9及以上的Spring框架均受影响。该漏洞目前无官方修复补丁

    Spring框架被爆出存在0day级别远程命令执行漏洞.漏洞危害程度不亚于log4j漏洞根据目前掌握的信息,JDK版本在9及以上的Spring框架均受影响. 漏洞信息和漏洞影响排查方法如下: 漏洞名称 ...

  8. J2EE高级开发框架小课程之Spring框架1——Spring创建三种方式:使用无参构造器创建对象(bean标签的方式),使用静态方法创建对象,使用实例方法创建对象(非静态方法)

    Spring是什么? 是一个开源的.用于简化企业级应用开发的应用开发框架. a.简化开发: Spring框架对很多常用的api做了简化,比如,使用Spring jdbc (Spring jdbc是Sp ...

  9. mybatis和spring框架的整合

    Mybatis+Sping整合 mybatis+spring整合 整合思路 思路:将mybatis框架的对象,交给spring管理. 把SqlSessionFactory对象,交给spring管理 把 ...

最新文章

  1. Apache httpd 以root身份运行
  2. stm32 内部sram大小_STM32第三天
  3. 从 2015 年起的未来五年内有哪些创业方向 | PMcaff-产品
  4. boost::initialized<T>相关的测试程序
  5. 作业27-登录之后更新导航
  6. 如何生成 jMeter 结果分析统计图表
  7. 编写一个简单的spring MVC程序
  8. powerdesigner 生成实体代码 附加生成xml
  9. 【kafka】kafka RecordAccumulator封装消息流程
  10. 有关胶囊网络你所应知道的一切
  11. Next, let’s look at the interface
  12. python成功之路,Day1-发展历史
  13. Java关系操作符简写
  14. FFmpeg滤镜实现区域视频增强 及 D3D实现视频播放区的拉大缩小转
  15. 父与子的编程之旅 python 3_python3-父与子的编程之旅第十五章
  16. TMS320F28335的SPI
  17. flex布局完整示例
  18. H5页面(微信也可)中调用手机拨打电话功能
  19. 计算机台式和电脑的区别吗,直观:工业计算机和家用台式计算机有什么区别
  20. AUTOCAD——快速标注命令

热门文章

  1. 计算机网络中的c类地址,计算机网络中C类地址的子网掩码是哪个
  2. Intra-Instance VICReg: Bag of Self-Supervised Image Patch Embedding
  3. php 服務器連接,cocos2d-x網絡編程 連接php服務器筆記4
  4. PMC新近推出的 DIGI-G4 400G OTN处理器荣获光波通信创新大奖
  5. 计算机科学与技术寒假社会实践,计算机科学与技术专业寒假社会实践报告.doc...
  6. 文件上传工具类FileUploadUtils
  7. 当婚纱摄影邂逅超级表格|流程监控
  8. Kubernetes的学习笔记总结之k8s集群安装部署
  9. 【创业笔记】团队建设--团队氛围的营造
  10. 人形机器人视觉处理——垃圾分类