Spring Boot
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.xwolf;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.xwolf.boot.dao")
|
||||
public class BootApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BootApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.xwolf.boot.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author xwolf
|
||||
* @date 2017-02-25 08:14
|
||||
* @since V1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
public class DataSourceConfig {
|
||||
|
||||
@ConfigurationProperties(prefix = "datasource.druid")
|
||||
@Bean(name="datasource",initMethod = "init",destroyMethod = "close")
|
||||
public DruidDataSource getDataSource(){
|
||||
return new DruidDataSource();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(){
|
||||
return new DataSourceTransactionManager(getDataSource());
|
||||
}
|
||||
|
||||
// 指定环境下dev 开启监控
|
||||
@Bean
|
||||
@Profile("dev")
|
||||
public FilterRegistrationBean druidFilter() {
|
||||
FilterRegistrationBean registration = new FilterRegistrationBean();
|
||||
registration.setFilter(new WebStatFilter());
|
||||
Map<String, String> intParams = new HashMap<>();
|
||||
intParams.put("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
|
||||
registration.setName("DruidWebStatFilter");
|
||||
registration.setUrlPatterns(Lists.newArrayList("/*"));
|
||||
registration.setInitParameters(intParams);
|
||||
return registration;
|
||||
}
|
||||
|
||||
//数据源监控
|
||||
@Bean
|
||||
@Profile("dev")
|
||||
public ServletRegistrationBean servletRegistrationBean() {
|
||||
ServletRegistrationBean registration = new ServletRegistrationBean();
|
||||
registration.setServlet(new StatViewServlet());
|
||||
registration.setName("druid");
|
||||
registration.setUrlMappings(Lists.newArrayList("/druid/*"));
|
||||
//自定义添加初始化参数
|
||||
Map<String, String> intParams = new HashMap<>();
|
||||
intParams.put("loginUsername","druid");
|
||||
intParams.put("loginPassword","druid");
|
||||
registration.setName("DruidWebStatFilter");
|
||||
registration.setInitParameters(intParams);
|
||||
return registration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.xwolf.boot.controller;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.xwolf.boot.entity.User;
|
||||
import com.xwolf.boot.service.IUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author xwolf
|
||||
* @date 2017-02-25 09:20
|
||||
* @since V1.0.0
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("user")
|
||||
@Slf4j
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@RequestMapping("add")
|
||||
@ResponseBody
|
||||
public String insert(@RequestParam(value = "name",defaultValue = "")String name){
|
||||
User user = new User();
|
||||
user.setUname(name);
|
||||
user.setBirth(new Date());
|
||||
log.info("请求参数:{}",user);
|
||||
return userService.insert(user);
|
||||
}
|
||||
|
||||
@RequestMapping("list/{start}/{size}")
|
||||
public PageInfo<User> getUserList(@PathVariable("start")int start,@PathVariable("size")int size){
|
||||
PageHelper.startPage(start,size);
|
||||
List<User> list= userService.getList();
|
||||
return new PageInfo<>(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.xwolf.boot.dao;
|
||||
|
||||
import com.xwolf.boot.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author xwolf
|
||||
* @date 2017-02-25 09:07
|
||||
* @since V1.0.0
|
||||
*/
|
||||
public interface IUserDao {
|
||||
|
||||
int insert(User user);
|
||||
|
||||
List<User> queryList();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.xwolf.boot.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author xwolf
|
||||
* @date 2017-02-25 09:00
|
||||
* @since V1.0.0
|
||||
*/
|
||||
@Data
|
||||
@Slf4j
|
||||
public class User {
|
||||
|
||||
private int uid;
|
||||
private String uname;
|
||||
private Date birth;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.xwolf.boot.service;
|
||||
|
||||
import com.xwolf.boot.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author xwolf
|
||||
* @date 2017-02-25 09:16
|
||||
* @since V1.0.0
|
||||
*/
|
||||
public interface IUserService {
|
||||
|
||||
String insert(User user);
|
||||
|
||||
List<User> getList();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.xwolf.boot.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xwolf.boot.dao.IUserDao;
|
||||
import com.xwolf.boot.entity.User;
|
||||
import com.xwolf.boot.service.IUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author xwolf
|
||||
* @date 2017-02-25 09:17
|
||||
* @since V1.0.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserServiceImpl implements IUserService {
|
||||
@Autowired
|
||||
private IUserDao userDao;
|
||||
|
||||
@Override
|
||||
public String insert(User user) {
|
||||
int resultInt=userDao.insert(user);
|
||||
JSONObject result = new JSONObject();
|
||||
if(resultInt>0){
|
||||
result.put("success",true);
|
||||
result.put("msg","成功");
|
||||
}else{
|
||||
result.put("success",false);
|
||||
result.put("msg","失败");
|
||||
}
|
||||
return result.toJSONString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> getList() {
|
||||
return userDao.queryList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user