3.mapper代理方式(程序员只需mapper接口(相当于Dao的接口))
3.1 思路:1.程序员只需要写mapper接口(相当于Dao接口),mybatis可以自动生成mapper接口实现类的代理对象
2.程序员需要编写mapper.xml映射文件
3.开发规范:1.在mapper.xml中namespace等于mapper接口的地址
-->
<mapper namespace="top.haoyeyin.mapper.StudentMapper">
2.mapper接口中方法名和mapper.xml中的statement中的id一致
public Student findStudentById(int id) throws Exception;
总结:1.mapper.java public interface StudentMapper {
//根据ID查询学生信息
public Student findStudentById(int id) throws Exception;
2.mapper.xml <mapper namespace="top.haoyeyin.mapper.StudentMapper">
<!-- 映射文件中配置多种sql语句 -->
<!-- 通过select语句查询,id用于标识映射文件中的sql,所以id为statement的ID
将来sql语句封装到mapperstatement对象中
paramterType:指定输入参数类型,这里ID指定int型
#{}:占位符
#{id}:id表示输入的参数,id是参数名称,如果输入是简单类型#{}中的参数名可以任意,可以是
value或其名称
resultType:指定sql输出结果对应映射java对象,select指定的 resulType表示单条记录
所映射的Java对象
-->
<select id="findStudentById" parameterType="int" resultType="top.haoyeyin.pojo.Student">
select * from student where id=#{id}
</select>
3.测试方法
package top.haoyeyin.mapper;
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.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
public class StudentMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setup() throws Exception{
BasicConfigurator.configure();
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂sessionFactory,传入SqlMapConfig.xml配置信息
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
@Test
public void testFindStudentById() {
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建StudentMapper对象
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//调用studentMapper的方法
try {
studentMapper.findStudentById(4);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.1 思路:1.程序员只需要写mapper接口(相当于Dao接口),mybatis可以自动生成mapper接口实现类的代理对象
2.程序员需要编写mapper.xml映射文件
3.开发规范:1.在mapper.xml中namespace等于mapper接口的地址
注意:使用mapper代理的方法进行开发,namespace具有特殊的意义<!-- namespace命名空间 ,作用对sql进行分类化管理,理解sql隔离,namespace等于mapper接口地址
-->
<mapper namespace="top.haoyeyin.mapper.StudentMapper">
2.mapper接口中方法名和mapper.xml中的statement中的id一致
4.mapper.java接口中方法的返回类型和mapper.xml中resultType指定的类型一致3.mapper.java接口中的输入参数类型和mapper.xml中statement的parameterType的指定 类型一致
public Student findStudentById(int id) throws Exception;
总结:1.mapper.java public interface StudentMapper {
//根据ID查询学生信息
public Student findStudentById(int id) throws Exception;
2.mapper.xml <mapper namespace="top.haoyeyin.mapper.StudentMapper">
<!-- 映射文件中配置多种sql语句 -->
<!-- 通过select语句查询,id用于标识映射文件中的sql,所以id为statement的ID
将来sql语句封装到mapperstatement对象中
paramterType:指定输入参数类型,这里ID指定int型
#{}:占位符
#{id}:id表示输入的参数,id是参数名称,如果输入是简单类型#{}中的参数名可以任意,可以是
value或其名称
resultType:指定sql输出结果对应映射java对象,select指定的 resulType表示单条记录
所映射的Java对象
-->
<select id="findStudentById" parameterType="int" resultType="top.haoyeyin.pojo.Student">
select * from student where id=#{id}
</select>
3.测试方法
package top.haoyeyin.mapper;
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.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;
public class StudentMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setup() throws Exception{
BasicConfigurator.configure();
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂sessionFactory,传入SqlMapConfig.xml配置信息
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
@Test
public void testFindStudentById() {
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建StudentMapper对象
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//调用studentMapper的方法
try {
studentMapper.findStudentById(4);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文详细介绍了MyBatis框架中的Mapper代理机制,包括如何通过编写接口而非实现类来简化DAO层的开发过程,以及如何配合XML映射文件完成数据库操作。文中还提供了具体的示例代码,帮助读者更好地理解和应用这一机制。
596

被折叠的 条评论
为什么被折叠?



