• 使用maven配置基本Mybatis


    Mybatis 也称为是ibatis,主要体现在普通、底层SQL查询、存储过程、高级映射的持久化框架!

    优点:                   

                     1.消除了几乎所有的JBDC代码、参数!

                     2.手动设置以及结果集的检索

                     3.简化了xml、注解配置和对象关系映射

                     4.将接口、pojo映射成数据库中的记录

    对比Hibernate:

                    1.配置文件中获取-sessionfactory

                    2.从sessionfactory中得到session

                    3.在session中完成增删改查操作,完成后并自动关闭!

                    4.java 对象和 数据库之间有做mapping 的配置文

    一、使用Maven:创建Web工程,右键点击configure,选择Convert Maven Project

    二、编辑pom.xml:加入以下代码!

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="
    http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
                      <modelVersion>4.0.0</modelVersion>
                      <groupId>mabatis_v1.0</groupId>
                      <artifactId>mabatis_v1.0</artifactId>
                      <version>0.0.1-SNAPSHOT</version>
                      <packaging>war</packaging>
                      <build>
                       <sourceDirectory>src</sourceDirectory>
                       <plugins>
                        <plugin>
                         <artifactId>maven-compiler-plugin</artifactId>
                         <version>3.3</version>
                         <configuration>
                          <source>1.8</source>
                          <target>1.8</target>
                         </configuration>
                        </plugin>
                        <plugin>
                         <artifactId>maven-war-plugin</artifactId>
                         <version>2.6</version>
                         <configuration>
                          <warSourceDirectory>WebContent</warSourceDirectory>
                          <failOnMissingWebXml>false</failOnMissingWebXml>
                         </configuration>
                        </plugin>
                       </plugins>
                      </build>
                      
    <!-- 依赖关系 -->
                      <dependencies>
                      
    <!-- ibatis.jar 从网络获取,则不需要手动导入ibatis的jar包!-->
                       <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                        <version>3.2.8</version>
                       </dependency>
                      <!-- junit.jar -->
                       <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.10</version>
                       </dependency>
                      </dependencies>
                     </project>

    三、创建java类,必须对应数据库中:字段、数据类型

    四、创建java对象与数据库之间的xml文件

    <?xml version="1.0" encoding="UTF-8"?>
                     <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
                     "
    http://mybatis.org/dtd/mybatis-3-mapper.dtd">
                     <!-- 命名空间mapper:com.model.professional -->
                     <mapper namespace="com.model.professional">
                     <select id="selectUserById" parameterType="int" resultType="professional">
                     <!-- 底层的SQL语句 -->
                     select * from PROFESSIONAL_INFOTB where PROFESSIONAL_ID=#{PROFESSIONAL_ID}
                     </select>
                     </mapper>

    五、创建confg.xml文件,并注册:创建java对象与数据库之间的xml文件

    <?xml version="1.0" encoding="UTF-8"?>
                     <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
                     "
    http://mybatis.org/dtd/mybatis-3-config.dtd">
                     <configuration>
                     <!-- 注册对象的空间命名 -->
                     <environments default="development">
                     <environment id="development">
                     <transactionManager type="JDBC"/>
                     <dataSource type="POOLED">
                     <!-- 1.加载数据库驱动:oracle.jdbc.driver.OracleDriver -->
                     
    <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                     <!-- 2.数据库连接地址:oracle.jdbc.driver.OracleDriver -->
                     
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
                     <!-- 数据库用户... -->
                     
    <property name="username" value="ED"/>
                     <!-- 数据库密码... -->
                     
    <property name="password" value="admin"/>
                     </dataSource>
                     </environment>
                     </environments>
                     <!-- 注册映射文件:java对象与数据库之间的xml文件路径! -->
                     </configuration>

    六、创建SqlSessionFactory,获取session,完成简单查询!

  • 相关阅读:
    http
    node 学习 http
    socket.io
    每日日报
    每日日报
    每日日报
    06人件读书笔记之一
    每日日报
    每日日报
    05程序员修炼之道:从小工到专家阅读笔记之三
  • 原文地址:https://www.cnblogs.com/xxt19970908/p/5223755.html
Copyright © 2020-2023  润新知