在这里使用的ssm框架完成对于mysql数据库数据查找
这里主要对于java包中Controller(前台实现)dao(基础层,连接数据库对数据库进行增删改查)po(数据类) Service(包括接口及接口的实现)mapper(数据库查询语句映射器)
按个人个人习惯来写从前往后
1.
以Department为例
首先在po中写SysDepartment.class其中包括所有需要的对象
package com.dc.ssm.po;
import com.wordnik.swagger.annotations.ApiModelProperty;
import javax.xml.crypto.Data;
public class SysDepartment {
@ApiModelProperty(value = " 用户id")
private Integer id;
@ApiModelProperty(value = " 姓名")
private String name;
@ApiModelProperty(value = " 部门")
private String department;
@ApiModelProperty(value = " 电话")
private String phone;
@ApiModelProperty(value = " 入职时间")
private Data addTime;
@ApiModelProperty(value = " 是否删除 0为不删除 1为删除")
private int is_deleted;
@ApiModelProperty(value = " 状态 0为不禁用 1为禁用")
private int state;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Data getAddTime() {
return addTime;
}
public void setAddTime(Data addTime) {
this.addTime = addTime;
}
public int getIs_deleted() {
return is_deleted;
}
public void setIs_deleted(int is_deleted) {
this.is_deleted = is_deleted;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("SysDepartment{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", department='").append(department).append('\'');
sb.append(", phone='").append(phone).append('\'');
sb.append(", addTime=").append(addTime);
sb.append(", is_deleted=").append(is_deleted);
sb.append(", state=").append(state);
sb.append('}');
return sb.toString();
}
}
其中 @ApiModelProperty(value = " 部门")这是对于每一个对象的注释
2.
然后是dao中的SysDepartmentDao接口
package com.dc.ssm.dao;
import com.dc.ssm.po.SysDepartment;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface SysDepartmentDao {
//查询所有部门信息
public List<SysDepartment> selectDepartmentAll();
//根据名称查询部门信息
public List<SysDepartment> selectInfoByDepartment(@Param(value = "department") String department);
}
在SysDepartmentDao中我们完成了对于需要完成的连个方法的定义
3.
再然后是在service中的DepartmentService,DepartmentServiceImpl
service完成了对于dao的调用而dao则完成了将内存数据转换为永久数据
DepartmentService
package com.dc.ssm.service;
import com.dc.ssm.po.SysDepartment;
import java.util.List;
public interface DepartmentService {
//查询所有部门信息
public List<SysDepartment> selectDepartmentAll();
//根据名称查询部门信息
public List<SysDepartment> selectInfoByDepartment(String department);
}
DepartmentServiceImpl
package com.dc.ssm.service.impl;
import com.dc.ssm.dao.SysDepartmentDao;
import com.dc.ssm.po.SysDepartment;
import com.dc.ssm.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
SysDepartmentDao sysDepartmentDao;
@Override
public List<SysDepartment> selectDepartmentAll() {
return sysDepartmentDao.selectDepartmentAll();
}
@Override
public List<SysDepartment> selectInfoByDepartment(String department) {
return sysDepartmentDao.selectInfoByDepartment(department);
}
}
其中对于
@Service的注释必须添加不然DepartmentServiceImpl将不会被调用
其中
@Autowired
SysDepartmentDao sysDepartmentDao;
则精简了程序 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
4.
再然后是
<?xml version="1.0" encoding="UTF-8" ?>
<!--持久化数据的接收与转化-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dc.ssm.dao.SysDepartmentDao">
<!--id为接口名-->
<select id="selectDepartmentAll" resultType="com.dc.ssm.po.SysDepartment">
select *from t_sys_department where is_deleted=0 and state=1
</select>
<select id="selectInfoByDepartment" parameterType="string" resultType="com.dc.ssm.po.SysDepartment">
select *from t_sys_department where is_deleted=0 and state=1 and department=#{department}
</select>
</mapper>
其中parameterType这是输入的数据类型
5.
最后这是对于控制器Controller中的书写
package com.dc.ssm.controller;
import com.dc.base.pojo.BaseModel;
import com.dc.ssm.po.SysDepartment;
import com.dc.ssm.service.DepartmentService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.ws.rs.HttpMethod;
import java.util.List;
@Controller
public class DepartmentController {
@Autowired
DepartmentService departmentService;
@RequestMapping("/selectDepartmentAll")
@ResponseBody
public BaseModel selectDepartmentAll(BaseModel baseModel) {
List<SysDepartment> departments = departmentService.selectDepartmentAll();
baseModel.setResultCode(0);
baseModel.setMessage("查询所有用户信息成功");
baseModel.setData(departments);
return baseModel;
};
@ApiOperation(value = "根据用户部门查询用户列表", notes = "查询", httpMethod = HttpMethod.GET)
@RequestMapping("/selectInfoByDepartment")
@ResponseBody
public BaseModel selectNameAll(BaseModel baseModel, @RequestParam(value = "department") String department) {
List<SysDepartment> departments = departmentService.selectInfoByDepartment(department);
baseModel.setResultCode(0);
baseModel.setMessage("查询所有用户信息成功");
baseModel.setData(departments);
return baseModel;
};
}
其中@Controller注释必须填写否者DepartmentController将不会被使用
添加@ApiOperation(value = “根据用户部门查询用户列表”, notes = “查询”, httpMethod = HttpMethod.GET)
则可以http://localhost:8991/swagger/index.html测试api
@RequestMapping("/selectInfoByDepartment")
说明了查询地址
@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文(ResponseBody)中,一般在异步获取数据时使用,通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中。
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
作者:java_xxxx
来源:CSDN
原文:https://blog.csdn.net/java_xxxx/article/details/80135671
版权声明:本文为博主原创文章,转载请附上博文链接!