Search in sources :

Example 46 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class UserTest method findUserById.

private void findUserById(int id) {
    SqlSession sqlSession = null;
    User user = null;
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        user = mapper.findUserById(id);
    } catch (Exception ex) {
        logger.info("findUserById exception, id:{}, exception:{}", id, ex);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
    if (user == null) {
        logger.info("findUserById fail, id {} not exist", id);
    } else {
        logger.info("findUserById success, User: {}", user);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 47 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class UserTest method deleteUserById.

private void deleteUserById(int id) {
    SqlSession sqlSession = null;
    int result = 0;
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        result = mapper.delete(id);
        sqlSession.commit();
    } catch (Exception ex) {
        logger.info("deleteUserById exception, id:{}, exception:{}", id, ex);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
    if (result <= 0) {
        logger.info("deleteUserById fail, id {} not exist, result:{}", id, result);
    } else {
        logger.info("deleteUserById success, result:{}", result);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 48 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class UserTest method update.

private void update(User user) {
    SqlSession sqlSession = null;
    int result = 0;
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        result = mapper.update(user);
        sqlSession.commit();
    } catch (Exception ex) {
        logger.info("update exception, exception:{}, User:{}", ex, user);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
    if (result <= 0) {
        logger.info("update fail, result:{}", result);
    } else {
        logger.info("update success, result:{}", result);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 49 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class UserTest method insertUser.

private void insertUser(int id, String name, String email, Integer age) {
    SqlSession sqlSession = null;
    User u = new User(name, email, age);
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insert(u);
        sqlSession.commit();
        logger.info("insert success, result:{}, User:{}", result, u);
    } catch (Exception ex) {
        logger.info("insert exception, exception:{}, User:{}", ex, u);
        if (sqlSession != null) {
            sqlSession.rollback();
        }
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 50 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class App method insertUser.

private void insertUser(int id, String name, String email, Integer age) {
    SqlSession sqlSession = null;
    User u = new User(id, name, email, age);
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insert(u);
        sqlSession.commit();
        logger.info("insert success, result:{}, User:{}", result, u);
    } catch (Exception ex) {
        logger.info("insert exception, exception:{}, User:{}", ex, u);
        if (sqlSession != null) {
            sqlSession.rollback();
        }
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Aggregations

SqlSession (org.apache.ibatis.session.SqlSession)2117 Test (org.junit.jupiter.api.Test)1845 BaseDataTest (org.apache.ibatis.BaseDataTest)593 Test (org.junit.Test)127 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)104 Reader (java.io.Reader)97 User (com.github.pagehelper.model.User)87 ArrayList (java.util.ArrayList)84 UserMapper (com.github.pagehelper.mapper.UserMapper)75 Connection (java.sql.Connection)68 FirstName (mbg.test.common.FirstName)60 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)60 HashMap (java.util.HashMap)59 BeforeClass (org.junit.BeforeClass)56 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)54 RowBounds (org.apache.ibatis.session.RowBounds)36 Map (java.util.Map)34 Date (java.util.Date)33 List (java.util.List)24 AwfulTableMapper (mbg.test.mb3.generated.dsql.mapper.AwfulTableMapper)23