java-CRUD

DepartmentController:

package com.teach.ssm.mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.ssm.mvc.domain.Department;
import com.teach.ssm.mvc.domain.News;
import com.teach.ssm.mvc.dto.DepartmentDto;
import com.teach.ssm.mvc.service.DepartmentService;


@Controller
public class DepartmentController {
	
	@Autowired
	private DepartmentService departmentService ;
	
	@RequestMapping("/pubDepartment")
	@ResponseBody
	public Map<String, Object> pubNews(@RequestBody DepartmentDto departmentDto) {
		//1.
		Department department = departmentDto.getDepartment();
		
		//2.    service
		int count = departmentService.pubDepartment(department);
		
		Map<String, Object> ret = new HashMap(){{
			put("code", 200);
			put("message","添加数据成功");
		}};
		
		//4
		return ret;
	}
	
	@RequestMapping("/queryDepartmentByName")
	@ResponseBody
	public Map<String, Object> queryDepartmentByName(@RequestBody Map<String, Object> params){
		String name=(String)params.get("name");
		List<Department> departmentsList=departmentService.queryDepartmentByName(name);
		//3
		Map<String, Object> retData=new HashMap(){{
			put("departmentsList",departmentsList);
		}};
		Map<String, Object> ret = new HashMap(){{
			put("code", 200);
			put("message","根据name模糊查询成功");
			put("data", retData);
			
		}};
		return ret;
	}
	
	@RequestMapping("/modifyDepartmentNameById")
	@ResponseBody
	public Map<String, Object> modifyDepartmentNameById(@RequestBody Map<String, Object> params){
		Integer id=(Integer)params.get("id");
		String name = (String)params.get("name");
		Department department=new Department();
		department.setId(id);
		department.setName(name);
		
		departmentService.modifyDepartmentNameById(department);
		
		Map<String, Object> ret=new HashMap(){{
			put("code",200);
			put("message","修改id="+department.getId()+"数据成功");
		}};
		return ret;
	}
	
	@RequestMapping("/deleteDepartmentId")
	@ResponseBody
	private Map<String, Object>deleteDepartmentId(@RequestBody Map<String, Object> params){
		Integer id=(Integer)params.get("id");
		
		departmentService.deleteDepartmentId(id);
		
		Map<String, Object> ret=new HashMap(){{
			put("code",200);
			put("message","成功删除id="+params.get("id")+"的数据");
		}};
		return ret;
	}

}

NewsController:

package com.teach.ssm.mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.ssm.mvc.domain.News;
import com.teach.ssm.mvc.dto.NewsDto;
import com.teach.ssm.mvc.service.NewsService;

@Controller
public class NewsController {
	
	
	@Autowired
	private NewsService newsService;
	
	@RequestMapping("/pubNews")
	@ResponseBody
	public Map<String, Object> pubNews(@RequestBody NewsDto newsDto){
		//1
		News news = newsDto.getNews();
		//2
		int pubCount = newsService.pubNews(news);
		//3
		
		Map<String, Object> ret = new HashMap(){{
			put("code", 200);
			put("message", "成功发布新闻"+pubCount+"条,id="+news.getId());
			
		}};
		//4
		return ret;
	}
	
	
	@RequestMapping("/queryNewsByTitle")
	@ResponseBody
	public Map<String, Object> queryNewsByTitle(@RequestBody Map<String, Object> params){
		String title=(String)params.get("title");
		List<News> newsList=newsService.queryNewsByTitle(title);
		//3
		Map<String, Object> retData=new HashMap(){{
			put("newsList",newsList);
		}};
		Map<String, Object> ret = new HashMap(){{
			put("code", 200);
			put("message","根据title模糊查询成功");
			put("data", retData);
			
		}};
		return ret;
	}
	
	
	
