springboot+mybatis的深化

110 篇文章 0 订阅
29 篇文章 0 订阅

接口

package com.yanshu.dao;


import java.util.List;


import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Param;
import com.yanshu.pojo.Emp;


/**
 * mybatis
 * @author Administrator
 * @Select 是查询类的注解,所有的查询均使用这个
@Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
@Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值
@Update 负责修改,也可以直接传入对象
@delete 负责删除
 */
@Mapper
public interface EmpDao {
@Select("select * from emp")
List<Emp> findEmpByAll();
@Select("select COUNT(*) from emp")
int findEmpByNames();
/**
* 参数传值
* @param name
*/
@Insert("insert into emp(name) values(#{name})")
void insertsName(@Param("name")String name);
@Select("select * from emp where name= #{name}")
List<Emp> findUserByName(@Param("name")String name);
/**
* 经过实体传参
* @param emp
*/
@Insert("insert into emp(name,sex) values(#{name},#{sex})")
void insertEmp(Emp emp);
/**
* 修改参数传参/实体传参
*/
@Update("update emp set sex=#{sex} where id=#{id}")
void updateParam(Emp emp);
@Update("update emp set sex=#{sex} where id=#{id}")
void updateEmp(@Param("sex")String sex,@Param("id")Integer id);

/**
* 删除参数传参/实体传参
*/
@Delete("delete emp where id=#{id}")
void deleteParam(@Param("id")Integer id);
@Delete("delete emp where id=#{id}")
void deleteEmp(Emp emp);





}

--

2.controller

package com.yanshu.controller;


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


import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.alibaba.fastjson.JSON;
import com.yanshu.dao.EmpDao;
import com.yanshu.pojo.Emp;
/**
 * 使用mybatis不会改变数据结构
 * @author Administrator
 *
 */
@RestController
public class EmpController {
@Autowired
private EmpDao empdao;
@RequestMapping("/show")
public String getShow()
{
return "项目启动";

}
/**
* 查询数据
* @return
*/
@RequestMapping("/select")
public String getShowa()
{
List<Emp> emp=empdao.findEmpByAll();
int e=empdao.findEmpByNames();
Map<String,Object> map=new HashMap<>();
map.put("emp", emp);
map.put("count", e);
return JSON.toJSONString(map);

}
/**
* 添加数据
* 经过参数传参,有一定的风险
* @param name
* @return
*/
@RequestMapping("/insert")
public String InsertName(String name)
{
empdao.insertsName(name);
List<Emp> emp=empdao.findUserByName(name);
Map<String,Object> map=new HashMap<>();
map.put("emp", emp);
return JSON.toJSONString(map);
}
/**
* 添加数据
* 经过实体传参,提高了数据的安全性
* @param emp
* @return
*/
@RequestMapping("/insertemp")
public String InsertEmp(Emp emp)
{
empdao.insertEmp(emp);
List<Emp> emp1=empdao.findEmpByAll();
int e=empdao.findEmpByNames();
Map<String,Object> map=new HashMap<>();
map.put("emp", emp1);
map.put("count", e);
return JSON.toJSONString(map);
}
/**
* 修改操作
* 经过参数传参
*/
@RequestMapping("/update")
public String UpdateParam(String sex,Integer id)
{
empdao.updateEmp(sex, id);
List<Emp> emp1=empdao.findEmpByAll();
int e=empdao.findEmpByNames();
Map<String,Object> map=new HashMap<>();
map.put("emp", emp1);
map.put("count", e);
return JSON.toJSONString(map);

}
/**
* 修改操作 
* 经过实体传参
* http://localhost:8080/bootMybatis/updateemp?sex=33cc&id=33
* @param emp
* @return
*/
@RequestMapping("/updateemp")
public String UpdateEmp(Emp emp)
{
empdao.updateParam(emp);
List<Emp> emp1=empdao.findEmpByAll();
int e=empdao.findEmpByNames();
Map<String,Object> map=new HashMap<>();
map.put("emp", emp1);
map.put("count", e);
return JSON.toJSONString(map);

}
/**
* 删除操作
* 经过参数传参
* http://localhost:8080/bootMybatis/delete?id=35
*/
@RequestMapping("/delete")
public String DeleteParam(Integer id)
{
empdao.deleteParam(id);
List<Emp> emp1=empdao.findEmpByAll();
int e=empdao.findEmpByNames();
Map<String,Object> map=new HashMap<>();
map.put("emp", emp1);
map.put("count", e);
return JSON.toJSONString(map);


}
/**
* 删除操作
* 经过参数实体
* http://localhost:8080/bootMybatis/delete?id=35
*/
@RequestMapping("/deleteemp")
public String DeleteEmp(Emp emp)
{
empdao.deleteEmp(emp);
List<Emp> emp1=empdao.findEmpByAll();
int e=empdao.findEmpByNames();
Map<String,Object> map=new HashMap<>();
map.put("emp", emp1);
map.put("count", e);
return JSON.toJSONString(map);


}





}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值