当前位置: 首页 > news >正文

苏州市城乡和建设局网站首页商机网创业好项目

苏州市城乡和建设局网站首页,商机网创业好项目,个人网站的设计,做网站建设哪家好【Java闭关修炼】MyBatis-接口代理的方式实现Dao层实现规则代码实现代理对象分析接口代理方式小结实现规则 映射配置文件中的名称空间必须和Dao层接口的全类名相同映射配置文件的增删改查标签的id属性必须和Dao层接口方法的参数相同映射配置文件中的增删改查标签的parameterTyp… 【Java闭关修炼】MyBatis-接口代理的方式实现Dao层实现规则代码实现代理对象分析接口代理方式小结实现规则 映射配置文件中的名称空间必须和Dao层接口的全类名相同映射配置文件的增删改查标签的id属性必须和Dao层接口方法的参数相同映射配置文件中的增删改查标签的parameterType属性必须和Dao层接口方法的参数相同映射配置文件中的增删改查标签中的resultType属性必须和Dao层接口方法的返回值相同 代码实现 删除mapper层接口的实现类 修改映射配置文件 修改service层接口的实现类 采用接口代理方式实现功能 mapper层接口 package com.itheima.mapper;import com.itheima.bean.Student;import java.util.List;/*持久层接口*/// 接口的名称要和namespace一样 public interface StudentMapper {//查询全部public abstract ListStudent selectAll();//根据id查询public abstract Student selectById(Integer id);//新增数据public abstract Integer insert(Student stu);//修改数据public abstract Integer update(Student stu);//删除数据public abstract Integer delete(Integer id);//多条件查询public abstract ListStudent selectCondition(Student stu);//根据多个id查询public abstract ListStudent selectByIds(ListInteger ids); } 映射配置文件 ?xml version1.0 encodingUTF-8 ? !--MyBatis的DTD约束-- !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--mapper核心根标签namespace属性名称空间 --!--名称空间要和接口的路径保持一致-- mapper namespacecom.itheima.mapper.StudentMappersql idselect SELECT * FROM student/sql!--select查询功能的标签id属性唯一标识resultType属性指定结果映射对象类型 返回值类型parameterType属性指定参数映射对象类型--!-- id必须和方法名保持一致--select idselectAll resultTypestudentinclude refidselect//selectselect idselectById resultTypestudent parameterTypeintinclude refidselect/ WHERE id #{id}/selectinsert idinsert parameterTypestudentINSERT INTO student VALUES (#{id},#{name},#{age})/insertupdate idupdate parameterTypestudentUPDATE student SET name #{name},age #{age} WHERE id #{id}/updatedelete iddelete parameterTypeintDELETE FROM student WHERE id #{id}/deleteselect idselectCondition resultTypestudent parameterTypestudentinclude refidselect/whereif testid ! nullid #{id}/ifif testname ! nullAND name #{name}/ifif testage ! nullAND age #{age}/if/where/selectselect idselectByIds resultTypestudent parameterTypelistinclude refidselect/whereforeach collectionlist openid IN ( close) itemid separator,#{id}/foreach/where/select /mapper StudentServiceImpl package com.itheima.service.impl;import com.itheima.bean.Student; import com.itheima.mapper.StudentMapper; import com.itheima.service.StudentService; 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.io.InputStream; import java.util.List; /*业务层实现类*/ public class StudentServiceImpl implements StudentService {Overridepublic ListStudent selectAll() throws IOException {ListStudent students null;SqlSession sqlSession null;InputStream is null;// 没有了持久层实现对象 只有持久层的接口try{// 加载核心配置文件is Resources.getResourceAsStream(MyBatisConfig.xml);// 返回一个字节输入流对象// 获取sqlSession工厂对象SqlSessionFactory build new SqlSessionFactoryBuilder().build(is);// 通过工厂对象获取SqlSession对象sqlSession build.openSession(true);// 代表自动提交事务// 获取StudentMapper接口的实现类对象// 父类的接口指向实现类对象StudentMapper mapper sqlSession.getMapper(StudentMapper.class);// 通过实现类对象调用方法 接受结果students mapper.selectAll();// 释放资源// 返回结果}catch(Exception e){e.printStackTrace();}finally {// 释放资源if(sqlSession ! null){sqlSession.close();}if(is ! null){is.close();}}// 返回结果return students;}Overridepublic Student selectById(Integer id) throws IOException {// 根据id来查询对象Student stu null;SqlSession sqlSession null;InputStream is null;try{// 加载核心配置文件is Resources.getResourceAsStream(MyBatisConfig.xml);//获取工厂对象SqlSessionFactory build new SqlSessionFactoryBuilder().build(is);// 通过工厂对象获取SqlSessionsqlSession build.openSession(true);// 代表自动提交事务// 获取StudentMapper接口的实现类对象StudentMapper mapper sqlSession.getMapper(StudentMapper.class);// 通过实现类对象调用方法 接受结果stu mapper.selectById(id);// 获取学生对象}catch (Exception e){e.printStackTrace();}finally {if(sqlSession ! null){sqlSession.close();}if(is ! null){is.close();}}// 获取学生对象return stu;}// 新增学生对象Overridepublic Integer insert(Student stu) throws IOException {// 根据id来查询对象Integer result null;SqlSession sqlSession null;InputStream is null;try{// 加载核心配置文件is Resources.getResourceAsStream(MyBatisConfig.xml);//获取工厂对象SqlSessionFactory build new SqlSessionFactoryBuilder().build(is);// 通过工厂对象获取SqlSessionsqlSession build.openSession(true);// 代表自动提交事务// 获取StudentMapper接口的实现类对象StudentMapper mapper sqlSession.getMapper(StudentMapper.class);// 通过实现类对象调用方法 接受结果result mapper.insert(stu);// 返回影响的行数}catch (Exception e){e.printStackTrace();}finally {if(sqlSession ! null){sqlSession.close();}if(is ! null){is.close();}}// 获取学生对象return result;}Overridepublic Integer update(Student stu) throws IOException {// 根据id来查询对象Integer result null;SqlSession sqlSession null;InputStream is null;try{// 加载核心配置文件is Resources.getResourceAsStream(MyBatisConfig.xml);//获取工厂对象SqlSessionFactory build new SqlSessionFactoryBuilder().build(is);// 通过工厂对象获取SqlSessionsqlSession build.openSession(true);// 代表自动提交事务// 获取StudentMapper接口的实现类对象StudentMapper mapper sqlSession.getMapper(StudentMapper.class);// 通过实现类对象调用方法 接受结果result mapper.update(stu);// 返回影响的行数}catch (Exception e){e.printStackTrace();}finally {if(sqlSession ! null){sqlSession.close();}if(is ! null){is.close();}}// 获取学生对象return result;}Overridepublic Integer delete(Integer id) throws IOException {// 根据id来查询对象Integer result null;SqlSession sqlSession null;InputStream is null;try{// 加载核心配置文件is Resources.getResourceAsStream(MyBatisConfig.xml);//获取工厂对象SqlSessionFactory build new SqlSessionFactoryBuilder().build(is);// 通过工厂对象获取SqlSessionsqlSession build.openSession(true);// 代表自动提交事务// 获取StudentMapper接口的实现类对象StudentMapper mapper sqlSession.getMapper(StudentMapper.class);// 通过实现类对象调用方法 接受结果result mapper.delete(id);// 返回影响的行数}catch (Exception e){e.printStackTrace();}finally {if(sqlSession ! null){sqlSession.close();}if(is ! null){is.close();}}// 获取学生对象return result;} } 代理对象分析 接口代理方式小结 接口代理方式可以让我们之编写接口即可而实现类对象由MyBatis生成 实现规则 映射配置文件中的名称空间必须和Dao层接口的全类名相同映射配置文件中的增删改查标签的id属性必须和Dao层接口的方法名相同映射配置文件中的增删改查标签的parameterType属性必须和Dao层接口方法的参数相同映射配置文件中的增删改查标签的resultType属性必须和Dao层接口方法的返回值相同 获取动态代理对象 SqlSession功能类中的getMapper()方法
http://www.hkea.cn/news/14548015/

