1.环境搭建
1.1.pom文件
<?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>com.boxuegu.javaee</groupId>
<artifactId>mybatis-source-learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis-source-learn</name>
<description>mybatis-source-learn</description>
<properties>
<!-- 统一编译版本 -->
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- mybatis版本 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 数据库连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
1.2.数据库脚本:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for bxg_employee
-- ----------------------------
DROP TABLE IF EXISTS `bxg_employee`;
CREATE TABLE `bxg_employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`last_name` varchar(255) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of bxg_employee
-- ----------------------------
INSERT INTO `bxg_employee` VALUES ('1', 'zhangsan', '1', 'zhangsan@itcast.cn');
1.3.mybatis-config.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Cofig 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- mybatis插件机制,这里不讲(用的不多) -->
<!-- <plugins>-->
<!-- <plugin interceptor="com.boxuegu.javaee.mybatissourcelearn.MyIntercepts">-->
<!-- <property name="test" value="testvalue"/>-->
<!-- </plugin>-->
<!-- </plugins>-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml"></mapper>
</mappers>
</configuration>
1.4.EmployeeMapper:
public interface EmployeeMapper {
public Employee getEmployeeById(Integer id);
}
1.5.Employee:
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
}
2.测试接口
public class Test {
public static void main(String[] args) {
String resource = "myb