Search in sources :

Example 81 with SqlSession

use of org.apache.ibatis.session.SqlSession in project generator by mybatis.

the class ConditionalJava5Test method testAwfulTableUpdateByPrimaryKey.

@Test
public void testAwfulTableUpdateByPrimaryKey() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        AwfulTableMapper mapper = sqlSession.getMapper(AwfulTableMapper.class);
        AwfulTable record = new AwfulTable();
        record.seteMail("fred@fred.com");
        record.setEmailaddress("alsofred@fred.com");
        record.setFirstFirstName("fred1");
        record.setFrom("from field");
        record.setId1(1);
        record.setId2(2);
        record.setId5(5);
        record.setId6(6);
        record.setId7(7);
        record.setSecondFirstName("fred2");
        record.setThirdFirstName("fred3");
        int rows = mapper.insert(record);
        assertEquals(1, rows);
        Integer generatedCustomerId = record.getCustomerId();
        assertEquals(57, generatedCustomerId.intValue());
        record.setId1(11);
        record.setId2(22);
        rows = mapper.updateByPrimaryKey(record);
        assertEquals(1, rows);
        AwfulTable returnedRecord = mapper.selectByPrimaryKey(generatedCustomerId);
        assertEquals(generatedCustomerId, returnedRecord.getCustomerId());
        assertEquals(record.geteMail(), returnedRecord.geteMail());
        assertEquals(record.getEmailaddress(), returnedRecord.getEmailaddress());
        assertEquals(record.getFirstFirstName(), returnedRecord.getFirstFirstName());
        assertEquals(record.getFrom(), returnedRecord.getFrom());
        assertEquals(record.getId1(), returnedRecord.getId1());
        assertEquals(record.getId2(), returnedRecord.getId2());
        assertEquals(record.getId5(), returnedRecord.getId5());
        assertEquals(record.getId6(), returnedRecord.getId6());
        assertEquals(record.getId7(), returnedRecord.getId7());
        assertEquals(record.getSecondFirstName(), returnedRecord.getSecondFirstName());
        assertEquals(record.getThirdFirstName(), returnedRecord.getThirdFirstName());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) AwfulTableMapper(mbg.test.mb3.generated.annotated.conditional.mapper.AwfulTableMapper) AwfulTable(mbg.test.mb3.generated.annotated.conditional.model.AwfulTable) Test(org.junit.jupiter.api.Test)

Example 82 with SqlSession

use of org.apache.ibatis.session.SqlSession in project generator by mybatis.

the class DynamicSqlTest method testFieldsOnlycount.

@Test
public void testFieldsOnlycount() {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
        FieldsonlyMapper mapper = sqlSession.getMapper(FieldsonlyMapper.class);
        Fieldsonly record = new Fieldsonly();
        record.setDoublefield(11.22);
        record.setFloatfield(33.44);
        record.setIntegerfield(5);
        mapper.insert(record);
        record = new Fieldsonly();
        record.setDoublefield(44.55);
        record.setFloatfield(66.77);
        record.setIntegerfield(8);
        mapper.insert(record);
        record = new Fieldsonly();
        record.setDoublefield(88.99);
        record.setFloatfield(100.111);
        record.setIntegerfield(9);
        mapper.insert(record);
        long rows = mapper.count(dsl -> dsl.where(fieldsonly.integerfield, isGreaterThan(5)));
        assertThat(rows).isEqualTo(2);
        rows = mapper.count(CountDSLCompleter.allRows());
        assertThat(rows).isEqualTo(3);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) FieldsonlyMapper(mbg.test.mb3.generated.dsql.mapper.FieldsonlyMapper) Fieldsonly(mbg.test.mb3.generated.dsql.model.Fieldsonly) Test(org.junit.jupiter.api.Test)

Example 83 with SqlSession

use of org.apache.ibatis.session.SqlSession in project generator by mybatis.

the class DynamicSqlTest method testPKFieldsSelectByExampleComplexLike.

