1.项目结构
2.java类代码
①Employee.java
package com.atguigu.mybatis.bean; 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 + "]"; } }
②MybatisTest.java
package com.atguigu.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 org.junit.Test; import com.atguigu.mybatis.bean.Employee; public class MybatisTest { @Test public void test() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession openSession =sqlSessionFactory.openSession(); try { Employee employee= openSession.selectOne("com.atguigu.mybatis.EmployeeMapper.selectEmp", 1); System.out.println(employee); } finally { openSession.close(); } } }
a.resource: mybatis的配置文件。
b.使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
c.构建sqlSession的工厂
d.创建sqlSession对象
e.com.atguigu.mybatis.EmployeeMapper是EmployeeMapper.xml文件中mapper标签的namespace属性的值,selectEmp是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
3.配置文件
①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.atguigu.mybatis.EmployeeMapper"> <!-- namespace:名称空间 id:唯一标识 resultType:返回值类型 #{id}:从传递过来的参数中取出id值 --> <select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee"> select id,last_name lastName,email,gender from tbl_employee where id = #{id} </select> </mapper>
把实体类放在配置文件中,并完成实体类与sql(方法)映射
注:数据库字段与实体类字段 一 一 对应
②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" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="111111" /> </dataSource> </environment> </environments> <mappers> <!-- 注册EmployeeMapper.xml文件, --> <mapper resource="EmployeeMapper.xml"/> </mappers> </configuration>
配置数据库连接,包括事物管理,并完成实体类映射文件(EmployeeMapper.xml)注册