寫在前面的話:在慕課網(wǎng)學(xué)習(xí)的時候,老師使用的是一般的web結(jié)構(gòu),更多的時候都是使用的mevan項目結(jié)構(gòu),所以這里呢就改成了mevan項目結(jié)構(gòu)。
【強調(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>此步驟如果是使用的Maven搭建可省略
找到下載好的這個壓縮包并解壓
打開解壓所得的文件夾
將mybatis-3.4.2.jar文件拷貝到web項目的WEB-INF/lib文件夾中即可
MySQL的jar包路徑同上
首先找到我們之前下載好的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文件下,一樣的,也是從包名開始檢索
主要是修改配置文件中的數(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>由于在Mybatis中主要是通過SqlSeesion來訪問數(shù)據(jù)庫,所以我們需要單獨使用一個類來真正的去操作數(shù)據(jù)庫。
git地址:https://code.csdn.net/baofeidyz/mybatisstudypratice02mircromessage_no-2/tree/master 資源下載地址:http://download.csdn.net/detail/baofeidyz/9749383
新聞熱點
疑難解答