	@RequestMapping("/modifyNewsTitleById")
	@ResponseBody
	public Map<String, Object> modifyNewsTitleById(@RequestBody Map<String, Object> params){
		Integer id=(Integer)params.get("id");
		String title = (String)params.get("title");
		News news=new News();
		news.setId(id);
		news.setTitle(title);
		
		newsService.modifyNewsTitleById(news);
		
		Map<String, Object> ret=new HashMap(){{
			put("code",200);
			put("message","修改id="+news.getId()+"新闻成功!");
		}};
		return ret;
	}

	
	@RequestMapping("/deleteId")
	@ResponseBody
	private Map<String, Object>deleteId(@RequestBody Map<String, Object> params){
		Integer id=(Integer)params.get("id");
		
		newsService.deleteId(id);
		
		Map<String, Object> ret=new HashMap(){{
			put("code",200);
			put("message","成功删除id="+params.get("id")+"的新闻");
		}};
		return ret;
	}
	
	
	@RequestMapping("/removeNewsByIds")
	@ResponseBody
	public Map<String, Object> removeNewsByIds(@RequestBody Map<String, Object> params) {
		
		//获取参数
		List<Integer> idList = (List<Integer>)params.get("ids");
		Integer[] ids = idList.toArray(new Integer[0]);
		
		//调用service方法,并获取数据
		int deleteCount = newsService.removeNewsByIds(ids);
		
		//包装数据
		Map<String, Object> data = new HashMap() {{
			put("deleteCount", deleteCount);
		}};
		Map<String, Object> ret = new HashMap() {{
			put("code", 200);
			put("message", "根据id数组删除新闻成功");
			put("data", data);
		}};
		
		//返回数据
		return ret;
		
	}
   
	
}

TestController:

package com.teach.ssm.mvc.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.ssm.mvc.service.NewsService;

@Controller
public class TestController {
	@Autowired

	private NewsService newsService;
	
	
	@RequestMapping("/test")
	@ResponseBody
	public Map<String, Object> test() {
		System.out.println("in TestController test.....");
		//1.获得请求
		
		//2.调用service方法,获取返回的数据
		//newsService.pubNews();
		
		//3.包装数据
		Map<String, Object> ret = new HashMap(){{
			put("code", 200);
			put("message", "ok");
		}};
		
		//4.返回数据
		return ret;
	}

}

DepartmentDao:

package com.teach.ssm.mvc.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.teach.ssm.mvc.domain.Department;


@Repository
public interface DepartmentDao {
	@Insert("insert into department(name, description,status,goal) values(#{name}, #{description}, #{status}, #{goal})")
	@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
	public int createDepartment(Department department);
	
	//@Select("select id, name, description, status, goal form department where id =#{id}")
	//public Department readDepartmentById(int id);
	@Select("select id, name, description, status, goal from department where id=#{id}")
	public Department readDepartmentById(int id);

	@Select("select id, name, description, status, goal from department where name like #{name}")
	public List<Department> readDepartmentByName(String name);
	
	@Update("update department set name=#{name} where id=#{id}")
	public int updateDepartmentNameById(Department department); 
	
	@Delete("delete from department where id = #{id}")
	public int deleteDepartmentById(int id);
}

NewsDao:

package com.teach.ssm.mvc.dao;


import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.teach.ssm.mvc.domain.News;

@Repository
public interface NewsDao {
	
	
	@Insert("insert into news(title, author,content) values(#{title}, #{author}, #{content})")
	@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
	public int createNews(News news);
	
	@Select("select id, title, author, content form news where id =#{id}")
	public News readNewsById(int id);
	
	@Select("select id, title, author,content from news where title like #{title}")
	public List<News> readNewsByTitle(String title);
	
	@Update("update news set title=#{title} where id=#{id}")
	public int updateNewsTitleById(News news);
	
	
	@Delete("delete from news where id = #{id}")
	public int deleteNewsById(int id);
	
	@Delete("delete from news where id in (${ids})")
	public int deleteNewsByIds(@Param("ids") String ids);
}

Department:

package com.teach.ssm.mvc.domain;

public class Department {
     private int id;
     private String  name;
     private String description;
     private String status;
         private String goal;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() { 
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getGoal() {
		return goal;
	}
	public void setGoal(String goal) {
		this.goal = goal;
	}
	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + ", description=" + description + ", status=" + status
				+ ", goal=" + goal + "]";
	}
	
     
     
}

NewsDao:

package com.teach.ssm.mvc.domain;

public class News {
	private int id;
	private String title;
	private String author;
	private String content;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public String toString() {
		return "News [id=" + id + ", title=" + title + ", author=" + author + ", content=" + content + "]";
	}

}

DepartmentDto:

package com.teach.ssm.mvc.dto;

import com.teach.ssm.mvc.domain.Department;

public class DepartmentDto {
	private Department department;
	
	public Department getDepartment(){
		return department;
	}
	public void setDepartment(Department department){
		this.department=department;
	}

}

