mapper
<sql id="Base_Column_List" >
id, username, password, model,miles,year,purchase,maintain
</sql>
<--! 查-->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<--! 查看所有的-->
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
</select>
<--! 删除-->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<--! 增加-->
<insert id="insert" parameterType="com.javen.model.User" >
insert into user (id, username, password, model,miles,year,purchase,maintain)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{model,jdbcType=VARCHAR},#{miles,jdbcType=VARCHAR},#{year,jdbcType=VARCHAR},#{purchase,jdbcType=VARCHAR},#{maintain,jdbcType=VARCHAR})
</insert>
<--! 更改-->
<update id="updateByPrimaryKey" parameterType="com.javen.model.User" >
update user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
model = #{model,jdbcType=VARCHAR},
miles = #{miles,jdbcType=VARCHAR},
year = #{year,jdbcType=VARCHAR},
purchase = #{purchase,jdbcType=VARCHAR},
maintain = #{maintain,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
dao层
import com.javen.model.User;
//dao层接口增删改查
public interface IUserDao {
int insert(User user);
int deleteByPrimaryKey(int id);
int updateByPrimaryKey(User user);
User selectByPrimaryKey(int id);
List<User> selectAll();
}
service层
public interface IUserService {
public int insert(User user);
public int deleteByPrimaryKey(int id);
public int updateByPrimaryKey(User user);
public User selectByPrimaryKey(int id);
public List<User> selectAll();
}
servicelmpl
//实现service中的方法
@Service("userService")
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
public int insert(User user) {
return this.userDao.insert(user);
}
public int deleteByPrimaryKey(int id) {
// TODO Auto-generated method stub
return this.userDao.deleteByPrimaryKey(id);
}
public int updateByPrimaryKey(User user) {
// TODO Auto-generated method stub
return this.userDao.updateByPrimaryKey(user);
}
public User selectByPrimaryKey(int id) {
// TODO Auto-generated method stub
return this.userDao.selectByPrimaryKey(id);
}
public List<User> selectAll() {
// TODO Auto-generated method stub
return this.userDao.selectAll();
}
}
controller层
@RequestMapping(value="/insert", method=RequestMethod.GET)
public String insert(HttpServletRequest request,Model model){
String id = request.getParameter("id");
String password = request.getParameter("password");
User user = new User();
user.setId(Integer.valueOf(id));
user.setPassword(password);
//System.out.println(user.toString());
userService.insert(user);
return "index";
}
@RequestMapping(value="/deleteById", method=RequestMethod.GET)
public String deleteById(HttpServletRequest request,Model model){
Integer id = Integer.valueOf(request.getParameter("id"));
userService.deleteByPrimaryKey(id);
return "index";
}
@RequestMapping(value="/updateByPrimaryKey", method=RequestMethod.GET)
public String updateByPrimaryKey(HttpServletRequest request,Model model){
String model1 = request.getParameter("model");
String miles = request.getParameter("miles");
String year = request.getParameter("year");
String purchase = request.getParameter("purchase");
String maintain = request.getParameter("maintain");
User user = new User();
user.setModel(model1);
user.setMiles(miles);
user.setYear(year);
user.setPurchase(purchase);
user.setMaintain(maintain);
userService.updateByPrimaryKey(user);
return "index";
}
@RequestMapping(value="/selectByPrimaryKey", method=RequestMethod.GET)
public String selectByPrimaryKey(HttpServletRequest request,Model model){
Integer id = Integer.valueOf(request.getParameter("id"));
userService.selectByPrimaryKey(id);
return "index";
}
@RequestMapping(value="/selectAll", method=RequestMethod.GET)
public JSONObject selectAll(HttpServletRequest request,Model model){
List<User> data= new ArrayList<User>();
data = userService.selectAll();
//System.out.println(data);
return "User";
}
}