1.代理方式开发是主流 2.Mapper接口开发方法只需要编写Mapper接口(相当于Dao接口),然后由Mybatis根据接口创建动态代理对象 Mapper接口开发需要遵循以下规范 一一对应 UserMapper.xml !-- 根据
1.代理方式开发是主流
2.Mapper接口开发方法只需要编写Mapper接口(相当于Dao接口),然后由Mybatis根据接口创建动态代理对象
Mapper接口开发需要遵循以下规范
一一对应
UserMapper.xml
<!-- 根据id查询--><select id="findById" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
UserMapper接口(dao层)
public User findById(int Id);ServiceTest类
public class ServiceTest {public static void main(String[] args) throws IOException {
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findById(1);
System.out.println(user);
}
}