相关文章:

  • 上海专门做培训的网站湖南建筑信息网
  • 如何利用微信进行企业网站推广怎么推广网站
  • 药厂网站建设南宁西乡塘区网站建设
  • 怎样在阿里巴巴做网站wordpress安装插件要求ftp
  • 给别人做网站挣钱服装网站建设环境分析
  • 建立网站需要多少钱多少钱28湖南岚鸿wordpress附件下载
  • 怎么开跨境电商网店seo课程培训机构
  • 酒店房产网站建设做网站商家
  • 东莞市纺织服装学校大型网站怎么做优化
  • 济南企业自助建站网络美工是干啥的
  • python做的网站如何打开杭州下城区建设局网站
  • 视频相亲网站开发成本编程如何自学
  • 汉寿做网站的公司家庭 wordpress
  • 免费网站模板 怎么用考研资料找微信hyhyk1推广可以
  • 网站开发应用价值建设部网站监理注销查询
  • 怎么建立企业网站php做二手商城网站源码
  • 做黑网站微信公众号接口开发
  • 域名和网站编程培训网站
  • 洛阳网站在哪备案手机wordpress上传失败
  • 腾讯云服务器搭建网站wordpress删除主题时执行
  • 淄博周村网站建设哪家好seo优化技术是什么
  • 网站的留言功能苏州新区保洁公司
  • 网站ui标准个人可以建网站
  • h5说 网站如何屏蔽百度广告推广
  • 建设银行网站上预览电子回单wordpress google字体删除
  • 百度网站搜索排名网站网站建设考虑要素
  • 手机微信官方网站首页电子商务推广网站
  • 58同城保定网站建设做网站怎么收费多少
  • 深圳 福田网站建设个人店铺logo
  • 织梦贷款网站源码wordpress获取用户文章