写在前面的,刚刚开始学习使用Mybatis,网上参考了很多人的Demo示例,但还是踩了很多坑,运行不起来,经过重重磨难,终于让他跑起来了。
一:文件夹目录
二:新建项目时遇到的几个问题
1.相应的Package创建完毕后,右击new无法新建java类
右击java文件夹Make Directory As:Source Root。Test文件夹同理,Make Directory As: Tset Source Root
2.建议相应的包把dao,bean,mapper等放进去,这样后续引用比较方便
3.mybatis,mysql的相关配置建议放在resource文件夹下面
三:配置项目所需要的jar包,pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mybatis</groupId>
<artifactId>Batis</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Batis Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Batis</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
四:配置log4.proprttites
### 设置Logger输出级别和输出目的地 ###
log4j.rootLogger=debug,stdout,logfile
### 把日志信息输出到控制台 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAp