发表于: 2018-03-27 23:50:00

1 621


//标记一个类是Controller,和@RequestMapping配合一起,定义URL请求和Controller 方法之间的映视,这样Controller就能被外界访问到。

@Controller

public class StudentController {

private static final Logger log = LoggerFactory.getLogger(StudentController.class);

//自动装配,代表一种声明,也就是告诉spring,我要用你的bean了。

   @Autowired

//private代表只能在本类里面引用,StudentService代表引用数据类型,studentService是属性

  private StudentService studentService;
  

   //    查询所有学生

//查询方法用GET,@Re定义URL和Controller方法之间的映视

   @RequestMapping(value = "/student/list", method = RequestMethod.GET)

//返回的数据类型是String。传的参数,形参,是Model model。

public String list(Model model) {

// log.info("/student/list GET ");

//创建了一个新的studentList,调用studentService里面的list()方法

       List<Student> studentList = studentService.list();
       //从数据库查询,显示到页面,需要调用model接口里面的addAttribute方法。
       model.addAttribute("studentList", studentList);

      // log.info("get student size is {}", studentList.size());

//返回值是list

       return "list";
   }

//添加学员

//和@Controller配合,定义URL和Controller方法的映视。增加用的POST,这就是restful

   @RequestMapping(value = "/student",method = RequestMethod.POST)

//返回值类型是String,传的参数是Student student

public String addStudent(Student student){

//调用insert方法,为什么查询list,需要一个List<Student> studentService,而这个不需要呢?还是涉及到一个返回值的问题,增加,返回值是int,影响了数据库一行。查询,新建一个studentList,把查询到的数据,放到studentList里面。

studentService.insert(student);
       log.info("insert student information is {}",student);
//        ModelAndView mav = new ModelAndView("redirect:/student");
       return "redirect:/student/list";
   }

//添加学员跳转页

//

   @RequestMapping(value = "/student/a",method = RequestMethod.GET)
public String toAddStudent() {
log.info("open /student/a GET");
       return "add";
   }
//删除学员

   @RequestMapping(value = "/student/{id}",method = RequestMethod.DELETE)

//为什么会需要@路径变量呢?因为点击删除按钮,会有一个id,根据id删除数据嘛。这个id就是int id的id。

这个id往上传,变成了/student/{id}里面的id。这个id往下传,就是调用删除方法,根据id删除。

public String delStudent(@PathVariable(“id”) int id){
log.info("/student/{id} DELETE id is {}",id);
       studentService.deleteByPrimaryKey( id);
       log.info("delete {} success",id);
       return "redirect:/student/list";
   }
// 跳转到修改学员界面

   @RequestMapping(value = "/student/u/{id}", method = RequestMethod.GET)

//还是有@PathVariable,就是可以和{id}绑定,一块入参。

//@PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过

    //@PathVariable("xxx") 绑定到操作方法的入参中。

//返回参数类型,跳转到修改的页面,也就是修改之前的列表,其实还是查询,需要创建一个对象,把查询好的数据放进去,这个对象就是student。为什么小括号里有int id之外,还有Model和 model呢?


public String toUpdateStudent(@PathVariable int id,Model model){
       Student student = studentService.selectByPrimaryKey(id);
       model.addAttribute("student",student);
       return "update";
   }
//学员修改

   @RequestMapping(value = "/student/{id}",method = RequestMethod.PUT)

//也是根据id修改,参数和{id}绑定。

public String UpdateStudent(@PathVariable int id, String name,String qq,String major) {
       System.out.println("这是一个啥呀哈哈哈" + id + name + qq +major);
       Student student = new Student();
       log.info("/student/{id} the request parameters are id:{},name:{},"+"qq:{},major:{}",id,name,qq,major);
       System.out.println(major);
       student.setId(id);
       student.setQq(qq);
       student.setName(name);
       student.setMajor(major);
        studentService.updateByPrimaryKey(student);
       log.info("update student success is {}");
       return "redirect:/student/list";
   }


有了这个扫描之后,会自动扫描service包下方,把标注有@service的类,自动注册到spring配置文件bean里面。

<context:annotation-config />

<context:component-scan base-package="com.alibaba.service" />
数据源                                                                                                            驱动程序管理器数据源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName">
       <value>com.mysql.jdbc.Driver</value>
   </property>
   <property name="url">
       <value>jdbc:mysql://localhost:3306/mysql</value>
 </property>
   <property name="username">
       <value>root</value>
   </property>
   <property name="password">
       <value>1234</value>
   </property>
</bean>


model和数据源以及student.xml文件一起,组装成一个sqlSession,抛到程序里运行。


<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">

   <property name="typeAliasesPackage" value="com.alibaba.model" />

 //   jdbc的数据源

<property name="dataSource" ref="dataSource"/>

//  类路径,location 位置

   <property name="mapperLocations" value="classpath:mapper/Student.xml"/>
</bean>


                                                                      //接口扫描配置

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="basePackage" value="com.alibaba.mapper"/>
</bean>


明天的计划:明天把任务二web.xml的配置文件解释出来,list.jsp;add.jsp;等文件也要解释;学会json。

遇到的问题:暂无

今天的收获:明白了不少控制器的运行过程,尤其是从页面增删改查数据的过程。

java任务二开始时间:2018.01.25

预计demo时间:2018.02.12

可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html 



返回列表 返回列表
评论

    分享到