思路如下:
- 新建java项目
- 项目结构为:
mybatis_2
src
com.shen.bean
Employee.java
com.shen.handler
Handler.java
conf
mybatis-config.xml
EmployeeMapper.xml
lib
mybatis-3.4.5.jar
mysql-connector-java-5.1.44-bin.jar
代码如下
- 操作注意:
(1)新建的lib文件夹,加入jar包,右键点击Build Path
(2)新建的conf文件夹,加入xml配置文件后,右键点击Build Path,否则找不到xml文件报错
2.数据库新建如下表格:
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
)
3.bean文件
package com.shen.bean;
public class Employee {
private Integer id;
private String lastName;
private String gender;
private String email;
public Employee(Integer id, String lastName, String gender, String email) {
super();
this.id = id;
this.lastName = lastName;
this.gender = gender;
this.email = email;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", gender="
+ gender + ", email=" + email + "]";
}
}
- 核心配置文件
<?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" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
- mapper配置文件
<?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.shen.EmployeeMapper">
<!--
namespace:命名空间
id:sql语句的唯一标识
resultType:返回值类型
#{id}:执行sql传入的参数
-->
<select id="emp" resultType="com.shen.bean.Employee">
select id,last_name as lastName,gender,email from tbl_employee where id = #{id}
</select>
</mapper>
- Handler文件
package com.shen.handler;
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.shen.bean.Employee;
public class Handler {
public static void test() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
Employee employee = session.selectOne("com.shen.EmployeeMapper.emp", 1);
System.out.println(employee);
} finally {
session.close();
}
}
public static void main(String[] args) throws IOException {
test();
}
}