@Test
public void testPKFieldsSelectByExampleComplexLike() {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
        PkfieldsMapper mapper = sqlSession.getMapper(PkfieldsMapper.class);
        Pkfields record = new Pkfields();
        record.setFirstname("Fred");
        record.setLastname("Flintstone");
        record.setId1(1);
        record.setId2(1);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Wilma");
        record.setLastname("Flintstone");
        record.setId1(1);
        record.setId2(2);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Pebbles");
        record.setLastname("Flintstone");
        record.setId1(1);
        record.setId2(3);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Barney");
        record.setLastname("Rubble");
        record.setId1(2);
        record.setId2(1);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Betty");
        record.setLastname("Rubble");
        record.setId1(2);
        record.setId2(2);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Bamm Bamm");
        record.setLastname("Rubble");
        record.setId1(2);
        record.setId2(3);
        mapper.insert(record);
        List<Pkfields> answer = mapper.select(dsl -> dsl.where(pkfieldstable.firstname, isLike("B%"), and(pkfieldstable.id2, isEqualTo(3))).or(pkfieldstable.firstname, isLike("Wi%")).orderBy(pkfieldstable.id1, pkfieldstable.id2));
        assertEquals(2, answer.size());
        Pkfields returnedRecord = answer.get(0);
        assertEquals(1, returnedRecord.getId1().intValue());
        assertEquals(2, returnedRecord.getId2().intValue());
        returnedRecord = answer.get(1);
        assertEquals(2, returnedRecord.getId1().intValue());
        assertEquals(3, returnedRecord.getId2().intValue());
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) PkfieldsMapper(mbg.test.mb3.generated.dsql.mapper.PkfieldsMapper) Pkfields(mbg.test.mb3.generated.dsql.model.Pkfields) Test(org.junit.jupiter.api.Test)

Example 84 with SqlSession

use of org.apache.ibatis.session.SqlSession in project generator by mybatis.

the class DynamicSqlTest method testAwfulTableUpdateByPrimaryKey.

@Test
public void testAwfulTableUpdateByPrimaryKey() {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
        AwfulTableMapper mapper = sqlSession.getMapper(AwfulTableMapper.class);
        AwfulTable record = new AwfulTable();
        record.seteMail("fred@fred.com");
        record.setEmailaddress("alsofred@fred.com");
        record.setFirstFirstName("fred1");
        record.setFrom("from field");
        record.setId1(1);
        record.setId2(2);
        record.setId5(5);
        record.setId6(6);
        record.setId7(7);
        record.setSecondFirstName("fred2");
        record.setThirdFirstName("fred3");
        mapper.insert(record);
        Integer generatedCustomerId = record.getCustomerId();
        record.setId1(11);
        record.setId2(22);
        int rows = mapper.updateByPrimaryKey(record);
        assertEquals(1, rows);
        Optional<AwfulTable> returnedRecord = mapper.selectByPrimaryKey(generatedCustomerId);
        assertThat(returnedRecord).hasValueSatisfying(rr -> {
            assertEquals(generatedCustomerId, rr.getCustomerId());
            assertEquals(record.geteMail(), rr.geteMail());
            assertEquals(record.getEmailaddress(), rr.getEmailaddress());
            assertEquals(record.getFirstFirstName(), rr.getFirstFirstName());
            assertEquals(record.getFrom(), rr.getFrom());
            assertEquals(record.getId1(), rr.getId1());
            assertEquals(record.getId2(), rr.getId2());
            assertEquals(record.getId5(), rr.getId5());
            assertEquals(record.getId6(), rr.getId6());
            assertEquals(record.getId7(), rr.getId7());
            assertEquals(record.getSecondFirstName(), rr.getSecondFirstName());
            assertEquals(record.getThirdFirstName(), rr.getThirdFirstName());
        });
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) AwfulTableMapper(mbg.test.mb3.generated.dsql.mapper.AwfulTableMapper) AwfulTable(mbg.test.mb3.generated.dsql.model.AwfulTable) Test(org.junit.jupiter.api.Test)

Example 85 with SqlSession

use of org.apache.ibatis.session.SqlSession in project generator by mybatis.

the class DynamicSqlTest method testAwfulTableSelectByExampleLikeWithMultiInsert.

