Skip to content

Commit 68c3371

Browse files
springboot && mybatis
1 parent 00e89c2 commit 68c3371

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.controller;
2+
3+
import com.compont.Author;
4+
import com.service.AuthorService_mybatis;
5+
import org.mybatis.spring.annotation.MapperScan;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import javax.servlet.http.HttpServletRequest;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
* Created by zhuzhengping on 2017/3/10.
16+
*/
17+
@RestController
18+
@RequestMapping(value = "/data/mybatis/author")
19+
@MapperScan("com.dao")
20+
public class AuthorController_mybatis {
21+
22+
@Autowired
23+
private AuthorService_mybatis authorServiceMybatis;
24+
25+
@RequestMapping(method = RequestMethod.GET)
26+
public Map<String,Object> getAuthorList(HttpServletRequest request){
27+
28+
List<Author> authorList = this.authorServiceMybatis.findAuthorList();
29+
30+
Map<String,Object> param = new HashMap<String,Object>();
31+
32+
param.put("total",authorList.size());
33+
34+
param.put("rows",authorList);
35+
36+
return param;
37+
}
38+
39+
@RequestMapping(value = "/{userId}",method = RequestMethod.GET)
40+
public Author getAuthor(@PathVariable("userId") int userId, HttpServletRequest request){
41+
Author author = this.authorServiceMybatis.findAuthor((long) userId);
42+
if(author == null){
43+
throw new RuntimeException("查询错误");
44+
}
45+
return author;
46+
}
47+
48+
@RequestMapping(method = RequestMethod.POST)
49+
public void add(@RequestBody Map<String,Object> request){
50+
String userId = (String) request.get("user_id");
51+
String realName = (String) request.get("real_name");
52+
String nickName = (String) request.get("nick_name");
53+
try {
54+
this.authorServiceMybatis.add(realName,nickName);
55+
}catch (Exception e){
56+
e.printStackTrace();
57+
}
58+
59+
}
60+
61+
@RequestMapping(value = "/{userId}",method = RequestMethod.PUT)
62+
public void update(@PathVariable("userId") Long userId,@RequestBody Map<String,Object> request){
63+
Author author = this.authorServiceMybatis.findAuthor(userId);
64+
String realName = (String) request.get("real_name");
65+
String nickName = (String) request.get("nick_name");
66+
try {
67+
this.authorServiceMybatis.update(realName,nickName,author.getId());
68+
}catch (Exception e){
69+
e.printStackTrace();
70+
}
71+
}
72+
73+
@RequestMapping(value = "/{userId}",method = RequestMethod.DELETE)
74+
public void delete(@PathVariable("userId") Long userId){
75+
try {
76+
this.authorServiceMybatis.delete(userId);
77+
}catch (Exception e){
78+
e.printStackTrace();
79+
}
80+
}
81+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.dao;
2+
3+
import com.compont.Author;
4+
import org.apache.ibatis.annotations.*;
5+
import org.springframework.data.repository.query.Param;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Created by zhuzhengping on 2017/3/10.
12+
*/
13+
@Component
14+
@Mapper
15+
public interface AuthorMapper_mybatis {
16+
17+
@Insert("NSERT INTO tp_author(real_name,nick_name) VALUES (#{real_name},#{nick_name})")
18+
int add(@Param("realName") String realName,@Param("nick_name") String nickName);
19+
20+
@Update("UPDATE tp_author SET real_name = #{real_name},nick_name = #{nick_name} WHERE id = #{id}")
21+
int update(@Param("realName") String realName,@Param("nick_name") String nickName,@Param("id") Long id);
22+
23+
@Delete("DELETE FROM tp_author WHERE id = #{id}")
24+
int delete(Long id);
25+
26+
@Select("select id,real_name as realName,nick_name as nickName from tp_author where id = #{id}")
27+
Author findAuthor(@Param("id") Long id);
28+
29+
@Select("select id,real_name as realName,nick_name as nickName from tp_author")
30+
List<Author> findAuthorList();
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.service;
2+
3+
import com.compont.Author;
4+
import com.dao.AuthorMapper_mybatis;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Created by zhuzhengping on 2017/3/10.
12+
*/
13+
14+
@Service
15+
public class AuthorService_mybatis {
16+
17+
@Autowired
18+
private AuthorMapper_mybatis authorMapperMybatis;
19+
20+
21+
public int add(String realName,String nickName) {
22+
return this.authorMapperMybatis.add(realName,nickName);
23+
}
24+
25+
26+
public int update(String realName,String nickName,Long id) {
27+
return this.authorMapperMybatis.update(realName,nickName,id);
28+
}
29+
30+
31+
public int delete(Long id) {
32+
return this.authorMapperMybatis.delete(id);
33+
}
34+
35+
36+
public Author findAuthor(Long id) {
37+
return this.authorMapperMybatis.findAuthor(id);
38+
}
39+
40+
41+
public List<Author> findAuthorList() {
42+
return this.authorMapperMybatis.findAuthorList();
43+
}
44+
}

0 commit comments

Comments
 (0)