SSM 框架:Spring+SpringMVC+MyBatis
今天记录下其中的 MyBatis,这是持久化层,说白了就是将原来的 JDBC 封装抽取出来更加灵活简单易用的框架。
MyBatis:yBatis 本是 apache 的一个开源项目 iBatis, 2010 年这个项目由 apache software foundation 迁移到了 google code,并且改名为 MyBatis 。2013 年 11 月迁移到 Github。MyBatis 是一个优秀的持久层框架,它对 jdbc 的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建 connection、创建 statement、手动设置参数、结果集检索等 jdbc 繁杂的过程代码。Mybatis 通过 xml 或注解的方式将要执行的各种 statement(statement、preparedStatemnt、CallableStatement)配置起来,并通过 java 对象和 statement 中的 sql 进行映射生成最终执行的 sql 语句,最后由 mybatis 框架执行 sql 并将结果映射成 java 对象并返回。
下面的例子是用 MyBatis 实现的对 product 表的基本增删改查操作,可以帮助理解使用框架和单独使用 jdbc 有什么不同和优缺点。
beans 包下面的 Product 类:
- package cn.edu360.beans;
- public class Product {
- private long id;
- private String name;
- private float price;@Overridepublic String toString() {
- return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
- }
- public Product() {
- super();
- }
- public Product(int id, String name, float price) {
- super();
- this.id = id;
- this.name = name;
- this.price = price;
- }
- public long getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- }
Product 的 dao 层实现方法:
- package cn.edu360.dao;
- import java.io.IOException;
- import java.util.List;
- 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 cn.edu360.beans.Product;
- /** * DAO : Database Access Object 操作Product数据库的增删改查方法 * * @author Administrator * */
- public class ProductDaoImpl {
- /** * 创建SQLSessionFactory使用的是单例模式,相当于连接池 */
- private static SqlSessionFactory sqlSessionFactory = null;
- static {
- try {
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- /** * 向数据库插入一条Product数据 */
- public void save(Product product) {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- sqlSession.insert("product.save", product);
- sqlSession.commit();
- sqlSession.close();
- }
- /** * 根据id删除相关数据信息 * * @param id */
- public void deleteById(long id) {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- sqlSession.delete("product.deleteById", id);
- sqlSession.commit();
- sqlSession.close();
- }
- /** * 根据传进来的product各项属性更新数据库 * * @param product */
- public void update(Product product) {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- sqlSession.update("product.update", product);
- sqlSession.commit();
- sqlSession.close();
- }
- /** * 查找所有的Product信息,返回为List类型 * * @return */
- public List findAll() {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- List selectList = sqlSession.selectList("product.findProduct");
- sqlSession.close();
- return selectList;
- }
- /** * 根据id值查找数据库返回一个Product对象 * * @param id * @return */
- public Product getById(long id) {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- Product selectOne = sqlSession.selectOne("product.getById", id);
- sqlSession.close();
- return selectOne;
- }
- }
核心配置文件 SqlMapConfig.xml 子配置文件:
- insert into product(name, price) values(# {
- name
- },
- # {
- price
- }) delete from product where id = # {
- id
- }
- update product set name = # {
- name
- },
- price = # {
- price
- }
- where id = # {
- id
- }
JUnit 测试方法:
- package cn.edu360.dao;
- import java.util.List;
- import org.junit.Test;
- import cn.edu360.beans.Product;
- public class ProductDaoImplTest {@Testpublic void testSave() {
- ProductDaoImpl productDaoImpl = new ProductDaoImpl();
- Product product = new Product();
- product.setName("音乐");
- product.setPrice(19);
- productDaoImpl.save(product);
- }@Testpublic void testDeleteById() {
- ProductDaoImpl productDaoImpl = new ProductDaoImpl();
- productDaoImpl.deleteById(1L);
- }@Testpublic void testUpdate() {
- ProductDaoImpl productDaoImpl = new ProductDaoImpl();
- Product product = new Product();
- product.setId(1);
- product.setName("生物");
- product.setPrice(19);
- productDaoImpl.update(product);
- }@Testpublic void testFindAll() {
- ProductDaoImpl productDaoImpl = new ProductDaoImpl();
- List productList = productDaoImpl.findAll();
- for (Product product: productList) {
- System.out.println(product);
- }
- }@Testpublic void testGetById() {
- ProductDaoImpl productDaoImpl = new ProductDaoImpl();
- Product product = productDaoImpl.getById(5L);
- System.out.println(product);
- }
- }
其实基本的增删改查实现很简单,较之前的 JDBC 更加易于优化,将需要配置的信息抽取到 xml 配置文件中,便于后期的优化和维护。
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/02-25/17600409.html