什么是mybatis?
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
怎样下载mybatis
访问mybatis的github网址(https://github.com/mybatis/mybatis-3) 拉到下方选择Download Latest选择相应版本即可下载
第一个项目
数据库准备
创建数据库mybatis,创建表tbl_employee
CREATE TABLE tbl_employee
( id
int(10) NOT NULL AUTO_INCREMENT COMMENT '主键', last_name
varchar(255) DEFAULT NULL COMMENT '名称', gender
varchar(10) DEFAULT NULL COMMENT '性别', email
varchar(255) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
插入一条数据
INSERT INTO mybatis
.tbl_employee
(id
, last_name
, gender
, email
) VALUES (1, '张三', '1', 'ZFH_FISH@163.COM');
项目准备
创建项目MybatisLearn
导入相关jar包
junit-4.10.jar junit测试包
log4j-core-2.11.2.jar 日志包
mybatis-3.5.1.jar mybatis核心包
mysql-connector-java-5.1.30.jar 数据库驱动包
创建EmployeeMapper javabean
package com.fish.mybatis;
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
}
}
创建mybatis主配置文件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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" /><!-- driver -->
<property name="url" value="jdbc:mysql:///mybatis" /><!-- url -->
<property name="username" value="root" /><!-- 用户名 -->
<property name="password" value="root" /><!-- 密码 -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="config/EmployeeMapper.xml" /><!-- 需要映射的文件 后面会用到 -->
</mappers>
</configuration>
配置映射文件EmployeeMapper.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="com.fish.config.EmployeeMapper"><!--命名空间 最好是唯一的-->
<select id="selectEmp"
resultType="com.fish.mybatis.Employee">
select id,last_name lastName,email,gender from tbl_employee where id = #{id}<!--#{id}传递的参数-->
</select>
</mapper>
编写测试类Test.java
package com.fish.test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.fish.mybatis.Employee;
public class Test {
@org.junit.Test
public void test() throws IOException {
String resource = "config/mybatis-config.xml";//读取mybatis主配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//创建SqlSessionFactory
SqlSession openSession = sqlSessionFactory.openSession();//获取SqlSession
Employee selectOne = openSession.selectOne("com.fish.config.EmployeeMapper.selectEmp", 1);
//com.fish.config.EmployeeMapper.selectEmp 使用命名空间.id调用
//第二个参数是向方法传递的值
System.out.println(selectOne);//输出结果
openSession.close();//关闭session
}
}
运行结果
Employee [id=1, lastName=张三, email=ZFH_FISH@163.COM, gender=1]
项目结构
+