Skip to content

Commit

Permalink
feat:利用git上比较好的paheHelper增加mybatis分页;增加前端测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
2010yhh committed Jun 16, 2019
1 parent b9f9468 commit 4546679
Show file tree
Hide file tree
Showing 158 changed files with 24,656 additions and 5 deletions.
8 changes: 6 additions & 2 deletions springboot-static/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
Expand Down Expand Up @@ -102,7 +101,12 @@
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ctg.test.Async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
* @Description: TODO
* @Author: yanhonghai
* @Date: 2018/11/8 18:32
*/
@Service
public class AsyncTaskService {
@Async
void execute(int i){
System.out.print("线程ID"+Thread.currentThread().getId()+"执行序号:"+i+"\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ctg.test.Async;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

/**
* @Description: TODO
* @Author: yanhonghai
* @Date: 2018/11/8 18:35
*/
@Configuration
@EnableAsync
@Component
public class TaskExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor=new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(15);
threadPoolTaskExecutor.setQueueCapacity(10);
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
19 changes: 19 additions & 0 deletions springboot-static/src/main/java/com/ctg/test/Async/TestMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ctg.test.Async;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @Description: TODO
* @Author: yanhonghai
* @Date: 2018/11/8 18:38
*/
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AsyncTaskService.class);
AsyncTaskService asyncTaskService=context.getBean(AsyncTaskService.class);
for(int i=0;i<10;i++){
asyncTaskService.execute(i);
}
context.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.ctg.test.Configure;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
* @Description:
* @Configuration可理解为用spring的时候xml里面的<beans>标签
* @Bean可理解为用spring的时候xml里面的<bean>标签
* @Author: yanhonghai
* @Date: 2018/9/28 11:50
*/
@Configuration
@ImportResource(locations= {"classpath:application-bean.xml"})
public class XmlConfigure {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ctg.test;

import ch.qos.logback.classic.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
Expand All @@ -27,8 +29,10 @@
@MapperScan("com.ctg.test.mapper")//配置mybatis包扫描
@EnableCaching// 启用缓存注解
public class StartApplication {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(StartApplication.class);

public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);

logger.info(".............application start");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.ctg.test.controller;

import com.ctg.test.model.User;
import com.ctg.test.service.UserService;
import com.ctg.test.util.DefaultProperies;
import com.ctg.test.util.DefaultProperies2;
import com.ctg.test.util.DefaultProperies3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*http://localhost:8090/login?userName=yhh&passWord=yhh
*/
@Controller
@EnableAutoConfiguration
public class IndexController {
@Autowired
UserService userService;
@RequestMapping(value = {"/login"})
@ResponseBody
public Object login(@RequestParam String userName, @RequestParam String passWord,
HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> result=new HashMap<>();
List<User>users=userService.findByUserName(userName,passWord);
if(!CollectionUtils.isEmpty(users)){
result.put("code","200");
result.put("msg","success");
result.put("user",users);
}else
{
result.put("code","-1");
result.put("msg","error!");
}
return result;
}
@RequestMapping("/logout")
public String logout() {
return "logout";
}
@RequestMapping("/index2")
String index2(HttpServletRequest request) {
// 逻辑处理
request.setAttribute("key", "index");
return "/index2";
}
@RequestMapping("/")
String index(HttpServletRequest request) {
return "/index";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.ctg.test.controller;

import com.ctg.test.model.User;
import com.ctg.test.model.UserExample;
import com.ctg.test.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
Expand All @@ -17,6 +20,8 @@

/**
*http://localhost:8090/login?userName=yhh&passWord=yhh
* http://localhost:8090/all
* http://localhost:8090/all?pageNum=4&pageSize=3
*/
@Controller
public class TestDbController {
Expand Down Expand Up @@ -140,5 +145,11 @@ public void batchDeleteUser() {
}
userService.batchDeleteUser(ids);
}

@RequestMapping(value = {"/all"},method={RequestMethod.GET})
@ResponseBody
public Object findAll(@RequestParam(defaultValue = "1")int pageNum,
@RequestParam(defaultValue = "5")int pageSize) {
PageInfo users= userService.findAll(pageNum,pageSize);
return users;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ctg.test.controller;

import com.ctg.test.model.User;
import com.ctg.test.service.UserService;
import com.ctg.test.util.DefaultProperies;
import com.ctg.test.util.DefaultProperies2;
import com.ctg.test.util.DefaultProperies3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*http://localhost:8090/test
*/
@Controller
@EnableAutoConfiguration
public class TestPropertyController {
@Autowired
UserService userService;
@Autowired
DefaultProperies defaultProperies;
@Autowired
DefaultProperies2 defaultProperies2;
@Autowired
private Environment env;
@RequestMapping(value = {"/test"})
@ResponseBody
public Object test(
HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> result=new HashMap<>();
result.put("DefaultProperies",defaultProperies);
result.put("DefaultProperies2",defaultProperies2);
DefaultProperies3 defaultProperies3=new DefaultProperies3();
defaultProperies3.setUserName(env.getProperty("default.userName"));
defaultProperies3.setPassWord(env.getProperty("default.passWord"));
result.put("DefaultProperies3",defaultProperies3);
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ctg.test.service;

import com.ctg.test.model.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.util.List;

Expand All @@ -13,7 +15,7 @@ public interface UserService {
public List<User> findByUserName(String userName, String passWord);

public List<User> findByUserId(int userId);

public PageInfo findAll(int pageNum, int pageSize);
public void addUser(User user);

public void updateUser(User user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.ctg.test.model.User;
import com.ctg.test.model.UserExample;
import com.ctg.test.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
Expand Down Expand Up @@ -56,6 +59,27 @@ public List<User> findByUserId(int userId) {
return userMapper.selectByExample(example);
}

/**
* 测试pageHelper分页查询
* @return
*/
@Override
public PageInfo findAll(int pageNum,int pageSize) {
//当第三个参数没有或者为true的时候,进行count查询
//应避免多线程不按照,判断参数或者按照下面的释放线程中的ThreadLocal变量
List<User> users;
try {
PageHelper.startPage(pageNum, pageSize);
users = userMapper.selectByExample(new UserExample());
} finally {
PageHelper.clearPage();
}
//Page<User>page=(Page<User>)users;
PageInfo pageInfo = new PageInfo(users);
return pageInfo;
// return page;
}

@CachePut(key = "#user.userId")
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
Expand Down
Loading

0 comments on commit 4546679

Please sign in to comment.