发表于: 2016-06-05 12:23:14
0 1222
controller :
package com.bude.ww.controller;
import com.bude.ww.model.BaseResponse;
import com.bude.ww.model.User;
import com.bude.ww.services.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by andy on 2016/6/1.
*/
@RestController
@RequestMapping("users")
public class UserController {
Logger logger= LoggerFactory.getLogger(UserController.class);
private BaseResponse resp;
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.POST)
public Object add(User user){
logger.info("add");
userService.addUser(user);
return user;
}
@RequestMapping(method = RequestMethod.PUT)
public Object update(User user){
user = userService.updateUser(user);
return resp;
}
@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
public Object delete(@PathVariable("id") Integer id){
Boolean ret = userService.deleteUser(id);
return ret;
}
@RequestMapping(value = "{id}",method = RequestMethod.GET)
public Object get(@PathVariable("id") Integer id){
logger.info("get "+id);
User user = userService.getUser(id);
return user;
}
@RequestMapping(method = RequestMethod.GET)
public Object getAll(){
return resp;
}
}
public interface UserService {
User addUser(User user);
User updateUser(User user);
Boolean deleteUser(Integer id);
User getUser(Integer id);
List<User> getUserAll();
}
package com.bude.ww.services;
import com.bude.ww.DAO.UseDao;
import com.bude.ww.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by andy on 2016/6/1.
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UseDao userDao;
@Override
public User addUser(User user) {
return userDao.add(user);
}
@Override
public User updateUser(User user) {
return userDao.update(user);
}
@Override
public Boolean deleteUser(Integer id) {
int ret = userDao.delete(id);
return ret>0?Boolean.TRUE:Boolean.FALSE;
}
@Override
public User getUser(Integer id) {
return userDao.get(id);
}
@Override
public List<User> getUserAll() {
return null;
}
}
package com.bude.ww.DAO;
import com.bude.ww.UserMapper;
import com.bude.ww.model.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
/**
* Created by andy on 2016/6/5.
*/
@Repository
public class UserDao {
@Autowired
private SqlSession sqlSession;
public UserMapper getMapper(){
return sqlSession.getMapper(UserMapper.class);
}
public User add(User user){
int ret = getMapper().insert(user);
if(ret > 0){
}
return user;
}
public User update(User user){
int ret = getMapper().updateByPrimaryKey(user);
if(ret > 0){
}
return user;
}
public User get(Integer userId){
User ret = getMapper().selectByPrimaryKey(userId);
return ret;
}
public int delete(Integer userId){
int ret = getMapper().deleteByPrimaryKey(userId);
if(ret > 0){
}
return ret;
}
}
sevlet 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<mvc:annotation-driven>
</mvc:annotation-driven>
<context:component-scan base-package="com.bude.ww"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<array>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</array>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="cnManager"/>
</bean>
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="ignoreAcceptHeader" value="true"/>
<property name="defaultContentType" value="text/json" />
</bean>
</beans>
评论