当前位置: 首页 > news >正文

Spring整合第三方框架-MyBatis原始操作代码

建议自己写一下

实体类,用于封装数据库数据

package com.example.pojo;import java.util.Date;public class Emp {private Integer id;private String username;private String password;private String name;private Integer gender;private String image;private Integer job;private Date entrydate;private Date create_time;private Date update_time;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getGender() {return gender;}public void setGender(Integer gender) {this.gender = gender;}public String getImage() {return image;}public void setImage(String image) {this.image = image;}public Integer getJob() {return job;}public void setJob(Integer job) {this.job = job;}public Date getEntrydate() {return entrydate;}public void setEntrydate(Date entrydate) {this.entrydate = entrydate;}public Date getCreate_time() {return create_time;}public void setCreate_time(Date create_time) {this.create_time = create_time;}public Date getUpdate_time() {return update_time;}public void setUpdate_time(Date update_time) {this.update_time = update_time;}@Overridepublic String toString() {return "Emp{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", name='" + name + '\'' +", gender=" + gender +", image='" + image + '\'' +", job=" + job +", entrydate=" + entrydate +", create_time=" + create_time +", update_time=" + update_time +'}';}
}

Mapper接口类,定义对于数据库的操作

package com.example.Mapper;import com.example.pojo.Emp;import java.util.List;public interface EmpMapper {List<Emp> findAll();
}

Mapper接口类对应的xml配置文件,用于实行Mapper接口类中对于数据库的操作

<?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="com.example.Mapper.EmpMapper"><select id="findAll" resultType="com.example.pojo.Emp">select *from tb_emp;</select>
</mapper>

MyBatis配置文件,定义和配置MyBatis框架的各种参数和属性,以及映射文件的路径和数据库连接信息。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/db02"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!--指定需要扫描的Mapper接口的包路径--><mappers><package name="com.example.Mapper"/></mappers>
</configuration>

测试类代码,实现MyBatis功能

package com.example.Test;import com.example.Mapper.EmpMapper;
import com.example.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;
import java.util.List;public class MyBatisTest {public static void main(String[] args) throws Exception {InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);List<Emp> all = mapper.findAll();for (Emp emp : all) {System.out.println(emp);}}
}

运行结果如下

http://www.lryc.cn/news/176157.html

相关文章:

  • 比特币 ZK 赏金系列:第 2 部分——查找哈希冲突
  • Android9底部导航栏出现空白按钮问题分析
  • 秦时明月沧海手游阵容推荐,秦时明月沧海角色强度
  • 基于微信小程序的大学生科技竞赛竞技报名系统设计与实现(源码+lw+部署文档+讲解等)
  • crypto:摩丝
  • Docker最基本使用
  • vue2.x 迭代更新项目去掉缓存处理
  • Linux高性能服务器编程 学习笔记 第八章 高性能服务器程序框架
  • 技术对比:Flutter vs. 传统桌面应用开发框架
  • [C++ 网络协议] 异步通知I/O模型
  • Postgresql事务测试
  • 【数据结构--排序】冒泡排序,选择排序,插入排序
  • vue pc端/手机移动端 — 下载导出当前表格页面pdf格式
  • 125. 验证回文串 【简单题】
  • 描述性统计分析
  • Visual Studio2019 C++ 编程问题集锦
  • 链表的回文判断
  • 281_JSON_两段例子的比较,哪一段更简洁、易懂、没有那么多嵌套
  • 想要精通算法和SQL的成长之路 - 最长递增子序列 II(线段树的运用)
  • java用easyexcel按模版导出
  • Servlet执行流程生命周期方法介绍体系结构、Request和Response的功能详解
  • 软件工程之总体设计
  • 监控员工电脑文件拷贝记录:电脑怎么看员工复制文件的历史记录
  • vue中request.js中axios请求和(若依)文件通用下载方法封装
  • 【大数据存储与处理】1. hadoop单机伪分布安装和集群安装
  • linux通过time命令统计代码编译时间
  • logback日志是怎么保证多线程输出日志线程安全的
  • 2022年统计用区划代码表SQL 01
  • EM@基本初等函数@幂和根式@指数函数
  • 时序预测 | MATLAB实现NGO-GRU北方苍鹰算法优化门控循环单元时间序列预测