com.eldar.daos.mybatis.MyBatisResourceDao.java Source code

Java tutorial

Introduction

Here is the source code for com.eldar.daos.mybatis.MyBatisResourceDao.java

Source

/*******************************************************************************
 * This file is part of Eldar Works.
 *
 * Eldar Works is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Eldar Works is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Eldar Works.  If not, see <http://www.gnu.org/licenses/>.
 * Copyright (c) 2012. Phillip Sanderson
 ******************************************************************************/

package com.eldar.daos.mybatis;

import com.eldar.daos.IResourceDao;
import com.eldar.daos.mybatis.mappers.ResourceMapper;
import com.eldar.models.resources.Resource;
import com.eldar.util.ListPage;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author Phillip Sanderson
 *         Date: 30/05/12
 *         Time: 9:24 PM
 */
@Repository
public class MyBatisResourceDao implements IResourceDao {

    private ResourceMapper resourceMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public MyBatisResourceDao() {

    }

    @Override
    public ListPage<Resource> list(long start, long size) {
        SqlSession session = sqlSessionFactory.openSession();
        ListPage<Resource> page = null;
        try {
            page = new ListPage<Resource>(start, size);
            page.setItems(resourceMapper.list(page));
        } finally {
            session.close();
        }
        return page;
    }

    @Override
    public void save(Resource resource) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            DateTime dateTime = DateTime.now(DateTimeZone.UTC);
            resource.setModifiedDate(dateTime);
            if (resource.getId() == 0) {
                resource.setCreatedDate(dateTime);
                resourceMapper.insert(resource);
            } else {
                resourceMapper.update(resource);
            }
        } finally {
            session.commit();
            session.close();
        }
    }

    @Override
    public Resource get(long id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            return resourceMapper.select(id);
        } finally {
            session.close();
        }
    }

    @Override
    public void delete(Resource resource) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            resourceMapper.delete(resource.getId());
        } finally {
            session.commit();
            session.close();
        }

    }

    @Autowired
    public void setResourceMapper(ResourceMapper resourceMapper) {
        this.resourceMapper = resourceMapper;
    }
}