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

企业加强网站建设的必要性流量精灵app

企业加强网站建设的必要性,流量精灵app,网站建设优化安徽,一家专门做原型的网站【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 List<Student> 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 List<Student> selectCondition(Student stu);//根据多个id查询public abstract List<Student> selectByIds(List<Integer> ids);
}
  • 映射配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--mapper:核心根标签namespace属性:名称空间
--><!--名称空间要和接口的路径保持一致-->
<mapper namespace="com.itheima.mapper.StudentMapper"><sql id="select" >SELECT * FROM student</sql><!--select:查询功能的标签id属性:唯一标识resultType属性:指定结果映射对象类型  返回值类型parameterType属性:指定参数映射对象类型--><!--    id必须和方法名保持一致--><select id="selectAll" resultType="student"><include refid="select"/></select><select id="selectById" resultType="student" parameterType="int"><include refid="select"/> WHERE id = #{id}</select><insert id="insert" parameterType="student">INSERT INTO student VALUES (#{id},#{name},#{age})</insert><update id="update" parameterType="student">UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}</update><delete id="delete" parameterType="int">DELETE FROM student WHERE id = #{id}</delete><select id="selectCondition" resultType="student" parameterType="student"><include refid="select"/><where><if test="id != null">id = #{id}</if><if test="name != null">AND name = #{name}</if><if test="age != null">AND age = #{age}</if></where></select><select id="selectByIds" resultType="student" parameterType="list"><include refid="select"/><where><foreach collection="list" open="id IN (" close=")" item="id" 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 List<Student> selectAll() throws IOException {List<Student> 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.dinnco.com/news/26061.html

相关文章:

  • wordpress链接样式设置方法北京seo关键词优化收费
  • 网站开发用什么系统比较好?今日新闻网
  • 网站建设运行维护合同海淀搜索引擎优化seo
  • 美工做任务网站我是站长网
  • 自己的网站怎么做淘宝联盟下载百度到桌面上
  • 江门企业免费建站近期出现的病毒叫什么
  • html5 电商网站模板免费b站推广软件
  • 网站还未被收录可以做推广吗石家庄网站建设方案推广
  • 建筑工程网络进度计划备注填写范例seo按照搜索引擎的什么对网站
  • 用流媒体做的电台网站谷歌seo快速排名软件首页
  • 手机上可建网站做淘宝客吗国产长尾关键词拘挖掘
  • 关于网站设计的新闻百度站长提交
  • 防城港网络推广谷歌优化方法
  • 做网站是用c 吗企业建站要多少钱
  • 崇明做网站宣传推广网络推广
  • 广州市网站建设制作费用百度域名收录
  • 网站app怎么制作海南网站设计
  • 宝鸡做网站公司推广软文案例
  • 如何自己做网站腾讯系统开发
  • 高新区建网站外包东莞做一个企业网站
  • 怎么把网站上线营销型企业网站建设的内容
  • 简述使用asp建设动态网站查询网入口
  • 挂机宝怎么做网站全网关键词优化公司哪家好
  • wordpress后台点击菜单没反应应新乡百度关键词优化外包
  • 哪个网站做的w7系统好各大网站推广平台
  • 衡阳商城网站建设打字赚钱平台 学生一单一结
  • 魔力百科网站做料理视频seo技术培训岳阳
  • 网站建设设计报告前言百度数据研究中心
  • WordPress显示不出广告如何优化推广中的关键词
  • 如何做二级域名子目录网站百度热搜广告设计公司