NewsDto:

package com.teach.ssm.mvc.dto;

import com.teach.ssm.mvc.domain.News;

public class NewsDto {
    private News news;

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}
    
    
}

DepartmentService:

package com.teach.ssm.mvc.service;

import java.util.List;

import com.teach.ssm.mvc.domain.Department;
import com.teach.ssm.mvc.domain.News;


public interface DepartmentService {
	
	public int pubDepartment(Department department);
	
	//public List<Department> queryDepartmentByName(String name);
	public List<Department> queryDepartmentByName(String name);
	
	public int modifyDepartmentNameById(Department department);
	
	public int deleteDepartmentId(int id);

}
 

NewsService:

package com.teach.ssm.mvc.service;

import java.util.List;

import com.teach.ssm.mvc.domain.News;

public interface NewsService {
   
    public int pubNews(News news);
    
	public List<News> queryNewsByTitle(String title);
	
	public int modifyNewsTitleById(News news);
	
	public int deleteId(int id);

	int removeNewsByIds(Integer[] ids);
	
	
}

DepartmentServiceImpl:

package com.teach.ssm.mvc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teach.ssm.mvc.dao.DepartmentDao;
import com.teach.ssm.mvc.domain.Department;
import com.teach.ssm.mvc.service.DepartmentService;

@Service
public class DepartmentServiceImpl implements DepartmentService {

	@Autowired
	private DepartmentDao departmentDao;

	@Override
	public int pubDepartment(Department department) {
		int result = departmentDao.createDepartment(department);
		return result;
	}

	@Override
	public List<Department> queryDepartmentByName(String name) {
		// TODO Auto-generated method stub
		return departmentDao.readDepartmentByName(name);
	}

	@Override
	public int modifyDepartmentNameById(Department department) {
		// TODO Auto-generated method stub
		return departmentDao.updateDepartmentNameById(department);
	}

	@Override
	public int deleteDepartmentId(int id) {
		// TODO Auto-generated method stub
		return departmentDao.deleteDepartmentById(id);
	}

}

NewsServiceImpl:

package com.teach.ssm.mvc.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.teach.ssm.mvc.dao.NewsDao;
import com.teach.ssm.mvc.domain.News;
import com.teach.ssm.mvc.service.NewsService;

@Service
public class NewsServiceImpl implements NewsService{
	
	@Autowired
	private NewsDao newsDao;
	
	
	@Override
	public int pubNews(News news) {
		// TODO Auto-generated method stub
		int result = newsDao.createNews(news);
		//System.out.println(news.getId());
		return result;
	}


	@Override
	public List<News> queryNewsByTitle(String title) {
		// TODO Auto-generated method stub
		return newsDao.readNewsByTitle("%"+title+"%");
	}


	@Override
	public int modifyNewsTitleById(News news) {
		// TODO Auto-generated method stub
		return newsDao.updateNewsTitleById(news);
	}


	@Override
	public int deleteId(int id) {
		// TODO Auto-generated method stub
		return newsDao.deleteNewsById(id);
	}

	@Override
	public int removeNewsByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		//ids=[1,2,3,4]    =>1,2,3,4
		return newsDao.deleteNewsByIds(StringUtils.join(ids,","));
	}
	

}

spring:

<context:component-scan base-package="com.teach.ssm.mvc.service" />

	<context:annotation-config />


	<!-- 引入db配置 -->
	<context:property-placeholder location="classpath:config/jdbc.properties" />
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 让mybatis使用spring创建的数据源 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 配置mybatis扫描的dao包 -->
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.teach.ssm.mvc.dao" />
	</bean>

springmvc:

<!-- spring mvc 框架采用注解 注入 Bean -->
	<context:component-scan base-package="com.teach.ssm.mvc.controller" />

	<mvc:annotation-driven />


	<!-- 拦截器配置 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" /> 
		<bean class="com.teach.ssm.mvc.interceptor.LogInterceptor" /> </mvc:interceptor> 
		</mvc:interceptors> -->

web.xml:

<!-- springMVC -->
	<servlet>
		<servlet-name>springMvcServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvcServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 配置spring -->
    <context-param>
	     <param-name>contextConfigLocation</param-name>
	     <param-value>classpath:config/spring.xml</param-value>
     </context-param>
     <listener>
	      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
	
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值