写在前面的话:在慕课网学习的时候,老师使用的是一般的web结构,更多的时候都是使用的mevan项目结构,所以这里呢就改成了mevan项目结构。
【强调】在博客完成中,突然发现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将这个配置复制到web项目中,建议在web项目中创建一个包用于存放配置文件,如图所示:
如果使用的是Maven搭建的项目结构,建议将配置文件存放在resources文件下,一样的,也是从包名开始检索
主要是修改配置文件中的数据库驱动、地址、数据库名、用户名、密码等信息 另外注释掉一部分暂时不需要的代码
<?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>由于在Mybatis中主要是通过SqlSeesion来访问数据库,所以我们需要单独使用一个类来真正的去操作数据库。
git地址:https://code.csdn.net/baofeidyz/mybatisstudypratice02mircromessage_no-2/tree/master 资源下载地址:http://download.csdn.net/detail/baofeidyz/9749383
新闻热点
疑难解答