springboot+hibernate+sql service实现简单的增删改查操作(注解开发,前后端分离)

最近接手一个springboot+hibernate+sql service的一个小项目:在这里记录一下自己的 简单实现操作:

coltroller层:

package springBootOltPriject.olt.coltroller;


import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springBootOltPriject.olt.service.OltService;
import springBootOltPriject.olt.vo.SpringBootOtl;





@MapperScan("springBootOltPriject.olt.dao")
//@Controller是这个注解可以return 页面
@RestController
@SpringBootApplication
@ComponentScan("springBootOltPriject.olt.service")
//@RequestMapping("/")
public class CotrollerDemo {
	//注入OltService
	@Autowired
	private OltService oltService;
	
	
	
//根据id查询数据   ===================================================================
	//@GetMapping("/upolt")
	/**
	 * 
	 * @param id
	 * @return
	 */
	
	@RequestMapping(value ="/upolt")
	public SpringBootOtl   getOltMappingById( int  id) {
		System.out.println(id);
		SpringBootOtl springBootOtl=	oltService.getOltMappingById(id);
		return springBootOtl;
	}
							
						////查询所有数据
						//	
						//	@RequestMapping("/oltEdit")
						//	public ModelAndView  getAll(){
						//		
						//		ModelAndView view = new ModelAndView("oltEdit");
						//		//查询数据
						//		List list=oltService.getAll();
						//		view.addObject("list", list);
						//		return view;
						//	}
	
	

//查询所有数据 完成===================================================================
	
	@RequestMapping("/getAll")
	public List  getAll(){
		//查询数据
		List list=oltService.getAll();
		//测试
		System.out.println(list.get(0));
		return list;
	}
	
//根据id删除数据  完成 ==============================================================
	
	//@GetMapping("/delById")
	@RequestMapping(value="/delById")
	public int   delById( int  id){
		//从数据库删除数据
		oltService.delById(id);
		
		return 1;
	}
	
//根据id跟新数据  ==================================================================
	//@GetMapping("/upById")
	@RequestMapping(value="/upById")
	public int   upById( int  id, SpringBootOtl springBootOtl ) {
		//跟新数据
		oltService.upById(springBootOtl);
		
		return 1;
	}
	
//添加数据  完成===================================================================
	@RequestMapping("/save")
	public int    save( SpringBootOtl springBootOtl){
		System.out.println(springBootOtl);
		//保存数据
		oltService.save(springBootOtl);
		
		//return "redirect:/login";
		return 1;
	}
	
					//	@RequestMapping("/login")
					//	public  String login(Model model ) {
					//		//查询数据
					//	List list=oltService.getAll();
					//	System.out.println(list.get(0));
					//	model.addAttribute("list", list);
					//		return "oltList1";
					//	}
	
	
}

注解说明:

@MapperScan("springBootOltPriject.olt.dao")
//用来扫面dAO层数据

@ComponentScan("springBootOltPriject.olt.service")
//用来扫面service层


@RestController
//告诉容器这是一个controller控制层 并将数据封装成json格式和交给spring容器管理

@RequestMapping("/")
//指定这个controller的访问路劲

springboot的控制层与前后交互传值依靠参数注入:即绑定参数在方法体中,当注入参数是单个时 只需要在方法体注入同名即可

当前台传值很多时:可以自己分装一个实体类用于控制层接收参数 (实体类中的变量需要和前台传入参数一致才可以直接接收参数)例如:

接受少量参数

	
//根据id查询数据   ===================================================================
	//@GetMapping("/upolt")
	/**
	 * 
	 * @param id
	 * @return
	 */
	
	@RequestMapping(value ="/upolt")
//接受前台传递的 id (简单是接受少量参数)
	public SpringBootOtl   getOltMappingById( int  id) { 
		System.out.println(id);
		SpringBootOtl springBootOtl=	oltService.getOltMappingById(id);
		return springBootOtl;
	}
	

当大量接受参数时:

//添加数据  完成===================================================================
	@RequestMapping("/save")
    //大量传递参数 实体类接受参数 SpringBootOtl springBootOtl
	public int    save( SpringBootOtl springBootOtl){
		System.out.println(springBootOtl);
		//保存数据
		oltService.save(springBootOtl);
		
		//return "redirect:/login";
		return 1;
	}
	

大大简化参数绑定操作,方便操作

dao层:

package springBootOltPriject.olt.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import springBootOltPriject.olt.vo.SpringBootOtl;

@Mapper
public interface OltMapping {
	
	//根据id查询
	SpringBootOtl getOltMappingById(int id);
	
	//查询所有数据
	List getAll();
	
	//根据id删除数据
	void   delById(Integer id);
	
	//根据id更新数据
	 void   upById(SpringBootOtl springBootOtl);
	
	//添加数据
	void save(SpringBootOtl springBootOtl);
	
	
}

注解:

@Mapper

//告诉容器这是一个dao层 交予容器管理

配置文件:mapper.xml







		
			
			
			
			
			
			
			
			
			
			
			
			
			
		
	


	
	
	

	
	
	

	
	delete  from olt where id=#{id}
	
	
	

	
	UPDATE olt 
	
			
				dev_name = #{dev_name},
			
			
				dev_ip = #{dev_ip},
			
			
				manufacturer = #{manufacturer},
			
			
				modle = #{modle},
			
			
				location = #{location},
			
			
				svlan_cmcc = #{svlan_cmcc},
			
			
				svlan_cucc = #{svlan_cucc},
			
			
				svlan_ct = #{svlan_ct},
			
			
				cvlan_num = #{cvlan_num},
			
			
				note = #{note},
			
			
				create_time = #{create_time},
			
			
				modify_time = getdate(),
			
		
	 WHERE
	  		 id =#{id} 
	
	
	

	
		insert into olt 
		(dev_name,dev_ip,manufacturer,modle,location,svlan_cmcc,svlan_cucc,svlan_ct,cvlan_num,note,create_time,modify_time)
		 values
		 (#{dev_name},#{dev_ip},#{manufacturer},#{modle},#{location},
		 #{svlan_cmcc},#{svlan_cucc},#{svlan_ct},#{cvlan_num},#{note},
		 #{create_time},#{modify_time})
	
	
	

配置文件开发解释:


	

/*标签是 查询