@Test
public void testAwfulTableSelectByExampleLikeWithMultiInsert() {
    try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
        AwfulTableMapper mapper = sqlSession.getMapper(AwfulTableMapper.class);
        List<AwfulTable> records = new ArrayList<>();
        AwfulTable record = new AwfulTable();
        record.seteMail("fred@fred.com");
        record.setEmailaddress("alsofred@fred.com");
        record.setFirstFirstName("fred1");
        record.setFrom("from field");
        record.setId1(1);
        record.setId2(2);
        record.setId5(5);
        record.setId6(6);
        record.setId7(7);
        record.setSecondFirstName("fred2");
        record.setThirdFirstName("fred3");
        records.add(record);
        record = new AwfulTable();
        record.seteMail("wilma@wilma.com");
        record.setEmailaddress("alsoWilma@wilma.com");
        record.setFirstFirstName("wilma1");
        record.setFrom("from field");
        record.setId1(11);
        record.setId2(22);
        record.setId5(55);
        record.setId6(66);
        record.setId7(77);
        record.setSecondFirstName("wilma2");
        record.setThirdFirstName("wilma3");
        records.add(record);
        record = new AwfulTable();
        record.seteMail("pebbles@pebbles.com");
        record.setEmailaddress("alsoPebbles@pebbles.com");
        record.setFirstFirstName("pebbles1");
        record.setFrom("from field");
        record.setId1(111);
        record.setId2(222);
        record.setId5(555);
        record.setId6(666);
        record.setId7(777);
        record.setSecondFirstName("pebbles2");
        record.setThirdFirstName("pebbles3");
        records.add(record);
        record = new AwfulTable();
        record.seteMail("barney@barney.com");
        record.setEmailaddress("alsoBarney@barney.com");
        record.setFirstFirstName("barney1");
        record.setFrom("from field");
        record.setId1(1111);
        record.setId2(2222);
        record.setId5(5555);
        record.setId6(6666);
        record.setId7(7777);
        record.setSecondFirstName("barney2");
        record.setThirdFirstName("barney3");
        records.add(record);
        record = new AwfulTable();
        record.seteMail("betty@betty.com");
        record.setEmailaddress("alsoBetty@betty.com");
        record.setFirstFirstName("betty1");
        record.setFrom("from field");
        record.setId1(11111);
        record.setId2(22222);
        record.setId5(55555);
        record.setId6(66666);
        record.setId7(77777);
        record.setSecondFirstName("betty2");
        record.setThirdFirstName("betty3");
        records.add(record);
        record = new AwfulTable();
        record.seteMail("bammbamm@bammbamm.com");
        record.setEmailaddress("alsoBammbamm@bammbamm.com");
        record.setFirstFirstName("bammbamm1");
        record.setFrom("from field");
        record.setId1(111111);
        record.setId2(222222);
        record.setId5(555555);
        record.setId6(666666);
        record.setId7(777777);
        record.setSecondFirstName("bammbamm2");
        record.setThirdFirstName("bammbamm3");
        records.add(record);
        int rowsInserted = mapper.insertMultiple(records);
        assertEquals(6, rowsInserted);
        // check generated keys
        assertEquals(57, records.get(0).getCustomerId());
        assertEquals(58, records.get(1).getCustomerId());
        assertEquals(59, records.get(2).getCustomerId());
        assertEquals(60, records.get(3).getCustomerId());
        assertEquals(61, records.get(4).getCustomerId());
        assertEquals(62, records.get(5).getCustomerId());
        List<AwfulTable> answer = mapper.select(dsl -> dsl.where(awfulTable.firstFirstName, isLike("b%")).orderBy(awfulTable.customerId));
        assertEquals(3, answer.size());
        AwfulTable returnedRecord = answer.get(0);
        assertEquals(1111, returnedRecord.getId1().intValue());
        assertEquals(2222, returnedRecord.getId2().intValue());
        assertEquals(60, returnedRecord.getCustomerId());
        returnedRecord = answer.get(1);
        assertEquals(11111, returnedRecord.getId1().intValue());
        assertEquals(22222, returnedRecord.getId2().intValue());
        assertEquals(61, returnedRecord.getCustomerId());
        returnedRecord = answer.get(2);
        assertEquals(111111, returnedRecord.getId1().intValue());
        assertEquals(222222, returnedRecord.getId2().intValue());
        assertEquals(62, returnedRecord.getCustomerId());
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) AwfulTableMapper(mbg.test.mb3.generated.dsql.mapper.AwfulTableMapper) ArrayList(java.util.ArrayList) AwfulTable(mbg.test.mb3.generated.dsql.model.AwfulTable) Test(org.junit.jupiter.api.Test)

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