发表于: 2018-03-30 20:24:05
1 626
今天完成的事情:
完成基本的增删改查功能,还有大量缺陷陆续修改
package com.controller;
import com.DAO.StudentMapper;
import com.POJO.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/user")
public class mvcController {
Student student;
@Autowired
StudentMapper studentMapper;
@RequestMapping("/searchbyid")
public String jumptosearchid( ) throws IOException {
return "searchbyid";
}
/*
* 根据ID查询信息
*
* */
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String select( Integer ID, Model model) throws Exception {
student = studentMapper.findUserById(ID);
model.addAttribute("student", student);
return "list";
}
/**
* 新增用户
*/
@RequestMapping("/new")
public String turntoinsert( ) throws IOException {
return "insert";
}
@RequestMapping(value = "/add")
public String insert(Student newStudent,Model model) throws Exception {
studentMapper.insertUser(newStudent);
Integer a = newStudent.getID();
model.addAttribute("message2",a);
return "addcomplate";
}
/**
* 删除用户
*/
@RequestMapping(value = "/deletebyid/{ID}")
public String delect(@PathVariable("ID") int ID, Model model) throws Exception {
int a = studentMapper.deleteUser(ID);
String message;
if (a == 1) {
message = "数据删除成功";
} else {
message = "数据删除失败";
}
model.addAttribute("message", message);
return "deletebyid";
}
/**
* 修改用户
*/
@RequestMapping(value = "/list/{ID}",method = RequestMethod.GET)
public String jumptoupdate(@PathVariable("ID") Integer ID, Model model) throws Exception {
student = studentMapper.findUserById(ID);
System.out.println(student);
model.addAttribute("student", student);
return "updatebyid";
}
@RequestMapping(value = "/updatecomplate")
public String update(Student updatestudent,Model model) throws Exception {
int s = studentMapper.updateUser(updatestudent);
String message;
if (s == 1) {
message = "数据修改成功";
} else {
message = "数据修改失败";
}
model.addAttribute("message", message);
return "updatecomplate";
}
}
package com.DAO;
import com.POJO.Student;
public interface StudentMapper {
//根据id查询用户信息
public Student findUserById(int id) throws Exception;
//根据用户名模糊查询
// public List<User> findUserByName(String name) throws Exception;
//添加用户信息
public int insertUser(Student student) throws Exception;
//删除用户信息
public int deleteUser(int id) throws Exception;
//更新用户信息
public int updateUser(Student student) throws Exception;
}
<?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="com.DAO.StudentMapper">
<!-- 根据ID查找用户 -->
<select id="findUserById" parameterType="int" resultType="com.POJO.Student">
select * from student where ID = #{id}
</select>
<!--<select id="findUserByname" parameterType="java.lang.String" resultType="com.POJO.Student">-->
<!--select * from student where username like '%${value}%'-->
<!--</select>-->
<!-- 新增用户 -->
<insert id="insertUser" parameterType="com.POJO.Student">
insert into student(name,QQ,onlineNumber,enrollmentTime,professionType,dailyLink,promise,brotherName) values(#{name},#{QQ},#{onlineNumber},#{enrollmentTime},#{professionType},#{dailyLink},#{promise},#{brotherName})
<selectKey keyProperty="ID" order="AFTER" resultType="java.lang.Integer">
select last_insert_id()
</selectKey>
</insert>
<!-- 根据ID删除用户 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from student where ID=#{id}
</delete>
<!-- 修改用户 -->
<!--<update id="updateUser" parameterType="com.POJO.Student">-->
<!--update student set name=#{name} where ID=#{id}-->
<!--</update>-->
<update id="updateUser" parameterType="com.POJO.Student">
UPDATE `student `
<set><!--set标签会去除只有最后一个条件的,-->
<if test="name!= null and name!=''">name = #{name},</if>
<if test="QQ!= null and QQ!=''">QQ = #{QQ},</if>
<if test="onlineNumber!= null and onlineNumber!=''">onlineNumber = #{onlineNumber},</if>
<if test="enrollmentTime!= null ">enrollmentTime = #{enrollmentTime},</if>
<if test="professionType!= null and professionType!=''">professionType = #{professionType},</if>
<if test="dailyLink!= null and dailyLink!=''">dailyLink = #{dailyLink},</if>
<if test="promise!= null and promise!=''">promise = #{promise},</if>
<if test="brotherName!= null and brotherName!=''">brotherName = #{brotherName},</if>
</set>
<where><!--where标签会去除第一个条件的and和or-->
<if test="ID!=null">AND ID=#{ID}</if>
</where>
</update>
</mapper>
明天计划的事情:
完成模糊查询,解决修改不能单独修改的问题
遇到的问题:
503错误,发现是空指针的问题,一般这种问题就是大小写对应不上,或者方法对应的对象不正确
400错误,请求方法不正确,我的是因为,删除和查询都引用了同一个jsp文件
修改不能耽搁修改,估计是动态查询SQL语句没写对
收获:
接口存在的意义(也是面向接口编程的原因和不直接使用类的原因)
1 重要性:赋予Java强大的面向对象能力(暂时没有体会)
2 简单性,规范性:如果一个项目比较庞大,定义一些主要的接口。既可以告诉开发人员你需要实现哪些业务,而且也将命名规范限制住。
3 维护,拓展性:
package com.DAO;
import com.POJO.Student;
public interface StudentMapper {
//根据id查询用户信息
public Student findUserById(int id) throws Exception;
//根据用户名模糊查询
// public List<User> findUserByName(String name) throws Exception;
//添加用户信息
public int insertUser(Student student) throws Exception;
//删除用户信息
public int deleteUser(int id) throws Exception;
//更新用户信息
public int updateUser(Student student) throws Exception;
}
现在我把增删改查的功能放在接口中,我需要使用时直接引用即可
public String insert(Student newStudent,Model model) throws Exception {
studentMapper.insertUser(newStudent);
Integer a = newStudent.getID();
model.addAttribute("message2",a);
return "addcomplate";
}
而不需要自己定义这个类,而且以后需要更换引用时只不过更换一个接口就是。这样就达到维护和扩展的方便性
4 安全性和严密性:接口是实现软件松耦合的重要手段,如上所示描述了对外的服务,但没有涉及任何具体的实现细节。这样就比较安全严密。
评论