1.创建表emp
CREATE DATABASE mybatis; USE mybatis; CREATE TABLE emp( id INT(11) PRIMARY KEY AUTO_INCREMENT, last_name VARCHAR(255), gender CHAR(1), email VARCHAR(255) );
2.编写javaBean
package cn.bdqn.mybatis.been; public class Emp { private Integer id; private String last_name; private String email; private String gender; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } 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 "Emp [id=" + id + ", last_name=" + last_name + ", email=" + email + ", gender=" + gender + "]"; } }
3.编写Emp的DAO接口
package cn.bdqn.mybatis.dao;
import cn.bdqn.mybatis.been.Emp;
/**
* 这个接口不用写实现类,是用来给empMapper.xml进行动态绑定的
* @author Administrator
*
*/
public interface EmpMapper {
public Emp getEmpById(Integer id);
}
3添加需要的jar包
第一个是log4j的jar包(记录日志文件所需) 第二个是mybatis的核心jar包(必须),第三个是mysql的驱动jar包(必须)
4.创建需要的配置文件
第一个是SQL映射配置文件,所有的sql语句都写在这个配置文件里面
<?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">
<!--namespace:名称空间 值是Emp的DAO接口的全限定名称-->
<mapper namespace="cn.bdqn.mybatis.dao.EmpMapper">
<!--
id,唯一标识
resultType:返回值类型,bean的全限定名称
#{id},从传递过来的参数中取出id值,相当于换位符
public Emp getEmpById(Integer id);
id是我们接口中方法的方法名称
-->
<select id="getEmpById" resultType="cn.bdqn.mybatis.been.Emp">
select * from emp where id = #{id}
</select>
</mapper>
第二个是log4j记录日志所需
第三个是mybatis全局配置文件
<?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="admin"/> </dataSource> </environment> </environments> <!--将我们写好的SQL映射文件一定要注册到全局配置文件中 --> <mappers> <mapper resource="empMapper.xml"/> </mappers> </configuration>
5,最后编写测试类
package cn.bdqn.mybatis.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 cn.bdqn.mybatis.been.Emp; import cn.bdqn.mybatis.dao.EmpMapper; public class MyBatisTest_01 { public static void main(String[] args) throws IOException { //1.获取SqlSessionFactory对象 String resource="mybatis-config.xml"; InputStream ras = Resources.getResourceAsStream(resource); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(ras); //2.获取SqlSession对象 SqlSession openSession = build.openSession(); try{ //3.获取接口的实现类对象 //会为接口自动创建一个代理对象,代理对象去执行增删改查方法 EmpMapper mapper = openSession.getMapper(EmpMapper.class); Emp empById = mapper.getEmpById(1); System.out.println(empById); }finally{ openSession.close(); } } }