Skip to content

Commit 105a3d4

Browse files
jdbc demo CRUD
1 parent 7911adb commit 105a3d4

File tree

5 files changed

+224
-0
lines changed

5 files changed

+224
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.controller;
2+
3+
import com.compont.Author;
4+
import com.service.AuthorService_jdbc;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* Created by zhuzhengping on 2017/3/7.
15+
*/
16+
@RestController
17+
@RequestMapping(value = "/data/jdbc/author")
18+
public class AuthorController {
19+
20+
@Autowired
21+
private AuthorService_jdbc authorServiceJdbc;
22+
23+
@RequestMapping(method = RequestMethod.GET)
24+
public Map<String,Object> getAuthorList(HttpServletRequest request){
25+
26+
List<Author> authorList = this.authorServiceJdbc.findAuthorList();
27+
28+
Map<String,Object> param = new HashMap<String,Object>();
29+
30+
param.put("total",authorList.size());
31+
32+
param.put("rows",authorList);
33+
34+
return param;
35+
}
36+
37+
@RequestMapping(value = "/{userId}",method = RequestMethod.GET)
38+
public Author getAuthor(@PathVariable("userId") int userId,HttpServletRequest request){
39+
Author author = this.authorServiceJdbc.findAuthor((long) userId);
40+
if(author == null){
41+
throw new RuntimeException("查询错误");
42+
}
43+
return author;
44+
}
45+
46+
@RequestMapping(method = RequestMethod.POST)
47+
public void add(@RequestBody Map<String,Object> request){
48+
String userId = (String) request.get("user_id");
49+
String realName = (String) request.get("real_name");
50+
String nickName = (String) request.get("nick_name");
51+
Author author = new Author();
52+
if(author != null){
53+
author.setId(Long.valueOf(userId));
54+
}
55+
author.setRealName(realName);
56+
author.setRealName(nickName);
57+
try {
58+
this.authorServiceJdbc.add(author);
59+
}catch (Exception e){
60+
e.printStackTrace();
61+
}
62+
63+
}
64+
65+
@RequestMapping(value = "/{userId}",method = RequestMethod.PUT)
66+
public void update(@PathVariable("userId") Long userId,@RequestBody Map<String,Object> request){
67+
Author author = this.authorServiceJdbc.findAuthor(userId);
68+
String realName = (String) request.get("real_name");
69+
String nickName = (String) request.get("nick_name");
70+
author.setRealName(realName);
71+
author.setRealName(nickName);
72+
try {
73+
this.authorServiceJdbc.update(author);
74+
}catch (Exception e){
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
@RequestMapping(value = "/{userId}",method = RequestMethod.DELETE)
80+
public void delete(@PathVariable("userId") Long userId){
81+
try {
82+
this.authorServiceJdbc.delete(userId);
83+
}catch (Exception e){
84+
e.printStackTrace();
85+
}
86+
}
87+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.dao;
2+
3+
import com.compont.Author;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by zhuzhengping on 2017/3/7.
9+
*/
10+
public interface AuthorDao_jdbc {
11+
12+
int add(Author author);
13+
14+
int update(Author author);
15+
16+
int delete(Long id);
17+
18+
Author findAuthor(Long id);
19+
20+
List<Author> findAuthorList();
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.dao.impl;
2+
3+
import com.compont.Author;
4+
import com.dao.AuthorDao_jdbc;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.BeanPropertyRowMapper;
7+
import org.springframework.jdbc.core.JdbcTemplate;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Created by zhuzhengping on 2017/3/7.
14+
*/
15+
@Repository
16+
public class AuthorDaoImpl implements AuthorDao_jdbc {
17+
18+
@Autowired
19+
private JdbcTemplate jdbcTemplate;
20+
21+
@Override
22+
public int add(Author author) {
23+
return jdbcTemplate.update("INSERT INTO tp_author(real_name,nick_name) VALUES (?,?)",author.getRealName(),author.getNickName());
24+
}
25+
26+
@Override
27+
public int update(Author author) {
28+
return jdbcTemplate.update("UPDATE tp_author SET real_name = ?,nick_name = ? WHERE id = ?",new Object[]{author.getRealName(),author.getNickName(),author.getId()});
29+
}
30+
31+
@Override
32+
public int delete(Long id) {
33+
return jdbcTemplate.update("DELETE FROM tp_author WHERE id = ?",id);
34+
}
35+
36+
@Override
37+
public Author findAuthor(Long id) {
38+
List<Author> list = jdbcTemplate.query("select * from tp_author where id = ?",new Object[]{id},new BeanPropertyRowMapper(Author.class));
39+
if (null != list && list.size() > 0){
40+
Author author = list.get(0);
41+
return author;
42+
}else {
43+
return null;
44+
}
45+
}
46+
47+
@Override
48+
public List<Author> findAuthorList() {
49+
List<Author> list = jdbcTemplate.query("select * from tp_author",new Object[]{},new BeanPropertyRowMapper(Author.class));
50+
return list;
51+
}
52+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.service;
2+
3+
import com.compont.Author;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by zhuzhengping on 2017/3/7.
9+
*/
10+
public interface AuthorService_jdbc {
11+
int add(Author author);
12+
13+
int update(Author author);
14+
15+
int delete(Long id);
16+
17+
Author findAuthor(Long id);
18+
19+
List<Author> findAuthorList();
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.service.impl;
2+
3+
import com.compont.Author;
4+
import com.dao.AuthorDao_jdbc;
5+
import com.service.AuthorService_jdbc;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Created by zhuzhengping on 2017/3/7.
13+
*/
14+
@Service("authorService_jdbc")
15+
public class AuthorServiceImpl implements AuthorService_jdbc {
16+
17+
@Autowired
18+
private AuthorDao_jdbc authorDao_jdbc;
19+
20+
@Override
21+
public int add(Author author) {
22+
return this.authorDao_jdbc.add(author);
23+
}
24+
25+
@Override
26+
public int update(Author author) {
27+
return this.authorDao_jdbc.update(author);
28+
}
29+
30+
@Override
31+
public int delete(Long id) {
32+
return this.authorDao_jdbc.delete(id);
33+
}
34+
35+
@Override
36+
public Author findAuthor(Long id) {
37+
return this.authorDao_jdbc.findAuthor(id);
38+
}
39+
40+
@Override
41+
public List<Author> findAuthorList() {
42+
return this.authorDao_jdbc.findAuthorList();
43+
}
44+
}

0 commit comments

Comments
 (0)