实现步骤:创建一个接口
1.接口的包名和接口名与mapper.xml中<mapper>标签的namespace相同
2.接口中方法名和mapper.xml标签的id属性相同
3.在mybatis.xml中使用<package>标签进行扫描接口和mapper.xml
框架结构:
框架结构
mybatis.xml文件:(此处mappers标签内需要使用package标签,该标签的name属性表名接口及mapper文件所在包)
<?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>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<typeAlias type="com.pojo.Emp" alias="emp"/>
</typeAliases>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@192.168.74.3:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--mapper接口及xml文件所在包-->
<package name="com.mapper"></package>
</mappers>
</configuration>
mapper.xml文件:(此处注意namespace属性需要与接口吻合)
<?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.mapper.EmpMapper">
<select id="selAll" resultType="emp">
select * from emp
</select>
<!--多参数时,不需要写parameterType,因为接口上已经有参数声明,如果想改变#{}内的内容,则需要接口的相应参数名前加上@param(内容名称)注解即可。该操作实质是将参数转换为map,其中@param(key)-->
<select id="selByEnameAndEmpNo" resultType="emp">
select * from emp where ename=#{0} and #{1}
</select>
</mapper>
Test类:
package com;
import com.pojo.Emp;
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 java.io.IOException;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
SqlSession sqlSession = factory.openSession();
List<Emp> emps = sqlSession.selectList("com.mapper.EmpMapper.selAll");
for (int i = 0; i < emps.size(); i++) {
System.out.println(emps.get(i));
}
}
}
Test类也可以用如下方式:
package com;
import com.mapper.EmpMapper;
import com.pojo.Emp;
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 java.io.IOException;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
SqlSession sqlSession = factory.openSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
List<Emp> emps = mapper.selAll();
for (int i = 0; i < emps.size(); i++) {
System.out.println(emps.get(i));
}
}
}
网友评论