Browse Source

mybatis 分页功能

zhang 2 years ago
parent
commit
7dbc4d08ff

+ 5 - 0
mybatis_train/pom.xml

@@ -57,6 +57,11 @@
57 57
             <artifactId>log4j-slf4j-impl</artifactId>
58 58
             <version>2.11.1</version>
59 59
         </dependency>
60
+        <dependency>
61
+            <groupId>com.github.pagehelper</groupId>
62
+            <artifactId>pagehelper</artifactId>
63
+            <version>5.2.0</version>
64
+        </dependency>
60 65
     </dependencies>
61 66
 
62 67
 </project>

+ 3 - 0
mybatis_train/src/main/resources/mybatisConfig.xml

@@ -14,6 +14,9 @@
14 14
         <typeAlias type="com.pojo.entity.AccountEntity" alias="account"></typeAlias>
15 15
         <typeAlias type="com.pojo.entity.UserEntity" alias="user"></typeAlias>
16 16
     </typeAliases>
17
+    <plugins>
18
+        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
19
+    </plugins>
17 20
     <!--    核心配置信息-->
18 21
     <environments default="xmq_config">
19 22
         <!--        数据库相关配置-->

+ 34 - 0
mybatis_train/src/test/java/TestMybatisPage.java

@@ -0,0 +1,34 @@
1
+import com.github.pagehelper.PageHelper;
2
+import com.mapper.AccountMapper;
3
+import com.mapper.DepartmentMapper;
4
+import com.pojo.entity.AccountRealEntity;
5
+import org.apache.ibatis.io.Resources;
6
+import org.apache.ibatis.session.SqlSession;
7
+import org.apache.ibatis.session.SqlSessionFactory;
8
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
9
+import org.junit.Before;
10
+import org.junit.Test;
11
+
12
+import java.io.IOException;
13
+import java.io.InputStream;
14
+import java.util.List;
15
+
16
+public class TestMybatisPage {
17
+    SqlSession sqlSession;
18
+    AccountMapper accountMapper;
19
+    @Before
20
+    public void before() throws IOException {
21
+        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
22
+        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
23
+        sqlSession = build.openSession(true);
24
+        accountMapper = sqlSession.getMapper(AccountMapper.class);
25
+    }
26
+
27
+    @Test
28
+    public void page1() {
29
+        // 查询分页功能 在查询列表之前先开启分页
30
+        PageHelper.startPage(2,2);
31
+        List<AccountRealEntity> byDepId = accountMapper.findByDepId(1);
32
+        byDepId.forEach(System.out::println);
33
+    }
34
+}

+ 5 - 1
spring_aop/src/main/java/com/TestTx.java

@@ -17,6 +17,10 @@ public class TestTx {
17 17
     AccountTxService accountTxService;
18 18
     @Test
19 19
     public void transformAnno() {
20
-        accountTxService.transformTx();
20
+        try {
21
+            accountTxService.transformTx();
22
+        }catch (Exception e) {
23
+            System.out.println(e.getMessage());
24
+        }
21 25
     }
22 26
 }

+ 6 - 3
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java

@@ -20,12 +20,15 @@ public class AccountServiceImpl implements AccountService {
20 20
         accountEntity.setBalance(300.0);
21 21
         int i = accountDao.saveAccount(accountEntity);
22 22
     }
23
-//    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24
-    @Transactional
23
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24
+//    @Transactional
25 25
     @Override
26 26
     public int reduceAccount(AccountEntity toEntity) {
27 27
         int in = accountDao.incomeAccount(toEntity, "to");
28
-        System.out.println(1/0);
28
+/*        try {
29
+        }catch (Exception e) {
30
+            System.out.println(e.getMessage());
31
+        }*/
29 32
         return in;
30 33
     }
31 34
 }

+ 1 - 0
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTwoServiceImpl.java

@@ -15,5 +15,6 @@ public class AccountTwoServiceImpl implements AccountTwoService {
15 15
     @Transactional(rollbackFor = Exception.class)
16 16
     public void addAccountBalance(AccountEntity addEntity) {
17 17
         int in = accountDao.incomeAccount(addEntity, "in");
18
+        throw new RuntimeException("runtimeERROR");
18 19
     }
19 20
 }

+ 2 - 6
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTxServiceImpl.java

@@ -16,8 +16,8 @@ public class AccountTxServiceImpl implements AccountTxService {
16 16
     @Autowired
17 17
     AccountTwoService accountTwoService;
18 18
 
19
-//    @Transactional(propagation = Propagation.REQUIRED)
20
-    @Transactional
19
+    @Transactional(propagation = Propagation.REQUIRES_NEW)
20
+//    @Transactional
21 21
     @Override
22 22
     public void transformTx() {
23 23
         // 转账操作
@@ -25,10 +25,6 @@ public class AccountTxServiceImpl implements AccountTxService {
25 25
         AccountEntity addEntity = new AccountEntity();
26 26
         reduceEntity.setId(16).setBalance(3.0);
27 27
         addEntity.setId(17).setBalance(3.0);
28
-//        try {
29
-//        }catch (Exception e) {
30
-//            System.out.println(e.getMessage());
31
-//        }
32 28
         accountService.reduceAccount(reduceEntity);
33 29
         accountTwoService.addAccountBalance(addEntity);
34 30
     }