• Mybatis入门


    目录

    MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。简单来说,Mybatis是一个对象关系映射数据库层框架。

    一、Mybatis环境搭建

    二、基于ssm逆向工程的使用


    MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。简单来说,Mybatis是一个对象关系映射框架。

    一、Mybatis环境搭建

    1、当我们创建好一个Maven项目后,在pom.xml配置文件中导入Mybatis的相关依赖

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <groupId>org.examplegroupId>
    5. <artifactId>mybatis01artifactId>
    6. <version>1.0-SNAPSHOTversion>
    7. <packaging>warpackaging>
    8. <name>mybatis01 Maven Webappname>
    9. <url>http://www.example.comurl>
    10. <properties>
    11. <maven.compiler.source>1.8maven.compiler.source>
    12. <maven.compiler.target>1.8maven.compiler.target>
    13. properties>
    14. <dependencies>
    15. <dependency>
    16. <groupId>junitgroupId>
    17. <artifactId>junitartifactId>
    18. <version>4.12version>
    19. <scope>testscope>
    20. dependency>
    21. <dependency>
    22. <groupId>javax.servletgroupId>
    23. <artifactId>javax.servlet-apiartifactId>
    24. <version>4.0.0version>
    25. <scope>providedscope>
    26. dependency>
    27. <dependency>
    28. <groupId>org.mybatisgroupId>
    29. <artifactId>mybatisartifactId>
    30. <version>3.4.5version>
    31. dependency>
    32. <dependency>
    33. <groupId>mysqlgroupId>
    34. <artifactId>mysql-connector-javaartifactId>
    35. <version>5.1.44version>
    36. dependency>
    37. <dependency>
    38. <groupId>org.apache.logging.log4jgroupId>
    39. <artifactId>log4j-coreartifactId>
    40. <version>2.9.1version>
    41. dependency>
    42. <dependency>
    43. <groupId>org.apache.logging.log4jgroupId>
    44. <artifactId>log4j-apiartifactId>
    45. <version>2.9.1version>
    46. dependency>
    47. <dependency>
    48. <groupId>org.apache.logging.log4jgroupId>
    49. <artifactId>log4j-webartifactId>
    50. <version>2.9.1version>
    51. dependency>
    52. dependencies>
    53. <build>
    54. <finalName>mybatis01finalName>
    55. <resources>
    56. <resource>
    57. <directory>src/main/javadirectory>
    58. <includes>
    59. <include>**/*.xmlinclude>
    60. includes>
    61. resource>
    62. <resource>
    63. <directory>src/main/resourcesdirectory>
    64. <includes>
    65. <include>jdbc.propertiesinclude>
    66. <include>*.xmlinclude>
    67. includes>
    68. resource>
    69. resources>
    70. <pluginManagement>
    71. <plugins>
    72. <plugin>
    73. <artifactId>maven-clean-pluginartifactId>
    74. <version>3.1.0version>
    75. plugin>
    76. <plugin>
    77. <artifactId>maven-resources-pluginartifactId>
    78. <version>3.0.2version>
    79. plugin>
    80. <plugin>
    81. <artifactId>maven-compiler-pluginartifactId>
    82. <version>3.8.0version>
    83. plugin>
    84. <plugin>
    85. <artifactId>maven-surefire-pluginartifactId>
    86. <version>2.22.1version>
    87. plugin>
    88. <plugin>
    89. <artifactId>maven-war-pluginartifactId>
    90. <version>3.2.2version>
    91. plugin>
    92. <plugin>
    93. <artifactId>maven-install-pluginartifactId>
    94. <version>2.5.2version>
    95. plugin>
    96. <plugin>
    97. <artifactId>maven-deploy-pluginartifactId>
    98. <version>2.8.2version>
    99. plugin>
    100. <plugin>
    101. <groupId>org.mybatis.generatorgroupId>
    102. <artifactId>mybatis-generator-maven-pluginartifactId>
    103. <version>1.3.2version>
    104. <dependencies>
    105. <dependency>
    106. <groupId>mysqlgroupId>
    107. <artifactId>mysql-connector-javaartifactId>
    108. <version>5.1.44version>
    109. dependency>
    110. dependencies>
    111. <configuration>
    112. <overwrite>trueoverwrite>
    113. configuration>
    114. plugin>
    115. plugins>
    116. pluginManagement>
    117. build>
    118. project>

    2、添加数据库连接的配置文件 jdbc.properties

    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/zjy?useUnicode=true&characterEncoding=UTF-8
    3. jdbc.username=root
    4. jdbc.password=123456

    3、导入mybatis的配置文件

    1. configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    2. <configuration>
    3. <properties resource="jdbc.properties"/>
    4. <settings>
    5. <setting name="logImpl" value="LOG4J2"/>
    6. settings>
    7. <typeAliases>
    8. typeAliases>
    9. <environments default="development">
    10. <environment id="development">
    11. <transactionManager type="jdbc"/>
    12. <dataSource type="POOLED">
    13. <property name="driver"
    14. value="${jdbc.driver}"/>
    15. <property name="url"
    16. value="${jdbc.url}"/>
    17. <property name="username" value="${jdbc.username}"/>
    18. <property name="password" value="${jdbc.password}"/>
    19. dataSource>
    20. environment>
    21. environments>
    22. <mappers>
    23. <mapper resource="com/javaxl/mapper/BookMapper.xml"/>
    24. mappers>
    25. configuration>

    在mybatis配置文件中引入数据库连接的配置文件

     加载驱动

     4、下载相关插件

     其中maven helper插件能够将jar包之间的依赖关系呈树形结构展示,一目了然

    二、基于ssm逆向工程的使用

    1. 安装Mybatis generator代码生成器插件

    在pom。xml文件中引入该插件的字符串下载到本地仓库中

    注意:该插件不支持高版本的sql驱动

    2、配置 generatorConfig.xml

    2.1修改mysql的jar包存放路径,一定要与本地仓库位置相匹配

     2.2修改实体类、dao层和sql对应配置文件的生成地址并且指定需要生成增删改查代码的表

     

    3、配置Maven的mybatis逆向生成代码的commond line命令

     

    然后就成功生成了t_oa_permission的增删改查代码

  • 相关阅读:
    MyBatis
    微信小程序canvas画布绘制base64图片并保存图片到相册中
    Linux之部署Web项目到云服务器
    揭秘”智能定投“
    Java高级——模块化系统
    我,硬件6年,成功转入IC岗,想给大家提个醒!
    java面向对象(三)
    ​一款开源的.NET程序集反编译、编辑和调试神器
    09 模型的增删查改《ThinkPHP6 入门到电商实战》
    原生mysql与mybatis执行update语句的差异
  • 原文地址:https://blog.csdn.net/m0_67477525/article/details/126274275