国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

【初學(xué)Mybatis】No.2 Mybatis的下載和搭建核心架構(gòu)(Maven搭建)

2019-11-11 02:05:08
字體:
供稿:網(wǎng)友

寫在前面的話:在慕課網(wǎng)學(xué)習(xí)的時候,老師使用的是一般的web結(jié)構(gòu),更多的時候都是使用的mevan項目結(jié)構(gòu),所以這里呢就改成了mevan項目結(jié)構(gòu)。

No.1 下載地址

【強調(diào)】在博客完成中,突然發(fā)現(xiàn)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

將這個配置復(fù)制到web項目中,建議在web項目中創(chuàng)建一個包用于存放配置文件,如圖所示:

如果使用的是Maven搭建的項目結(jié)構(gòu),建議將配置文件存放在resources文件下,一樣的,也是從包名開始檢索

這里寫圖片描述

No.4 簡單修改配置文件

主要是修改配置文件中的數(shù)據(jù)庫驅(qū)動、地址、數(shù)據(jù)庫名、用戶名、密碼等信息 另外注釋掉一部分暫時不需要的代碼

<?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"> <!-- 數(shù)據(jù)庫驅(qū)動,此處使用的是MySQL數(shù)據(jù)庫 --> <property name="driver" value="com.mysql.jdbc.Driver"/> <!-- 數(shù)據(jù)庫地址以及數(shù)據(jù)庫的名字 --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mircro_message"/> <!-- 數(shù)據(jù)庫用戶名 --> <property name="username" value="user"/> <!-- 數(shù)據(jù)庫密碼 --> <property name="passWord" value="123456"/> </dataSource> </environment> </environments><!-- <mappers> <mapper resource="org/apache/ibatis/submitted/complex_property/User.xml"/> </mappers>--></configuration>

No.5 創(chuàng)建類訪問數(shù)據(jù)庫

由于在Mybatis中主要是通過SqlSeesion來訪問數(shù)據(jù)庫,所以我們需要單獨使用一個類來真正的去操作數(shù)據(jù)庫。

這里寫圖片描述

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;/** * * 描述:訪問數(shù)據(jù)庫類 * 開發(fā)人員:暴沸 * 開發(fā)時間: 2017年2月7日 下午8:37:48 * 聯(lián)系方式:admin@baofeidyz.com * */public class DBaccess { public SqlSession getSqlSession() throws IOException{ //通過配置文件獲取數(shù)據(jù)庫連接信息 //配置文件的路徑是從src包的根路徑下面開始寫 //因為我使用的是Maven項目,所以我這里就直接在resources目錄下創(chuàng)建了一個mybatis文件夾存放有關(guān)mybatis的配置文件,后面我會把項目源代碼放在csdn中。 Reader reader = Resources.getResourceAsReader("mybatis/Configuration.xml"); //通過配置信息構(gòu)建一個SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //再通過sqlSessionFactory打開一個數(shù)據(jù)庫會話 SqlSession sqlSession = sqlSessionFactory.openSession(); //需要關(guān)閉連接 return sqlSession; }}

No.6 創(chuàng)建DAO層對數(shù)據(jù)庫進行操作

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表相關(guān)的數(shù)據(jù)庫操作 * 開發(fā)人員:暴沸 * 開發(fā)時間: 2017年2月7日 下午9:08:11 * 聯(lián)系方式:admin@baofeidyz.com * */public class MessageDao { /** * 根據(jù)查詢條件查詢消息列表 */ public List<Message> queryMessageList(String command,String description){ DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { //通過daAccess對象獲取sqlSession對象 sqlSession = dbAccess.getSqlSession(); //通過sqlSession執(zhí)行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


上一篇:遞歸法解全排列

下一篇:PAT BASIC 1012

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 弥勒县| 镇平县| 沙湾县| 成都市| 黄梅县| 兴化市| 丁青县| 正蓝旗| 广汉市| 宜宾县| 定西市| 白城市| 香河县| 报价| 海城市| 张北县| 嘉鱼县| 平泉县| 通海县| 泰顺县| 独山县| 汪清县| 合水县| 衡水市| 新疆| 濉溪县| 同心县| 北票市| 集安市| 营口市| 大名县| 旬阳县| 旬邑县| 镇沅| 合川市| 凌源市| 徐汇区| 临夏市| 通州区| 盘山县| 石柱|