spring boot 项目结构

目录

1.application.properties 配置文件

2.pom.xml maven配置文件

3.bean(主要存储业务数据)

4.dao(数据操纵)

5.service (提供服务)

6.controller(前端调用接口提供服务)


主要结构

spring boot 项目结构_第1张图片

1.application.properties 配置文件

#用于连接mysql数据库
#useSSL=false用于消除远程连接数据库时的警告
spring.datasource.url=jdbc:mysql://(ip地址):(端口号)/(数据库名)?useSSL=false
spring.datasource.driver-class-name=org.gjt.mm.mysql.Driver
spring.datasource.username=(数据库登录名)
spring.datasource.password=(登陆密码)

#用于连接sql server数据库
#spring.datasource.url=jdbc:sqlserver://(ip地址):(端口号);DataBaseName=(数据库名)
#spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#spring.datasource.username=(数据库登录名)
#spring.datasource.password=(登陆密码)


#寻找mapper映射文件的路径
mybatis.mapper-locations= classpath:/mapper/**/*.xml

2.pom.xml maven配置文件

    在 pom.xml 中添加所需要的依赖信息,然后在项目根目录执行 mvn install 命令,maven就会自动下载相关依赖jar包到本地仓库

    

3.bean(主要存储业务数据)

   bean必须有无参的构造函数,必要属性要有get、set 方法,spring会利用反射机制来产生bean的实例

package cn.jxustnc.dyManagement.bean;

public class Example {
	private int attr1;
	private String attr2;
	
	public int getAttr1() {
		return attr1;
	}
	public void setAttr1(int attr1) {
		this.attr1 = attr1;
	}
	public String getAttr2() {
		return attr2;
	}
	public void setAttr2(String attr2) {
		this.attr2 = attr2;
	}
	
}

4.dao(数据操纵)

    只需要创建ExampleMapper接口,写好映射ExampleMapper.xml文件,然后在servic层自动注入该接口,mybatis就会根据该接口和xml映射文件创建实现类,进行sql语句查询

package cn.jxustnc.dyManagement.dao;

import java.util.List;

import cn.jxustnc.dyManagement.bean.Example;

public interface ExampleMapper {
	List selectAll();
	Example selectByAttr1(int attr1);
	void save(Example example);
	void update(Example example);
	void deleteByAttr1(int attr1);
}




  
  
  
  
  
  
  	insert into Example values(#{attr1},#{attr2})
  
  
  
  	update Example set attr2=#{attr2} where attr1=#{attr1}
  
  
  
  	delete Example where attr1=#{attr1}
  
  

5.service (提供服务)

    service 层主要调用dao层的功能实现增删改查,在此过程中可以可以进行数据检验,进行一些计算(例如某个人的信息含有出生日期和年龄,这个时候就可以不用在数据库中存储年龄信息,直接根据出生日期计算年龄,这样可以节省一个字段,还可以避免过了某个时间(生日)之后年龄不会自动增加的问题)

spring boot 项目结构_第2张图片

package cn.jxustnc.dyManagement.service;

import java.util.List;

import cn.jxustnc.dyManagement.bean.Example;

public interface IExampleService {
    //服务层的函数都要抛出异常,在controller中捕获错误信息返回给前端
	List findAll() throws Exception;
	
	Example findByAttr1(int attr1) throws Exception;
	
	void save(Example example) throws Exception;
	
	void update(Example example) throws Exception;
	
	void deleteByAttr1(int attr1) throws Exception;
}
package cn.jxustnc.dyManagement.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jxustnc.dyManagement.bean.Example;
import cn.jxustnc.dyManagement.dao.ExampleMapper;
import cn.jxustnc.dyManagement.service.IExampleService;

//添加注解,spring才会自动扫描到这个类
@Service
public class ExampleService implements IExampleService {
    //自动注入ExampleMapper的实现类
	@Autowired
	private ExampleMapper exampleMapper;
	
	@Override
	public List findAll() throws Exception {
		return exampleMapper.selectAll();
	}

	@Override
	public Example findByAttr1(int attr1) throws Exception {
		Example example = exampleMapper.selectByAttr1(attr1);
		if(example==null) {//可以根据需要进行数据检验,抛出异常信息
			throw new Exception("未找到!");
		}else {
			return example;
		}
	}

	@Override
	public void save(Example example) throws Exception {
		exampleMapper.save(example);
	}

	@Override
	public void update(Example example) throws Exception {
		findByAttr1(example.getAttr1());
		exampleMapper.update(example);
	}

	@Override
	public void deleteByAttr1(int attr1) throws Exception {
		findByAttr1(attr1);
		exampleMapper.deleteByAttr1(attr1);
	}

}

6.controller(前端调用接口提供服务)

package cn.jxustnc.dyManagement.util;
//工具类
public class MsgResponse {
	private Integer status;	//状态码	200 成功 500代码异常
	private String message;	//错误、成功信息
	private Object data;	//数据	500 null
	
	public static MsgResponse success(String message, Object data){
		MsgResponse response = new MsgResponse();
		response.setStatus(200);
		response.setMessage(message);
		response.setData(data);
		return response;
	}
	
	public static MsgResponse error(String message){
		MsgResponse response = new MsgResponse();
		response.setStatus(500);
		response.setMessage(message);
		response.setData(null);
		return response;
	}
	
	
	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
}
package cn.jxustnc.dyManagement.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.jxustnc.dyManagement.bean.Example;
import cn.jxustnc.dyManagement.service.IExampleService;
import cn.jxustnc.dyManagement.util.MsgResponse;

@RestController//表示所有数据都以json格式返回
@RequestMapping("/example")//请求该类的url
public class ExampleController {
	//自动注入实现类(很神奇,目前还不知道原理。。。)
	@Autowired
	private IExampleService exampleService;
	
    //spring 项目运行之后可以根据http://ip:port/example/findAll调用这个函数
    //用post方式调用
	@PostMapping("findAll")
	public MsgResponse findAll() {
		List list;
		try {
			list = exampleService.findAll();
			return MsgResponse.success(Integer.toString(list.size()), list);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResponse.error(e.getMessage());
		}
	}
	//用get方式调用
	@GetMapping("findByAttr1")
	public MsgResponse findByAttr1(int attr1) {
		try {
			Example example = exampleService.findByAttr1(attr1);
			return MsgResponse.success("OK", example);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResponse.error(e.getMessage());
		}
	}
	
	@GetMapping("save")
	public MsgResponse save(Example example) {
		try {
			exampleService.save(example);
			return MsgResponse.success("存储成功!", null);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResponse.error(e.getMessage());
		}
	}
	
	@GetMapping("update")
	public MsgResponse update(Example example) {
		try {
			exampleService.update(example);
			return MsgResponse.success("更新成功!", null);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResponse.error(e.getMessage());
		}
		
	}
	
	@DeleteMapping("deleteByAttr1")
	public MsgResponse deleteByAttr1(int attr1) {
		try {
			exampleService.deleteByAttr1(attr1);
			return MsgResponse.success("删除成功!", null);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResponse.error(e.getMessage());
		}
	}
}

(有错误和不足欢迎指出)

你可能感兴趣的:(Java)