发表于: 2016-09-09 23:46:55

5 2288


今天完成的事情:上午学习了关于mysql数据库语句的多表操作,还是很容易理解的。有关项目这种一对多,多对多的情况怎么解决呢。

  • 一对多与多对一

    一个班级对应多个学生(一对多),多个学生对应一个班级(多对一)。

    create table class(

        class_id int primary key, grade varchar(30) unique,    count int);

    create table students(

        stu_id int primary key,name varchar(30), age int, class_id int,  

      foreign key(class_id) references class (class_id));


    //查询一年级的学生信息

    select *from students,class where students.class_id = class.class_id and class.class_id = 1;

    //查询tom的信息

    select *from students,class where students.class_id = class.class_id and students.name = 'tom';

    //查询tom和mimi的年龄,上几年级,班上多少人

    select s.name,s.age,c.grade,c.count from students s,class c where s.class_id = c.class_id and 

    (s.name = 'tom' or s.name = 'mimi');

    //查询年龄8岁的学生信息

    select * from students s,class c where s.class_id = c.class_id and age=8;

    • 多对多
    • create table students( stu_id int primary key, name varchar(30), age int);
    • create table teachers(tea_id int primary key,name varchar(30),age int);
    • create table stu_tea(    id int primary key,stu_id int,tea_id int,    
    • foreign key (stu_id) references students(stu_id),    
    • foreign key (tea_id) references teachers(tea_id) );
    • //查询tom的老师及他们的信息
    • select * from students s,teachers t,stu_tea st where s.stu_id=st.stu_id and t.tea_id = st.tea_id and s.name = 'tom';
    • //查询tom的老师的信息(tom姓名,tom年龄,tom老师的name,tom老师的年龄)
    • select s.name as stu_name ,s.age as stu_age,t.name as tea_name,t.age as tea_age  from students s,teachers t,stu_tea st where s.stu_id=st.stu_id and t.tea_id = st.tea_id and s.name = 'tom';
    • //查询blue老师所教的学生
    • select * from teachers t,students s,stu_tea st where s.stu_id = st.stu_id and t.tea_id = st.tea_id and t.name = 'blue';
    • //查询blue老师所教的学生(blue姓名,blue年龄,blue的学生name,blue的学生年龄)
    • select t.name as tea_name,t.age as tea_age,s.name as stu_name,s.age as stu_age from students s,teachers t,stu_tea st where t.tea_id = st.tea_id and s.stu_id = st.stu_id and t.name = 'blue';
    以上方法虽然比较复杂,但是知道遇到这种情况可以去怎么解决。仅供参考。
  • 下午写了一个简单的自定义拦截器的demo

主要在springmvc的配置文件中加上下面的部分,配置一下

<!-- 拦截器配置 -->

    <mvc:interceptors>

        <!-- 多个拦截器,顺序执行 -->

        <mvc:interceptor>

            <!-- 需要拦截的url -->

            <mvc:mapping path="/u/*" />

            <!-- 拦截器实现类 -->

            <bean class="com.Interceptor.MyInterceptor" />

        </mvc:interceptor>

    </mvc:interceptors>

在你运行controller控制前之前,会先执行一下拦截器,其中上面加粗的部分是自己定义的拦截器.

需要继承HandlerInterceptorAdapter并重写其三个方法,preHandle,postHandle,afterCompletion方法.

preHandle在业务处理器处理请求之前被调用, postHandle在业务处理器处理请求执行完成后,生成视图之前执行,afterCompletion在DispatcherServlet完全处理完请求后被调用,可用于清理资源等.

控制器的内容:


话说回来,/u/。。的重点就在这里吧,在上面MyInterceptor拦截器的preHandle里进行判断,是不是有cookie,有的话,携带token在去数据库中去检查一下,确实是以前生成的cookie就放行直接显示页面,不通过转到登录注册页面去验证,他、去数据库中比对,验证通过了,生成cookie。不通过继续转到登录页面。我要在去验证一下我的想法。


晚上叶钦讲了一下spring+mybatis。感觉他学习能力很强,而且特别努力。你尽力了才有资格说自己运气不好,不要说你不行,是你对自己不够狠。







遇到的问题:

收获:



返回列表 返回列表
评论

    分享到