搜索
首页Javajava教程MyBatis入门(六)---mybatis与spring的整合

一、整合需要

1.1、方法

上一章中的数据

需要sPRing通过单例方式管理SqlsessionFactory

spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession

(spring和mybatis整合自动完成)

持久层的mapper都需要由spring进行管理

二、创建项目整合环境

2.1、创建项目

46.png

2.2、数据

db.properties

#数据库配置信息
#驱动
driverClass=com.MySQL.jdbc.Driver
#连接url
jdbcUrl=jdbc:mysql://localhost:3306/mybatis?character=utf8#用户名
user=root
#密码
passWord=root
#连接池中保留的最小连接数
minPoolSize=10#连接池中保留的最大连接数。Default: 15 maxPoolSize=20#最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 maxIdletime=1800#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3acquireIncrement=3#连接池中初始化连接数 应在minPoolSize与maxPoolSize之间取值。默认为3
initialPoolSize=15

 

2.3、confinguration

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
   
   
   
   
   
   
   
   
   

   

2.4 POJO类与接口

 

package com.pb.ssm.po;import java.util.Date;/**
*

* @ClassName: Author

* @Description: TODO(作者)

* @author 刘楠

* @date 2015-10-31 下午12:39:33

* */public class Author {    //作者id
   private Integer authorId;    //作者姓名
   private String authorUserName;    //作者密码
   private String authorPassword;    //作者邮箱
   private String authorEmail;    //作者介绍
   private String authroBio;    //注册时间
   private Date registerTime;    
   
   
   public Integer getAuthorId() {        return authorId;
   }    public void setAuthorId(Integer authorId) {        this.authorId = authorId;
   }    public String getAuthorUserName() {        return authorUserName;
   }    public void setAuthorUserName(String authorUserName) {        this.authorUserName = authorUserName;
   }    public String getAuthorPassword() {        return authorPassword;
   }    public void setAuthorPassword(String authorPassword) {        this.authorPassword = authorPassword;
   }    public String getAuthorEmail() {        return authorEmail;
   }    public void setAuthorEmail(String authorEmail) {        this.authorEmail = authorEmail;
   }    public String getAuthroBio() {        return authroBio;
   }    public void setAuthroBio(String authroBio) {        this.authroBio = authroBio;
   }    public Date getRegisterTime() {        return registerTime;
   }    public void setRegisterTime(Date registerTime) {        this.registerTime = registerTime;
   }
   @Override    public String toString() {        return "Author [authorId=" + authorId + ", authorUserName="
               + authorUserName + ", authorPassword=" + authorPassword                + ", authorEmail=" + authorEmail + ", authroBio=" + authroBio                + ", registerTime=" + registerTime + "]";
   }
   
   
}

 

接口

 

package com.pb.ssm.mapper;import com.pb.ssm.po.Author;public interface AuthorMapper {    /**
    *
   
   * @Title: findAuthorById
   
   * @Description: TODO(根据id查找)
   
   * @param @param id
   * @param @return    设定文件
   
   * @return Author    返回类型
   
   * @throws
    */
   public Author findAuthorById(int id);    
   /**
    *
   
   * @Title: addAuthor
   
   * @Description: TODO(添加)
   
   * @param @param author
   * @param @return    设定文件
   
   * @return int    返回类型
   
   * @throws
    */
   public int addAuthor(Author author);    /**
    *
   
   * @Title: updateAuthor
   
   * @Description: TODO(更新)
   
   * @param @param author
   * @param @return    设定文件
   
   * @return int    返回类型
   
   * @throws
    */
   public int updateAuthor(Author author);    
   /**
    * 删除
   
   * @Title: delteAuthor
   
   * @Description: TODO(根据ID删除)
   
   * @param @param id
   * @param @return    设定文件
   
   * @return int    返回类型
   
   * @throws
    */
   public int delteAuthor(int id);
}

 

mapper.xml

 

 

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{authroBio})
update authorauthor_username=#{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime}where author_id=#{authorId}delete from author where author_id=#{authorId}

 

三、使用Mybatis配置文件.xml整合

3.1、写applicationContext.xml



 

3.2、测试

 

package com.pb.ssm.mapper;import java.io.InputStream;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 org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pb.ssm.po.Author;public class AuthorMapperTest {    private ApplicationContext applicationContext;

   @Before    public void setUp() throws Exception {
       applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
   }

   @Test    public void testFindAuthorById() {
       

       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       Author author = authorMapper.findAuthorById(2);
       System.out.println(author);
   
   }

   @Test    public void testAddAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       
       Author author=new Author();
       author.setAuthorUserName("程序猿");
       author.setAuthorPassword("QWERdlfdad");
       author.setAuthorEmail("QWER@QQ.com");        
       
       int  num = authorMapper.addAuthor(author);
   
       System.out.println("num="+num);
       System.out.println("添加后的ID:"+author.getAuthorId());
   }

   @Test    public void testUpdateAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");
       Author author = authorMapper.findAuthorById(13);
       author.setAuthroBio("天天写代码");
       author.setAuthorUserName("码农");        int num=authorMapper.updateAuthor(author);
   
       System.out.println("num="+num);
       System.out.println(author);
   }

   @Test    public void testDeleteAuthor() {        // 获取会话工厂
       AuthorMapper authorMapper = (AuthorMapper) applicationContext.getBean("authorMapper");        int num= authorMapper.delteAuthor(13);
       
   }

}

 

 

 

四、不使用mybatis配置文件

4.1、写ApplicationContext.xml

 



 

