JdbcConfig
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager ds = new DataSourceTransactionManager(dataSource);
return ds;
}
}
MyBatisConfig
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setTypeAliasesPackage("cn.itaxu.domain");
factoryBean.setDataSource(dataSource);
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("cn.itaxu.dao");
return msc;
}
}
ServletConfig
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
// 加载spring配置
@Override
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
// 加载springMVC配置
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
// 设置哪些方法归属springMVC处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
// 处理POST请求中文乱码问题
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8");
return new Filter[]{filter};
}
}
SpringConfig
@Configuration
@ComponentScan("cn.itaxu.service")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement
public class SpringConfig {
}
SpringMvcConfig
@Configuration
@ComponentScan({"cn.itaxu.controller","cn.itaxu.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
SpringMvcSupport
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
}
}
BrandController
@RestController
@RequestMapping("/brands")
public class BrandController {
@Autowired
private BrandService brandService;
@PostMapping
public Result save(@RequestBody Brand brand){
boolean flag = brandService.save(brand);
return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
}
@DeleteMapping("/{id}")
public Result deleteById(@PathVariable Integer id){
boolean flag = brandService.deleteById(id);
return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
}
@DeleteMapping
public Result deleteMultiply(@RequestBody int[] ids){
boolean flag = brandService.deleteMultiply(ids);
return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
}
@PutMapping
public Result update(@RequestBody Brand brand){
System.out.println(brand);
boolean flag = brandService.update(brand);
return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
}
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id){
Brand brand = brandService.getById(id);
Integer code =brand != null ? Code.GET_OK:Code.GET_ERR;
String msg = brand != null ? "success":"fail";
return new Result(code,brand,msg);
}
@GetMapping
public Result getAll(){
List brandList = brandService.getAll();
Integer code = brandList != null ? Code.GET_OK:Code.GET_ERR;
String msg = brandList != null ? "success":"fail";
return new Result(code,brandList,msg);
}
@PostMapping("/{currentPage}/{pageSize}")
public Result selectByPageAndCondition(@PathVariable int currentPage,@PathVariable int pageSize,
@RequestBody Brand brand) {
PageBean pageBean = brandService.selectByPageAndCondition(currentPage, pageSize, brand);
Integer code = pageBean != null ? Code.GET_OK:Code.GET_ERR;
String msg = pageBean != null ? "success":"fail";
return new Result(code,pageBean,msg);
}
}
Code 响应状态码
public class Code {
public static final Integer SAVE_OK = 20011;
public static final Integer DELETE_OK = 20021;
public static final Integer UPDATE_OK = 20031;
public static final Integer GET_OK = 20041;
public static final Integer SAVE_ERR = 20010;
public static final Integer DELETE_ERR = 20020;
public static final Integer UPDATE_ERR = 20030;
public static final Integer GET_ERR = 20040;
public static final Integer SYSTEM_ERR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
public static final Integer SYSTEM_UNKNOW_ERR = 59999;
public static final Integer BUSINESS_ERR = 60002;
}
ProjectExceptionAdvice 项目异常通知
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
// 记录日志
// 发送消息给运维
// 发送消息给开发人员,ex对象发送给开发人员
return new Result(ex.getCode(),"null","系统异常");
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),"null","业务异常");
}
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
// 记录日志
// 发送消息给运维
// 发送消息给开发人员,ex对象发送给开发人员
return new Result(Code.SYSTEM_UNKNOW_ERR,"null","系统繁忙,请稍后再试!");
}
}
Result 统一格式数据
public class Result {
private Object data;
private Integer code;
private String msg;
public Result() {
}
public Result(Integer code,Object data) {
this.data = data;
this.code = code;
}
public Result(Integer code, Object data, String msg) {
this.data = data;
this.code = code;
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
BusinessException 业务异常
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
SystemException 系统异常
public class SystemException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code = code;
}
public SystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
BrandService接口
@Transactional
public interface BrandService {
/**
* 添加数据
* @param brand
* @return
*/
public boolean save(Brand brand);
/**
* 根据id删除
* @param id
* @return
*/
public boolean deleteById(Integer id);
/**
* 根据ids批量删除
* @param ids
* @return
*/
public boolean deleteMultiply(@Param("ids") int[] ids);
/**
* 修改数据
* @param brand
* @return
*/
public boolean update(Brand brand);
/**
* 查询单条
* @param id
* @return
*/
public Brand getById(Integer id);
/**
* 查询所有
* @return
*/
public List getAll();
/**
* 分页查询
* @param currentPage
* @param pageSize
* @return
*/
PageBean selectByPage(int currentPage, int pageSize);
/**
* 分页条件查询
* @param currentPage
* @param pageSize
* @param brand
* @return
*/
PageBean selectByPageAndCondition(int currentPage, int pageSize, Brand brand);
}
BrandServiceImpl
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
private BrandDao brandDao;
@Override
public boolean save(Brand brand) {
return brandDao.save(brand) > 0;
}
@Override
public boolean deleteById(Integer id) {
return brandDao.deleteById(id) > 0;
}
@Override
public boolean deleteMultiply(int[] ids) {
return brandDao.deleteMultiply(ids) > 0;
}
@Override
public boolean update(Brand brand) {
return brandDao.update(brand) > 0;
}
@Override
public Brand getById(Integer id) {
return brandDao.getById(id);
}
@Override
public List getAll() {
return brandDao.getAll();
}
@Override
public PageBean selectByPage(int currentPage, int pageSize) {
int begin = (currentPage-1) * pageSize;
int size = pageSize;
List rows = brandDao.selectByPage(begin, size);
int totalCount = brandDao.selectTotalCount();
PageBean pageBean = new PageBean();
pageBean.setRows(rows);
pageBean.setTotalCount(totalCount);
return pageBean;
}
@Override
public PageBean selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
int begin = (currentPage-1) * pageSize;
int size = pageSize;
// 处理模糊查询
String brandName = brand.getBrandName();
if (brandName!=null&&brandName.length()>0){
brand.setBrandName("%"+brandName+"%");
}
String companyName = brand.getCompanyName();
if (companyName!=null&&companyName.length()>0){
brand.setCompanyName("%"+companyName+"%");
}
// 查询当前页数据
List brandList = brandDao.selectByPageAndCondition(begin, size, brand);
// 查询当前页总记录条数
int totalCount = brandDao.selectTotalCountByCondition(brand);
// 封装PageBean
PageBean pageBean = new PageBean<>(brandList,totalCount);
return pageBean;
}
}
js
希望这些能够帮助到你的学习!!! 如果有需要源码的私信我