准备源代码包
下载源代码 3.4.6 版本 https://github.com/mybatis/mybatis-3/releases?page=2 通过 idea 导入然后回自动下载所有依赖,根据 3.4.6 版本的 pom.xml 找到依赖的 mybatis-parent 版本
< parent>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-parent</ artifactId>
< version> 29</ version>
< relativePath />
</ parent>
同样需要从 github 下载 https://github.com/mybatis/parent/releases/tag/mybatis-parent-29
准备测试环境
代码结构 创建 Maven 项目,并添加依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< 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/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< groupId> org.example</ groupId>
< artifactId> mybatisjsnm</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< properties>
< junit.version> 4.12</ junit.version>
< mybatis.version> 3.4.6</ mybatis.version>
< mysql.version> 8.0.16</ mysql.version>
< druid.version> 1.0.9</ druid.version>
</ properties>
< dependencies>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.17</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> ${mybatis.version}</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> ${mysql.version}</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> ${druid.version}</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> ${junit.version}</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.javassist</ groupId>
< artifactId> javassist</ artifactId>
< version> 3.26.0-GA</ version>
</ dependency>
< dependency>
< groupId> ognl</ groupId>
< artifactId> ognl</ artifactId>
< version> 3.0.6</ version>
</ dependency>
</ dependencies>
< build>
< finalName> mybatis</ finalName>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins</ groupId>
< artifactId> maven-compiler-plugin</ artifactId>
< configuration>
< source> 1.8</ source>
< target> 1.8</ target>
< encoding> UTF-8</ encoding>
</ configuration>
</ plugin>
</ plugins>
</ build>
</ project>
创建 mapper 接口 CountryMapper
public interface CountryMapper {
public List < Country > selectAll ( ) ;
}
创建实体类 Country
public class Country {
private Long id;
private String countryname;
private String countrycode;
. . .
}
create table ` country` (
` id` int ( 11 ) ,
` countryname` varchar ( 765 ) ,
` countrycode` varchar ( 765 )
) ;
创建 CountryMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " org.example.mybatisjsnm.mapper.CountryMapper" >
< select id = " selectAll" resultType = " Country" >
select id,countryname,countrycode from country
</ select>
</ mapper>
配置 log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR,stdout
# Mybatis logging configuration...
log4j.logger.com.simple.mapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.layout.ConversionPattern=%5p [%t] - %m%n
配置 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
< configuration>
< settings>
< setting name = " logImpl" value = " STDOUT_LOGGING" />
</ settings>
< typeAliases>
< package name = " org.example.mybatisjsnm.pojo" />
</ typeAliases>
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " JDBC" >
< property name = " " value = " " />
</ transactionManager>
< dataSource type = " POOLED" >
< property name = " driver" value = " com.mysql.cj.jdbc.Driver" />
< property name = " username" value = " root" />
< property name = " password" value = " root" />
< property name = " url" value = " jdbc:mysql://localhost:3306/mybatis" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< package name = " org.example.mybatisjsnm.mapper" />
</ mappers>
</ configuration>
创建运行主类 MyBatisTest
public class MyBatisTest {
@Test
public void testMyBatisBuild ( ) throws IOException {
InputStream input = Resources . getResourceAsStream ( "mybatis-config.xml" ) ;
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder ( ) . build ( input) ;
SqlSession sqlSession = sessionFactory. openSession ( ) ;
CountryMapper mapper = sqlSession. getMapper ( CountryMapper . class ) ;
List < Country > countries = mapper. selectAll ( ) ;
}
}
运行程序
集合成源代码
代码结构 首先将 mybatis-3-mybatis-3.4.6 、parent-mybatis-parent-29 导入到刚刚测试的相同 project 中,导入方式 File -> Project Structure -> Modules -> +号 然后分别在 mybatis-3-mybatis-3.4.6 、parent-mybatis-parent-29 执行下面命令
mvn clean install -Dmaven.test.skip = true
如果 mybatis-3-mybatis-3.4.6 显示 maven-pdf-plugin 相关的失败的话,把这个插件注释掉
然后再尝试 debug、运行代码,看看是否能跳转到你相应代码上