更改Mapper.xml,因为不能使用别名,所以type要写POJO类的全路径

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">INSERT INTO author(author_username,author_password,author_email,author_bio)
VALUES(#{authorUserName},#{authorPassword},#{authorEmail},#{authroBio})
update authorauthor_username=#{authorUserName},author_password=#{authorPassword},author_email=#{authorEmail},author_bio=#{authroBio},register_time=#{registerTime}where author_id=#{authorId}delete from author where author_id=#{authorId}

 

测试类同上

 以上就是MyBatis入门(六)---mybatis与spring的整合的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Java之Mybatis的二级缓存怎么使用Java之Mybatis的二级缓存怎么使用May 24, 2023 pm 06:16 PM

缓存的概述和分类概述缓存就是一块内存空间.保存临时数据为什么使用缓存将数据源(数据库或者文件)中的数据读取出来存放到缓存中,再次获取的时候,直接从缓存中获取,可以减少和数据库交互的次数,这样可以提升程序的性能!缓存的适用情况适用于缓存的:经常查询但不经常修改的(eg:省市,类别数据),数据的正确与否对最终结果影响不大的不适用缓存的:经常改变的数据,敏感数据(例如:股市的牌价,银行的汇率,银行卡里面的钱)等等MyBatis缓存类别一级缓存:它是sqlSession对象的缓存,自带的(不需要配置)不

怎么使用springboot+mybatis拦截器实现水平分表怎么使用springboot+mybatis拦截器实现水平分表May 14, 2023 pm 06:43 PM

MyBatis允许使用插件来拦截的方法Executor(update,query,flushStatements,commit,rollback,getTransaction,close,isClosed)ParameterHandler(getParameterObject,setParameters)ResultSetHandler(handleResultSets,handleOutputParameters)StatementHandler(prepare,parameterize,ba

mybatis分页的几种方式mybatis分页的几种方式Jan 04, 2023 pm 04:23 PM

mybatis分页的方式:1、借助数组进行分页,首先查询出全部数据,然后再list中截取需要的部分。2、借助Sql语句进行分页,在sql语句后面添加limit分页语句即可。3、利用拦截器分页,通过拦截器给sql语句末尾加上limit语句来分页查询。4、利用RowBounds实现分页,需要一次获取所有符合条件的数据,然后在内存中对大数据进行操作即可实现分页效果。

怎么用springboot+mybatis plus实现树形结构查询怎么用springboot+mybatis plus实现树形结构查询May 21, 2023 pm 05:01 PM

背景实际开发过程中经常需要查询节点树,根据指定节点获取子节点列表,以下记录了获取节点树的操作,以备不时之需。使用场景可以用于系统部门组织机构、商品分类、城市关系等带有层级关系的数据结构;设计思路递归模型即根节点、枝干节点、叶子节点,数据模型如下:idcodenameparent_code110000电脑0220000手机0310001联想笔记本10000410002惠普笔记本1000051000101联想拯救者1000161000102联想小新系列10001实现代码表结构CREATETABLE`

springboot配置mybatis的sql执行超时时间怎么解决springboot配置mybatis的sql执行超时时间怎么解决May 15, 2023 pm 06:10 PM

当某些sql因为不知名原因堵塞时,为了不影响后台服务运行,想要给sql增加执行时间限制,超时后就抛异常,保证后台线程不会因为sql堵塞而堵塞。一、yml全局配置单数据源可以,多数据源时会失效二、java配置类配置成功抛出超时异常。importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;importorg.apache.

springboot怎么整合mybatis分页拦截器springboot怎么整合mybatis分页拦截器May 13, 2023 pm 04:31 PM

简介今天开发时想将自己写好的代码拿来优化,因为不想在开发服弄,怕搞坏了到时候GIT到生产服一大堆问题,然后把它分离到我轮子(工具)项目上,最后运行后发现我获取List的时候很卡至少10秒,我惊了平时也就我的正常版本是800ms左右(不要看它很久,因为数据量很大,也很正常。),前提是我也知道很慢,就等的确需要优化时,我在放出我优化的plus版本,回到10秒哪里,最开始我刚刚接到这个app项目时,在我用PageHelper.startPage(page,num);(分页),还没等查到的数据封装(Pa

Java Mybatis一级缓存和二级缓存是什么Java Mybatis一级缓存和二级缓存是什么Apr 25, 2023 pm 02:10 PM

一、什么是缓存缓存是内存当中一块存储数据的区域,目的是提高查询效率。MyBatis会将查询结果存储在缓存当中,当下次执行相同的SQL时不访问数据库,而是直接从缓存中获取结果,从而减少服务器的压力。什么是缓存?存在于内存中的一块数据。缓存有什么作用?减少程序和数据库的交互,提高查询效率,降低服务器和数据库的压力。什么样的数据使用缓存?经常查询但不常改变的,改变后对结果影响不大的数据。MyBatis缓存分为哪几类?一级缓存和二级缓存如何判断两次Sql是相同的?查询的Sql语句相同传递的参数值相同对结

PageHelper在springboot+mybatis框架中如何使用PageHelper在springboot+mybatis框架中如何使用May 12, 2023 pm 03:55 PM

一、思路将分页所需的内容都放到一个实体类中分页数据所需要的实体类!内包含页码,页大小,总条数,总页数,起始行pagehelpr提供了这个类pageInfo,不需要我们自己创建二、主要逻辑select*from表名limit起始行,展示几条数据#第n页每页展示五条数据select*from表名limit(n-1)*5,5#每页展示多少条pageSize3#总共有多少条totalselectcount(*)from表名#总页数pagespages=total%pagesSize==0?total/p

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具