一,实体层
这个层主要就是跟数据库的表中数据结构对应
例如
public class loginUser implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@JSONField(name="ID")
private int id;
@JSONField(name="NAME")
private String name;
private String birth_date;
private String phone;
public String getName() {
return name;
}
public void setName(String username) {
this.name = username;
}
public void setBirth_date(String birth_date) {
this.birth_date = birth_date;
}
public String getBirth_date() {
return this.birth_date;
}
public void setPhone(String pon) {
this.phone = pon;
}
public String getPhone() {
return this.phone;
}
public loginUser(){};
public loginUser(int id ,String name,String birth,String phone) {
this.id = id;
this.birth_date = birth;
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "User [username=" + name + ", password=" + birth_date +", phone =" + phone + "]";
}
}
二 Mapper层
主要进行数据持久化,例如使用的是Mybatis框架,在xml 配置文件编写相关sql 语句进行增删改查操作
例如xml文件内容如下:
<?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="game.UserMappter">
<select id="Sel" resultType="game.loginUser" parameterType="int" >
select * from users where id = #{id}
</select>
<delete id="del" parameterType="int" >
delete from users where id = #{id}
</delete>
<insert id="addUser" parameterType="game.loginUser">
</insert>
<select id="selectAllReturnBigMap" resultType="map">
select * from users
</select>
</mapper>
对应的Mapper类
@Mapper
public interface UserMappter {
loginUser Sel(int id) ;
void del(int id);
void addUser(loginUser usr);
List< Map<String,Object>> selectAllReturnBigMap();
}
需要注意的是,Mapper类的函数方法名需与xml文件中的 各操作标签的id的值相一致。
三, 服务层
服务层也叫业务层,主要进行向controller层传递数据
例如:
public interface UserService{
public loginUser getUser(int id);
public void delUser(int id);
public void addUser(JSONObject json);
}
@Service
public class UserServiceImpl implements UserService{
@Resource
UserMappter userMapper;
@Override
public loginUser getUser(int id) {
// TODO Auto-generated method stub
return userMapper.Sel(id);
}
@Override
public void delUser(int id) {
// TODO Auto-generated method stub
userMapper.del(id);
}
@Override
public void addUser(JSONObject json) {
// TODO Auto-generated method stub
loginUser usr = new loginUser();
usr.setBirth_date(json.getString("birth_date"));
userMapper.addUser(null);
}
}
四,控制器层
这个层前端通过调用接口的方式,将数据返回给前端
例如
@RequestMapping("/user")
@RestController
public class HelloController {
@Autowired
UserService serv;
@Autowired
private UserService userService;
@GetMapping("/getUser")
public String GetUser(){
return userService.getUser(6).toString();
}
@GetMapping("/deleteUser")
public Integer delete( int id) {
// userService.delUser(6);
System.out.print("在这里" + id);
return 1;
}
//public String RemoveUser(@RequestBody Integer id) {
// userService.delUser(Integer.valueOf(id));
// return "success";
//}
@ResponseBody
@RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String addUser(@RequestBody JSONObject jsonParam) {
// 直接将json信息打印出来
//System.out.println(jsonParam.toJSONString());
// 将获取的json数据封装一层,然后在给返回
JSONObject result = new JSONObject();
result.put("msg", "ok");
result.put("method", "json");
result.put("data", jsonParam);
result.put("code",1);
return result.toJSONString();
}
}