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

Mybatis 源码搭建

文章目录

  • 源码下载
  • 测试模块搭建
    • 学习博客


源码下载

首先下载mybatis-parent的源码:gitee地址 => https://gitee.com/callback_lab/mybatis-parent.git

然后下载mybatis的源码:gitee地址 => https://gitee.com/callback_lab/mybatis-src.git

带中文注释的三方源码


以下包需要注释,否则会报错 :

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### Cause: java.lang.IllegalStateException: Cannot enable lazy loading because Javassist is not available. Add Javassist to your classpath.
    <dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>3.2.20</version>
<!--      <scope>compile</scope>-->
<!--      <optional>true</optional>--></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.27.0-GA</version>
<!--      <scope>compile</scope>-->
<!--      <optional>true</optional>--></dependency>

将mybatis-parent与mybatis导入idea同一个project下。

测试模块搭建

新建一个测试模块,这里叫mybatis-gabriel

项目基础架构
在这里插入图片描述

实例原博客

pom :

  <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0-SNAPSHOT</version></dependency></dependencies>

相关代码

主要测试入口代码

public class TestMain {public static void main(String[] args) {String resource = "mybatis-config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resource);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}SqlSessionFactory sqlSessionFactory = null;sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = null;try {sqlSession = sqlSessionFactory.openSession();RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);Role role = roleMapper.getRole(1L);System.out.println(role.getId() + ":" + role.getRoleName() + ":" + role.getNote());sqlSession.commit();} catch (Exception e) {// TODO Auto-generated catch blocksqlSession.rollback();e.printStackTrace();} finally {sqlSession.close();}}
}

po :

/** @author gethin* 角色的实体类*/
public class Role {private long id;private String roleName;private String note;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}public String getNote() {return note;}public void setNote(String note) {this.note = note;}
}

MyStringHandler :

@MappedTypes({String.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MyStringHandler implements TypeHandler<String> {Logger log= Logger.getLogger(MyStringHandler.class.getName());@Overridepublic String getResult(ResultSet rs, String colName) throws SQLException {log.info("使用我的TypeHandler,ResultSet列名获取字符串");return rs.getString(colName);}@Overridepublic String getResult(ResultSet rs, int index) throws SQLException {log.info("使用我的TypeHandler,ResultSet下标获取字符串");return rs.getString(index);}@Overridepublic String getResult(CallableStatement cs, int index) throws SQLException {log.info("使用我的TypeHandler,CallableStatement下标获取字符串");return cs.getString(index);}@Overridepublic void setParameter(PreparedStatement ps, int index, String value, JdbcType arg3) throws SQLException {log.info("使用我的TypeHandler");ps.setString(index, value);}}

mapper :

public interface RoleMapper {public Role getRole(Long id);public Role findRole(String roleName);public int deleteRole(Long id);public int insertRole(Role role);
}

RoleMapper.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.mybatis.debug.mapper.RoleMapper"><resultMap type="org.mybatis.debug.po.Role" id="roleMap"><id column="id" property="id" javaType="long" jdbcType="BIGINT" /><result column="role_name" property="roleName" javaType="string"jdbcType="VARCHAR" /><result column="note" property="note"typeHandler="org.mybatis.debug.handle.MyStringHandler" /></resultMap><select id="getRole" parameterType="long" resultMap="roleMap">selectid,role_name as roleName,note from role where id=#{id}</select><select id="findRole" parameterType="long" resultMap="roleMap">selectid,role_name,note from role where role_name like CONCAT('%',#{roleNamejavaType=string,jdbcType=VARCHAR,typeHandler=com.gethin.handler.MyStringHandler},'%')</select><insert id="insertRole" parameterType="org.mybatis.debug.po.Role">insert intorole(role_name,note) value(#{roleName},#{note})</insert><delete id="deleteRole" parameterType="long">delete from role whereid=#{id}</delete>
</mapper>

sql :

-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (`id` int(11) DEFAULT NULL,`role_name` varchar(255) DEFAULT NULL,`note` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '管理员', '管理员');
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (`id` int(11) DEFAULT NULL,`role_id` int(11) DEFAULT NULL,`user_name` varchar(255) DEFAULT NULL,`user_note` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `user` VALUES ('1', '1','张三', '管理员张三');

学习博客

调试博客

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

相关文章:

  • shell编程系列(5)-函数的定义
  • 鸿蒙应用开发-初见:入门知识、应用模型
  • 通过测试驱动开发(TDD)的方式开发Web项目
  • 技巧-PyCharm中Debug和Run对训练的影响和实验测试
  • 【古月居《ros入门21讲》学习笔记】07_创建工作空间和功能包
  • 第20章多线程
  • 深信服防火墙设置应用控制策略(菜鸟必看)
  • 解锁 ElasticJob 云原生实践的难题
  • 鸿蒙开发已成新趋势
  • 万人拼团团购小程序源码系统+拼团设置+拼团管理 附带完整的搭建教程
  • 软信天成:速看!云端混合数据管理的最佳解决方案
  • GO 集成Prometheus
  • ESP32-Web-Server 实战编程-通过网页控制设备的 GPIO
  • Springboot 中 指定 AspectJ 的织入模式
  • 【.NET全栈】.net的微软API接口与.NET框架源码
  • 【深度学习】基于深度学习的超分辨率图像技术一览
  • Android12强制所有应用跟随gsensor旋转
  • C#常用运算符的优先级
  • 鸿蒙4.0开发笔记之ArkTS语法的基础数据类型[DevEco Studio开发](七)
  • 集成学习的两种常见策略:bagging VS. boosting
  • 居家适老化设计第三十四条---卫生间之照明
  • 如何使用Cloudreve将个人电脑打造为私有云盘并实现远程访问
  • [SaaS] 淘宝AI淘淘秀
  • 第二证券:机构密集调研消费电子、半导体产业链
  • app小程序定制的重点|软件定制开发|网站搭建
  • 11-28渗透
  • qt实现一个安卓测试小工具
  • 驾驭未来,智能化管理——汽车ERP系统
  • flutter开发实战-当前界面无操作60s返回主页实现
  • 绩效考核的基础及基本内容