Spring注解

  之前一直使用的是一对一的单注解方式,今天考到了一对多..果真是什么不会考什么,结果就是什么考试没做出来,数据也没取到。。。。

mysql> select * from clazz;
+----+-------+
| id | title |
+----+-------+
| 1 | 一班 |
| 2 | 二班 |
+----+-------+
2 rows in set (0.00 sec)

mysql> select * from student;
+----+-------+-------+----------+
| id | name | phone | clazz_id |
+----+-------+-------+----------+
| 1 | pop | 24556 | 1 |
| 2 | bob | 24556 | 1 |
| 3 | alice | 24556 | 2 |
+----+-------+-------+----------+
3 rows in set (0.00 sec)

 这是我两张简单表的数据,模型如下:

package com.yangpeng.hr.model;

public class Clazz {
    int mid;
    String mtitle;

    public int getMid() {
        return mid;
    }

    public void setMid(int mid) {
        this.mid = mid;
    }

    public String getMtitle() {
        return mtitle;
    }

    public void setMtitle(String mtitle) {
        this.mtitle = mtitle;
    }

    

}
package com.yangpeng.hr.model;

public class Student {

    int vid;
    String vname;
    String vphone;
    Clazz vclazz;

    public int getVid() {
        return vid;
    }

    public void setVid(int vid) {
        this.vid = vid;
    }

    public String getVname() {
        return vname;
    }

    public void setVname(String vname) {
        this.vname = vname;
    }

    public String getVphone() {
        return vphone;
    }

    public void setVphone(String vphone) {
        this.vphone = vphone;
    }

    public Clazz getVclazz() {
        return vclazz;
    }

    public void setVclazz(Clazz vclazz) {
        this.vclazz = vclazz;
    }

}

 平常我都是保证表的数据和类的字段名字一致,以减少不必要的麻烦,有些坑毕竟还是要尝试一下的,如果表的字段和建立类的字段信息不一,可以在sql语句起

别名,另一种就是使用@Result指定property,你现在可以看到Student类里面放了Clazz字段,明显和student表的字段不一样,这里就要使用@one,来实现一对一的操作。

package com.yangpeng.hr.mapper;

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.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.yangpeng.hr.model.Clazz;
import com.yangpeng.hr.model.Student;

@Mapper
public interface StudentMapper {

    @Select("select * from student")
    @Results(value = { 
            @Result(property = "vid", column = "id"), 
            @Result(property = "vname", column = "name"),
            @Result(property = "vphone", column = "phone"),
            @Result(property = "vclazz", column = "clazz_id", 
            javaType = Clazz.class, one = @One(select = "loadclazzByid")) })
    public List<Student> getallStu();

    @Select("select * from clazz where id = #{clazz_id}")
    @Results(value = {
            @Result(property="mid",column="id"),
            @Result(property="mtitle",column="title")
    })
    public Clazz loadclazzByid(int clazz_id);

}

   @one里面有一个属性select,单引号包含的是一个函数名,如果在不同类或者不同包,要使用方法的全权定名,我的loadclazzByid(int clazz_id)就是下面,只要使用方法名就可以啦

注意cloum = clazz_id  这一句,它会把表中的这个字段自动赋给loadclazzByid方法中的clazz_id,调用loadclazzByid方法,实现clazz对象的返回。最后我的页面终于成功取到数据。

再写一点spring处理url的参数:

@PathVaribale 获取url中的参数

@RequestParam 获取请求的参数 :/url?id=1&name=hello    public viod chuli(@RquestParam("id") int id,@RequestParam("name") String name);

例如前台向后台发送对象,表单处理时如下:

<form action="method" method="post">
    <input name='id' value='张三'>
    <input name='name' value='张三'>
    <input name='sex' value=''>
    <input name='age' value='34'>
    <input name='number' value='14512341345'>
    <button type="submit">提交</button>

 后台处理:

@Controller
public class SpringTest {
    @RequestMapping(value="/method",method=RequestMethod.POST)
    public ModelAndView method(UserInfo userInfo){
        System.out.println(userInfo.getName());
        System.out.println(userInfo.getSex());
        ModelAndView view = new ModelAndView("success");
        view.addObject("time",new Date());
        return view;
    }

后台web.xml配置字符过滤器,

https://blog.csdn.net/qq_39470733/article/details/77709607

好好生活,天天向上
原文地址:https://www.cnblogs.com/linchongatfirst/p/9534278.html