【初学Mybatis】No.2 Mybatis的下载和搭建核心架构(Maven搭建)

3 篇文章 0 订阅
3 篇文章 0 订阅

写在前面的话:在慕课网学习的时候,老师使用的是一般的web结构,更多的时候都是使用的mevan项目结构,所以这里呢就改成了mevan项目结构。

No.1 下载地址

【强调】在博客完成中,突然发现3.4.2中没有org.apache.ibatis.session.SqlSession;这个类,请各位使用3.4.1版本,以后找时间再去完成这两个版本的差异性更新

https://github.com/mybatis/mybatis-3/releases

我们需要下载一个打包好的jar包以及源代码,如图所示:

这里写图片描述

如果你是使用的Maven搭建的项目,可以将下面的代码拷贝到pom.xml文件中

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.2</version>
</dependency>

No.2 拷贝Mybatis的jar包和MySQL的jar包到web项目

此步骤如果是使用的Maven搭建可省略

找到下载好的这个压缩包并解压

这里写图片描述

打开解压所得的文件夹

这里写图片描述

将mybatis-3.4.2.jar文件拷贝到web项目的WEB-INF/lib文件夹中即可

MySQL的jar包路径同上

No.3 拷贝Mybatis的配置文件模版到web项目

首先找到我们之前下载好的Mybatis源代码

这里写图片描述

将其解压,并打开解压所得的文件夹

这里写图片描述

因为配置文件模版的路径比较深,这里就不截图展示了。

\mybatis-3-mybatis-3.4.2\src\test\java\org\apache\ibatis\submitted\complex_property\Configuration.xml

将这个配置复制到web项目中,建议在web项目中创建一个包用于存放配置文件,如图所示:

如果使用的是Maven搭建的项目结构,建议将配置文件存放在resources文件下,一样的,也是从包名开始检索

这里写图片描述

No.4 简单修改配置文件

主要是修改配置文件中的数据库驱动、地址、数据库名、用户名、密码等信息
另外注释掉一部分暂时不需要的代码

<?xml version="1.0" encoding="UTF-8" ?>
<!--

       Copyright 2009-2016 the original author or authors.

       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.

-->
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<!--
  <settings>
    <setting name="useGeneratedKeys" value="false"/>
    <setting name="useColumnLabel" value="true"/>
  </settings>

  <typeAliases>
    <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/>
  </typeAliases>
-->
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC">
        <property name="" value=""/>
      </transactionManager>
      <dataSource type="UNPOOLED">
        <!-- 数据库驱动,此处使用的是MySQL数据库 -->
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <!-- 数据库地址以及数据库的名字 -->
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/mircro_message"/>
        <!-- 数据库用户名 -->
        <property name="username" value="user"/>
        <!-- 数据库密码 -->
        <property name="password" value="123456"/>
      </dataSource>
    </environment>
  </environments>
<!--
  <mappers>
    <mapper resource="org/apache/ibatis/submitted/complex_property/User.xml"/>
  </mappers>
-->
</configuration>

No.5 创建类访问数据库

由于在Mybatis中主要是通过SqlSeesion来访问数据库,所以我们需要单独使用一个类来真正的去操作数据库。

这里写图片描述

package com.baofeidyz.db;


import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * 
 * 描述:访问数据库类
 * 开发人员:暴沸
 * 开发时间: 2017年2月7日 下午8:37:48
 * 联系方式:admin@baofeidyz.com 
 *
 */
public class DBAccess {

    public SqlSession getSqlSession() throws IOException{
        //通过配置文件获取数据库连接信息
        //配置文件的路径是从src包的根路径下面开始写
        //因为我使用的是Maven项目,所以我这里就直接在resources目录下创建了一个mybatis文件夹存放有关mybatis的配置文件,后面我会把项目源代码放在csdn中。
        Reader reader = Resources.getResourceAsReader("mybatis/Configuration.xml");
        //通过配置信息构建一个SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //再通过sqlSessionFactory打开一个数据库会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //需要关闭连接
        return sqlSession;
    }

}

No.6 创建DAO层对数据库进行操作

package com.baofeidyz.dao;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.baofeidyz.db.DBAccess;
import com.baofeidyz.entity.Message;

/**
 * 
 * 描述:和message表相关的数据库操作
 * 开发人员:暴沸
 * 开发时间: 2017年2月7日 下午9:08:11
 * 联系方式:admin@baofeidyz.com 
 *
 */
public class MessageDao {

    /**
     * 根据查询条件查询消息列表
     */
    public List<Message> queryMessageList(String command,String description){
        DBAccess dbAccess = new DBAccess();
        SqlSession sqlSession = null;
        try {
            //通过daAccess对象获取sqlSession对象
            sqlSession = dbAccess.getSqlSession();
            //通过sqlSession执行SQL语句
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }
        return null;
    }
    //测试类,请自行删除
    public static void main(String[] args) {
        MessageDao messageDao = new MessageDao();
        messageDao.queryMessageList("123", "123");
    }

}

git地址:https://code.csdn.net/baofeidyz/mybatisstudypratice02mircromessage_no-2/tree/master
资源下载地址:http://download.csdn.net/detail/baofeidyz/9749383 解压密码 baofeidyz

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值