后台开发学习
系统结构
一、项目目录结构
二、各部分内容,以demo为例
1.domain
代码如下(示例):
package com.sddx.system.domain;
public class Demo {
private int id;
private String name;
private int age;
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
mapper
代码如下(示例):
package com.sddx.system.mapper;
import com.sddx.system.domain.Demo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@Mapper
public interface DemoMapper {
public List<Demo> getAllInfo(String tableName);
public List<Demo> selectDemo(Demo demo);
public List<Demo> selectName(String name);
public int deleteStudent(Integer id);
public int insertStudent(Demo demo);
public int updateStudent(Demo demo);
}
service
代码如下(示例):
package com.sddx.system.service;
import com.sddx.system.domain.Demo;
import java.util.List;
public interface DemoService {
public List<Demo> getAllInfo(String tableName);
public List<Demo> selectDemo(Demo demo);
public List<Demo> selectName(String name);
public int deleteStudent(Integer id);
public int insertStudent(Demo demo);
public int updateStudent(Demo demo);
}
impl接口
代码如下(示例):
package com.sddx.system.service.impl;
import com.sddx.system.domain.Demo;
import com.sddx.system.mapper.DemoMapper;
import com.sddx.system.service.DemoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
DemoMapper demoMapper;
@Override
public List<Demo> getAllInfo(String tableName){ return demoMapper.getAllInfo(tableName); }
@Override
public List<Demo> selectDemo(Demo demo){ return demoMapper.selectDemo(demo); }
@Override
public List<Demo> selectName(String name){ return demoMapper.selectName(name);}
@Override
public int deleteStudent(Integer id) {
return demoMapper.deleteStudent(id);
}
@Override
public int insertStudent(Demo demo) {
return demoMapper.insertStudent(demo);
}
@Override
public int updateStudent(Demo demo) {
return demoMapper.updateStudent(demo);
}
}
controller
代码如下(示例):
package com.sddx.web.controller.system;
import com.sddx.common.annotation.Log;
import com.sddx.common.core.controller.BaseController;
import com.sddx.common.core.domain.AjaxResult;
import com.sddx.common.enums.BusinessType;
import com.sddx.system.domain.Demo;
import com.sddx.system.service.DemoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/demo")
public class DemoController extends BaseController {
@Autowired
DemoService demoService;
@PostMapping("/{tableName}")
public List<Demo> getAllInfo(@PathVariable("tableName") String tableName){
return demoService.getAllInfo(tableName);
}
//查询全部
@GetMapping("/list")
public List<Demo> selectDemo(Demo demo) {
return demoService.selectDemo(demo);
}
// 查询 按名字
@GetMapping(value = "/{name}")
public List<Demo> selectName(@PathVariable("name") String name) {
return demoService.selectName(name);
}
//删除
@Log(title = "Demo", businessType = BusinessType.DELETE)
@DeleteMapping("/{id}")
public AjaxResult deleteStudent(@PathVariable("id") Integer id)
{
return toAjax(demoService.deleteStudent(id));
}
//添加
@Log(title = "Demo", businessType = BusinessType.INSERT)
@PostMapping("/list")
public void insertStudent(@RequestBody Demo demo)
{
demoService.insertStudent(demo);
}
//修改
@PutMapping("/list")
public void updateStudent(@RequestBody Demo demo) {
demoService.updateStudent(demo);
}
}
xml(sql语句)
代码如下(示例):
<?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.sddx.system.mapper.DemoMapper">
<select id="selectDemo" parameterType="Demo" resultType="Demo">
select * from student
</select>
<select id="selectName" parameterType="String" resultType="Demo">
select * from student where name=#{name}
</select>
<delete id="deleteStudent" parameterType="Integer" >
delete from student where id = #{id}
</delete>
<insert id="insertStudent" parameterType="Demo">
insert into student(name,age) values(#{name},#{age});
</insert>
<update id="updateStudent" parameterType="Demo" >
update student set name=#{name},age=#{age} where id=#{id};
</update>
<select id="getAllInfo" parameterType="String" resultType="Demo">
SELECT * FROM ${tableName}
</select>
</mapper>
总结
通过demo练习,较为熟练掌握了系统结构,并可以初步实现后台数据的调用,不过需要设计好sql语句来实现不同功能。