前言:今天分享的是Mybatis(ORM持久层框架)
码字不易,点个赞
转载请说明!
开发工具:IDEA
目录
一、Mybatis介绍
①Mybatis的定义
②Mybatis的特点
③Mybatis的优点
二、搭建Mybatis环境
①搭建Mybatis的环境
1.1创建maven工程:
1.2导入相关pom依赖
1.3Mybatis相关插件安装
三、基于ssm逆向工程的使用(IDEA)
1. 安装Mybatis generator插件:
2.配置generatorConfig.xml
四、Mybatis增删改查案例
提交事务以及关闭session的代码:
一、Mybatis介绍 ①Mybatis的定义
Mybatis和Hibernate框架很相似,但是Mybatis一款半自动的ORM持久层框架,具有较高的SQL灵活性,支持高级映射(一对一,一对多),动态SQL,延迟加载和缓存等特性,但它的数据库无关性较低,同样Mybatis是一款优秀的持久化层
②Mybatis的特点③Mybatis的优点1、持久化:持久化就是将程序的数据在持久状态和瞬时状态转化的过程,比如内存:断电即失,在数据库(JDBC)中,io文件持久化
2、持久层Dao层、Service层、Controller层等,完成持久化工作的代码块 层界限十分明显
二、搭建Mybatis环境 ①搭建Mybatis的环境 1.1创建maven工程:1、简化、框架、自动化
2、简单易学
3、灵活
4、sql和代码的分离,提高了可维护性。
5、提供映射标签,支持对象与数据库的orm字段关系映射
6、提供对象关系映射标签,支持对象关系组建维护
7、提供xml标签,支持编写动态sql
上一篇博客已经写明了该怎么去创建Maven项目,这里不过多讲解
上一篇博客链接:
1.2导入相关pom依赖Pom.xml依赖:
1.8 1.8 junit junit4.12 test javax.servlet javax.servlet-api4.0.0 provided org.mybatis mybatis3.4.5 mysql mysql-connector-java5.1.44 org.apache.logging.log4j log4j-core2.9.1 org.apache.logging.log4j log4j-api2.9.1 org.apache.logging.log4j log4j-web2.9.1 src/main/java ** public class SessionUtil { private static SqlSessionFactory sessionFactory; private static ThreadLocal threadLocal = new ThreadLocal (); static { sessionFactory = new SqlSessionFactoryBuilder().build(SessionUtil.class.getResourceAsStream("/mybatis.cfg.xml")); } public static SqlSession openSession() { SqlSession session = threadLocal.get(); if (null == session) { session = sessionFactory.openSession(); threadLocal.set(session); } return session; } public static void main(String[] args) { SqlSession session = openSession(); System.out.println(session.getConnection()); session.close(); // System.out.println(session.getConnection()); } }
4.写好BookService.java以及BookServiceImpl.java类:
BookService.java:
package com.zking.service; import com.zking.model.Book; import com.zking.util.PageBean; import com.zking.vo.BookVo; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; public interface BookService { int deleteByPrimaryKey(Integer bid); int insert(Book record); int insertSelective(Book record); Book selectByPrimaryKey(Integer bid); int updateByPrimaryKeySelective(Book record); int updateByPrimaryKey(Book record); ListselectBooksIn (List bookIds); List selectBooksLike1(@Param("bname") String bname); List selectBooksLike2(@Param("bname") String bname); List selectBooksLike3(@Param("bname") String bname); List list1(); List list2(); Book list3(BookVo bookVo); List
BookServiceImpl.java:
package com.zking.service.impl; import com.zking.mapper.BookMapper; import com.zking.model.Book; import com.zking.service.BookService; import com.zking.util.PageBean; import com.zking.util.SessionUtil; import com.zking.vo.BookVo; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class BookServiceImplTest { private BookMapper bookMapper; public BookMapper getBookMapper() { return bookMapper; } public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int deleteByPrimaryKey(Integer bid) { return bookMapper.deleteByPrimaryKey(bid); } @Override public int insert(Book record) { return bookMapper.insert(record); } @Override public int insertSelective(Book record) { return bookMapper.insertSelective(record); } @Override public Book selectByPrimaryKey(Integer bid) { return bookMapper.selectByPrimaryKey(bid); } @Override public int updateByPrimaryKeySelective(Book record) { return bookMapper.updateByPrimaryKeySelective(record); } @Override public int updateByPrimaryKey(Book record) { return bookMapper.updateByPrimaryKey(record); } }
5、建一个BookServiceImpl测试类:选中BookServiceImpl之后按住Alt+Enter键生成测试类:
点击之后:
BookServiceImpl测试类代码:
package com.zking.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.zking.mapper.BookMapper; import com.zking.model.Book; import com.zking.service.BookService; import com.zking.util.PageBean; import com.zking.vo.BookVo; import java.util.List; import java.util.Map; public class BookServiceImplTest extends TestCase { private BookService bookService; private SqlSession sqlSession; public void setUp() throws Exception { System.out.println("调用方法前执行"); BookServiceImpl bookService=new BookServiceImpl(); sqlSession = SessionUtil.openSession(); BookMapper bookMapper=sqlSession.getMapper(BookMapper.class); bookService.setBookMapper(bookMapper); this.bookService=bookService; } public void tearDown() throws Exception { System.out.println("调用方法后执行"); this.sqlSession.commit(); this.sqlSession.close(); } public void testDeleteByPrimaryKey() { System.out.println(this.bookService.deleteByPrimaryKey(24)); } public void testSelectByPrimaryKey() { System.out.println(this.bookService.selectByPrimaryKey(23)); } }
6、进行测试方法:先进行查询方法的测试:
6.1查询:
已经查询出数据:
6.2删除:
查询和删除的方法测试完毕!(增删改三个方法性质一样,测试一个简单的方法则另外两个也可运行)
提交事务以及关闭session的代码:this.sqlSession.commit(); this.sqlSession.close();
知识就分享就到这里了,希望能够帮助到你
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)