一、Mybatis
例:
staffmapper:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper的namespace属性指向关联的接口的全名 -->
<mapper namespace="com.easy.dao.IStaffDao">
<!--id要和IStaffDao里面的方法一样 resulttype可以写全称com.easy.bean.Staff -->
<select id="getList" resultType="Staff" parameterType="staff">
<!-- ${} 拼接字符串的形式 可以作为表名,列名来使用,一般不会用来传递参数,会出现sql注入的情况
#{}是一种预编译的形式,语句的结构和语义已经确定,可以有效的防止sql注入
-->
<!-- select * from t_staff where name='${name}' and sex='${sex}' -->
select * from t_staff where 1=1
<!-- name=#{name} and sex=#{sex} -->
<if test="name!=null">
and name=#{name}
</if>
<if test="sex!=null">
and sex=#{sex}
</if>
</select>
<update id="edit" parameterType="map">
update t_staff set name=#{name},sex=#{sex} where id=#{id}
</update>
<insert id="add" parameterType="staff" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into t_staff(name,sex,age) values (#{name},#{sex},#{age})
</insert>
</mapper>
IStaffDao:
package com.easy.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.easy.bean.Staff;
@Mapper
@Repository
public interface IStaffDao {
//跟mapper传入的参数名字可以一样 也可以不一样,不一样的话就用param声明一下
//List getList(@Param("name") String staffname);
List getList(Staff staff);
int edit(Map params);
int add(Staff staff);
}
StaffService:
package com.easy.service.impl;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.easy.bean.Staff;
import com.easy.dao.IStaffDao;
import com.easy.service.IBaseService;
//staffSer 给StaffService类起一个名字
@Service("staffSer")
public class StaffService {
@Autowired
IStaffDao dao;
public List getList(Object params) {
List list=dao.getList((Staff)params);
return list;
}
public Object getOne(Integer id) {
return null;
}
public void edit(Map params) {
dao.edit(params);
}
public void add(Staff staff) {
dao.add(staff);
}
// public void add1(Map params) {
// //准备id
// String uuid=UUID.randomUUID().toString();
// params.put("uuid",uuid);
// dao.add(params);
// }
}
StaffController:
package com.easy.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.easy.bean.Staff;
import com.easy.service.impl.StaffService;
@Controller
@RequestMapping("staff")
public class StaffController {
@Autowired
StaffService staffSer;
@RequestMapping("list")
@ResponseBody
public List getList(Staff staff) {
List list=staffSer.getList(staff);
return list;
}
@RequestMapping("edit")
@ResponseBody
//map类型需要@RequestParam注解
public void edit(@RequestParam Map params) {
staffSer.edit(params);
}
@RequestMapping("add")
@ResponseBody
public Staff add(Staff staff) {
staffSer.add(staff);
return staff;
}
}