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

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

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

2019-11-11 00:43:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

No.1 下載地址

【強(qiáng)調(diào)】在博客完成中,突然發(fā)現(xiàn)3.4.2中沒(méi)有org.apache.ibatis.session.SqlSession;這個(gè)類,請(qǐng)各位使用3.4.1版本,以后找時(shí)間再去完成這兩個(gè)版本的差異性更新

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

我們需要下載一個(gè)打包好的jar包以及源代碼,如圖所示:

這里寫圖片描述

如果你是使用的Maven搭建的項(xiàng)目,可以將下面的代碼拷貝到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項(xiàng)目

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

找到下載好的這個(gè)壓縮包并解壓

這里寫圖片描述

打開解壓所得的文件夾

這里寫圖片描述

將mybatis-3.4.2.jar文件拷貝到web項(xiàng)目的WEB-INF/lib文件夾中即可

MySQL的jar包路徑同上

No.3 拷貝Mybatis的配置文件模版到web項(xiàng)目

首先找到我們之前下載好的Mybatis源代碼

這里寫圖片描述

將其解壓,并打開解壓所得的文件夾

這里寫圖片描述

因?yàn)榕渲梦募0娴穆窂奖容^深,這里就不截圖展示了。

/mybatis-3-mybatis-3.4.2/src/test/java/org/apache/ibatis/submitted/complex_PRoperty/Configuration.xml

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

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

這里寫圖片描述

No.4 簡(jiǎn)單修改配置文件

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

<?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ù)庫(kù)驅(qū)動(dòng),此處使用的是MySQL數(shù)據(jù)庫(kù) --> <property name="driver" value="com.mysql.jdbc.Driver"/> <!-- 數(shù)據(jù)庫(kù)地址以及數(shù)據(jù)庫(kù)的名字 --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mircro_message"/> <!-- 數(shù)據(jù)庫(kù)用戶名 --> <property name="username" value="user"/> <!-- 數(shù)據(jù)庫(kù)密碼 --> <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)建類訪問(wèn)數(shù)據(jù)庫(kù)

由于在Mybatis中主要是通過(guò)SqlSeesion來(lái)訪問(wèn)數(shù)據(jù)庫(kù),所以我們需要單獨(dú)使用一個(gè)類來(lái)真正的去操作數(shù)據(jù)庫(kù)。

這里寫圖片描述

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

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

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ù)庫(kù)操作 * 開發(fā)人員:暴沸 * 開發(fā)時(shí)間: 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 { //通過(guò)daAccess對(duì)象獲取sqlSession對(duì)象 sqlSession = dbAccess.getSqlSession(); //通過(guò)sqlSession執(zhí)行SQL語(yǔ)句 } catch (IOException e) { e.printStackTrace(); }finally{ sqlSession.close(); } return null; } //測(cè)試類,請(qǐng)自行刪除 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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武威市| 永城市| 霍邱县| 舞钢市| 湘潭市| 河南省| 南皮县| 江油市| 布尔津县| 垦利县| 东丽区| 马山县| 清徐县| 寿宁县| 文登市| 石首市| 平江县| 荔浦县| 庆城县| 阆中市| 林州市| 广河县| 江永县| 栾城县| 平利县| 慈溪市| 邛崃市| 兰溪市| 平凉市| 航空| 鄂州市| 宜川县| 珠海市| 保定市| 蒙阴县| 鞍山市| 宁国市| 读书| 彰化县| 郸城县| 泰宁县|