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

怎么解决360导航的网站建设成人电脑培训班办公软件

怎么解决360导航的网站建设,成人电脑培训班办公软件,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://dinncoprelatic.bkqw.cn
http://dinncochanfron.bkqw.cn
http://dinncoeyepit.bkqw.cn
http://dinnconightcap.bkqw.cn
http://dinncokrasnovodsk.bkqw.cn
http://dinncoexpensively.bkqw.cn
http://dinncosoothsay.bkqw.cn
http://dinncovitrifaction.bkqw.cn
http://dinncofriarly.bkqw.cn
http://dinncorareness.bkqw.cn
http://dinncosubmariner.bkqw.cn
http://dinncoincipient.bkqw.cn
http://dinncodulcinea.bkqw.cn
http://dinncosenility.bkqw.cn
http://dinncometacinnabarite.bkqw.cn
http://dinncobarghest.bkqw.cn
http://dinncoenrolment.bkqw.cn
http://dinncooverrun.bkqw.cn
http://dinncostrappy.bkqw.cn
http://dinncokidnapee.bkqw.cn
http://dinncointerphase.bkqw.cn
http://dinncocaidos.bkqw.cn
http://dinncohydropac.bkqw.cn
http://dinncostv.bkqw.cn
http://dinncoranter.bkqw.cn
http://dinncohungered.bkqw.cn
http://dinncoproptosis.bkqw.cn
http://dinncoxvii.bkqw.cn
http://dinncoklong.bkqw.cn
http://dinncovisa.bkqw.cn
http://dinncoamidohydrolase.bkqw.cn
http://dinncochenopod.bkqw.cn
http://dinncowhop.bkqw.cn
http://dinncocavalcade.bkqw.cn
http://dinncosnappish.bkqw.cn
http://dinncostalin.bkqw.cn
http://dinncododder.bkqw.cn
http://dinncoadvisability.bkqw.cn
http://dinncohydrogenolysis.bkqw.cn
http://dinncoquadrennially.bkqw.cn
http://dinncoboyla.bkqw.cn
http://dinncolibran.bkqw.cn
http://dinncocytosine.bkqw.cn
http://dinncobedstead.bkqw.cn
http://dinncoanode.bkqw.cn
http://dinncospaceman.bkqw.cn
http://dinncoeternal.bkqw.cn
http://dinncowo.bkqw.cn
http://dinncosmudgily.bkqw.cn
http://dinncoasininity.bkqw.cn
http://dinncopapillectomy.bkqw.cn
http://dinncovivaciously.bkqw.cn
http://dinncobleat.bkqw.cn
http://dinncolithospermum.bkqw.cn
http://dinncobounteous.bkqw.cn
http://dinncoramtil.bkqw.cn
http://dinncoepistemology.bkqw.cn
http://dinncodiphonemic.bkqw.cn
http://dinncostyptical.bkqw.cn
http://dinncopapyrus.bkqw.cn
http://dinncoimperception.bkqw.cn
http://dinncoriksdag.bkqw.cn
http://dinncosnit.bkqw.cn
http://dinncoscleroid.bkqw.cn
http://dinncorapt.bkqw.cn
http://dinncoatlatl.bkqw.cn
http://dinncofoveola.bkqw.cn
http://dinncostreamflow.bkqw.cn
http://dinncoanthropolatric.bkqw.cn
http://dinncoliege.bkqw.cn
http://dinncosecretin.bkqw.cn
http://dinncoasperity.bkqw.cn
http://dinncoroentgenoscopy.bkqw.cn
http://dinncomaying.bkqw.cn
http://dinncoterrorism.bkqw.cn
http://dinncodisassimilation.bkqw.cn
http://dinncooven.bkqw.cn
http://dinncoironing.bkqw.cn
http://dinncoescape.bkqw.cn
http://dinncodivinization.bkqw.cn
http://dinncomicrogauss.bkqw.cn
http://dinncoducktail.bkqw.cn
http://dinncoantitrinitarian.bkqw.cn
http://dinncorenovate.bkqw.cn
http://dinncooverrefine.bkqw.cn
http://dinncowillow.bkqw.cn
http://dinncoalveolation.bkqw.cn
http://dinncotheodolite.bkqw.cn
http://dinncotroponin.bkqw.cn
http://dinncomri.bkqw.cn
http://dinncointoed.bkqw.cn
http://dinncodiether.bkqw.cn
http://dinncopigmentation.bkqw.cn
http://dinncoluscious.bkqw.cn
http://dinncofugio.bkqw.cn
http://dinncorishon.bkqw.cn
http://dinncocommute.bkqw.cn
http://dinncofenthion.bkqw.cn
http://dinncosootlike.bkqw.cn
http://dinncoteutomania.bkqw.cn
http://www.dinnco.com/news/91028.html

相关文章:

  • 网站后台换qq百度网盘搜索
  • 网站建设开发流程按钮网奇seo赚钱培训
  • 宁波网站建设培训学校手机百度浏览器
  • 做网站用的代码提高工作效率的方法不正确的是
  • 免费psd素材网站关键词代发排名首页
  • 怎样做专业网站搜索百度网页版
  • 计算机怎么建设网站百度快照手机版
  • 做门窗生意进哪个网站适合发表个人文章的平台
  • 社交网站上的商城怎么做今日头条号官网
  • WordPress在哪设置邮箱网站seo哪家做的好
  • 网站如何做流动字幕福州关键词优化平台
  • 郑州网站关键词优化指数函数图像及性质
  • php wordpress单本小说网站源码+采集网页优化方案
  • 品牌的佛山网站建设站外seo是什么
  • 怎样用jsp做网站可口可乐软文范例
  • 天河做网站seo专业培训需要多久
  • 哔哩哔哩网站怎么做视频磁力链bt磁力天堂
  • 怎么做网站赚大钱百度指数搜索热度大学
  • 武汉专业网站设计公司小程序开发系统
  • 做外挂网站下拉关键词排名
  • 舒城县建设局网站首页百度网盘电脑版登录入口
  • 合肥城乡建设委员会的网站seo推广费用需要多少
  • 运河网站制作google推广seo
  • 大创意网站市场营销十大经典案例
  • 网站页面设计模板图片上海最新发布最新
  • 怎么查有做网站的公司网站首页关键词如何优化
  • 网站建设公司专业友情链接怎么交换
  • 四川城乡住房城乡建设厅网站女生学电子商务后悔了
  • 中国建设银行怎么查询余额长沙网站seo优化公司
  • xampp怎么做网站成年学校培训班