项目结构
Employee.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"> <!-- 每个实体类都对应着Mybatis的一个命名空间。namespace属性值相当于id,不允许重复 --> <!-- 命名空间,一般情况下,在当前空间中声明的是,实体类对应全路径 --> <!-- <mapper namespace="com.mybatis.entity.Employee"> --><!-- select标签执行查询 结果集类型(resultType)是实体类对象 方法传入参数类型(parameterType) #{}相当于占位符,代表传过来的参数 select的id和方法名称一致 --> <mapper namespace="com.mybatis.dao.EmployeeDao"> <select id="selAll" parameterType="String" resultType="com.mybatis.entity.Employee"> select * from emp </select> <select id="selOne" parameterType="String" resultType="com.mybatis.entity.Employee"> select * from emp where id = #{id} </select> </mapper>
EmployeeDao.java
package com.mybatis.dao; import java.util.List; import com.mybatis.entity.Employee; public interface EmployeeDao{ //查询所有 返回是集合 不需要传递参数 public List<Employee> selAll(); //查询单一 返回是对象 需要传递参数 public Employee selOne(String id); }
TestImp.java
package com.mybatis.test; import java.io.IOException; import java.io.Reader; import java.util.List; 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.mybatis.dao.EmployeeDao; import com.mybatis.entity.Employee; public class TestImp { public void wan(){ //创建一个sqlSession Reader reader; try { String resource = "recourse.xml"; reader = Resources.getResourceAsReader(resource); //创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //通过数据库会话工厂开启跟数据库的一次会话 //true表示自动提交,否则需要使用commit方法才会提交,默认false方法 SqlSession sqlSession = sqlSessionFactory.openSession(true); //创建接口对象,是sqlsession对象通过动态代理自动创建 //反射对象 EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class); System.out.println(employeeDao); //使用创建好的接口对象调用好的接口方法 Employee emp = employeeDao.selOne("2"); System.out.println(emp.getName()); List<Employee> list =employeeDao.selAll(); System.out.println(list.size()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { TestImp testImp = new TestImp(); testImp.wan(); } }
resourse.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> <!-- 引入属性文件 --> <properties resource="dbconfig.properties"></properties> <environments default="development"> <environment id="development"><!-- 开发者模式 --> <transactionManager type="JDBC"/><!-- 事务管理器 --> <dataSource type="POOLED"><!-- 数据源 池连接--> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 对应实体类的配置文件 --> <mappers> <mapper resource="mappers/Employee.xml"/> </mappers> </configuration>
其余文件见:初入Mybatis
运行结果: