首页 > 其他 > 详细

7 Mybatis - 接口代理实现dao + 注解 开发 - 单表(案例)

时间:2020-11-28 09:34:02      阅读:31      评论:0      收藏:0      [点我收藏+]

Mybatis - 接口代理实现dao + 注解 开发

基本架构

  • domain包(bean)

    • Student实体类
  • 配置文件

    • 核心配置文件MyBatisConfig.xml

      • 引入jdbc.properties

      • 引入log4j.properties

      • 配置映射关系

        <!--配置映射关系-->
        <mappers>
           <package name="com.itheima.mapper"/>
        </mappers>
        
  • mapper包(dao)

    • StudentMapper接口
  • service包

    • StudentService接口
    • impl包
      • StudentServiceImpl实现类

单表CRUD

mapper层

public interface StudentMapper {
    //修改操作
    @Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
    public abstract Integer update(Student stu);
}

service层

public interface StudentService {
    void update();
}

public class StudentServiceImpl {
    public void update() throws Exception{
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        //2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.获取StudentMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //5.调用实现类对象中的方法,接收结果
        Student stu = new Student(4,"赵六",36);
        Integer result = mapper.update(stu);
        //6.处理结果
        System.out.println(result);
        //7.释放资源
        sqlSession.close();
        is.close();
    }
}

7 Mybatis - 接口代理实现dao + 注解 开发 - 单表(案例)

原文:https://www.cnblogs.com/60kmph/p/14051525.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!