最近接手一个springboot+hibernate+sql service的一个小项目:在这里记录一下自己的 简单实现操作:
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的访问路劲
接受少量参数
//根据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;
}
大大简化参数绑定操作,方便操作
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层 交予容器管理
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})
/*标签是 查询
最后将配置文件配置到mybatis.xml中
package springBootOltPriject.olt.service;
import java.sql.Timestamp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springBootOltPriject.olt.dao.OltMapping;
import springBootOltPriject.olt.vo.SpringBootOtl;
@Service("oltService")
public class OltService {
//注入dao层
@Autowired
private OltMapping oltMapping;
//根据id查询数据
public SpringBootOtl getOltMappingById(int id){
SpringBootOtl springBootOtl=oltMapping.getOltMappingById(id);
return springBootOtl;
}
//更新数据
public void upById(SpringBootOtl springBootOtl){
Timestamp d = new Timestamp(System.currentTimeMillis());
springBootOtl.setModify_time(d);
oltMapping.upById(springBootOtl);
}
//删除数据
public void delById(int id){
oltMapping.delById(id);
}
//插入数据
//@Options(useGeneratedKeys= true)使用自增长
//@Options(useGeneratedKeys= true,keyProperty="id")
public void save(SpringBootOtl springBootOtl){
/*SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string=simpleDateFormat.format(new Date());*/
Timestamp d = new Timestamp(System.currentTimeMillis());
//设置日期格式
//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//转化为字符串
//String time = df.format(new Date());
//转化为日期格式
//Timestamp ts = Timestamp.valueOf(time);
springBootOtl.setCreate_time(d);
oltMapping.save(springBootOtl);
}
//查询所有数据
public List getAll() {
List list = oltMapping.getAll();
if(list!=null &&list.size()>0) {
return list;
}
return null;
}
}
注解:
@Service
//标志服务层 交予容器