NutzCN Logo
问答 这个怎么才能给Student表中添加数据并且给学生和课程表的中间表添加数据
发布于 2683天前 作者 wx_0quujkao0it5ekj89bt6 2813 次浏览 复制 上一个帖子 下一个帖子
标签:
student实体类
@Table("student")
public class Student {

    @ManyMany(target = Lesson.class,relation = "less_stu",from="stuId",to="lessId")
    private List<Lesson> lesson;


    @Id
    private int id;
    @Column
    @ColDefine(width = 20)
    @Comment("名字")
    private String stuname;

  Lesson实体类
@Table("lesson")

public class Lesson {


    @ManyMany(target = Student.class,relation = "less_stu",from="lessId",to="stuId")
    private List<Student> students;



    @Id
    private int id;
    @Column
    @ColDefine(width = 20)
    @Comment("名字")
    private String lesname;

17 回复

插入操作 如果你已经实现准备好了这样的对象: Food food = new Food("Fish"); List<Pet> pets = new ArrayList<Pet>(); pets.add(new Pet("XiaoBai"); pets.add(new Pet("XiaoHei"); food.setPets(pets); 这个Food food=new Food("Fish")中的fish是啥?我在上面的实体类中找不到 求教

构造方法参数,无视。

关注里面dao语句。

多对多的关系,一般的先全删,然后插入

否则得逐条查询然后判断,更慢

啊!先全删了?我现在就是做一个功能 有课程表 学生表 两张表的中间表 然后多对多就是上面的实体类 然后就是添加学生的同时 为这个学生添加课程 就需要在中间表里面添加数据了

有人喜欢把中间表也做个pojo,控制起来也很方便

我说的全删是删除特定many端的记录,然后重新插入

呃 您说的这个 我还理解不了

算了,你把中间表也做个pojo,爱咋搞就咋搞

好的好的 谢谢您的指导

dao.insert(student);
dao.insertRelation(student, "lessons");

谢谢 感谢您的指导

```
添加方法
@At("addStudent")
@Ok(">>:queryArrStu")
@Aop(TransAop.READ_COMMITTED)
public boolean AddStudent(HttpServletRequest request){
String name = request.getParameter("name");
int lessId = Integer.parseInt(request.getParameter("lessId"));
Student student=new Student();
List lessons=new ArrayList();
Lesson lesson = new Lesson();
lesson.setId(lessId);
lessons.add(lesson);
student.setLesson(lessons);
student.setStuname(name);
Student insert = dao.insert(student);
Student student1 = dao.insertRelation(student, "lesson");
if(insert==null&&student1==null){
return false;
}
return true;
}

服务器结果
2017-07-12 15:22:16[INFO ]org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/][203]-15:22:14.346 DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [GET] path=/ : LoginController.index(LoginController.java:36)
15:22:14.374 DEBUG org.nutz.ioc.impl.NutIoc - Get 'loginController'<class team.chance.simple.module.web.controller.LoginController>
15:22:14.374 DEBUG org.nutz.ioc.impl.NutIoc - >> Load definition name=loginController
15:22:14.376 DEBUG org.nutz.ioc.loader.combo.ComboIocLoader - Found IocObject(loginController) in AnnotationIocLoader(packages=[team.chance.simple.module])
15:22:14.377 DEBUG org.nutz.ioc.impl.NutIoc - >> Make...'loginController'<class team.chance.simple.module.web.controller.LoginController>
15:22:14.378 DEBUG org.nutz.ioc.aop.impl.DefaultMirrorFactory - Load class team.chance.simple.module.web.controller.LoginController without AOP
15:22:14.379 DEBUG org.nutz.ioc.impl.ScopeContext - Save object 'loginController' to [app]
15:22:14.411 DEBUG org.nutz.ioc.impl.NutIoc - Get 'dao'<interface org.nutz.dao.Dao>

2017-07-12 15:22:20[INFO ]org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/][203]-15:22:19.223 DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [GET] path=/queryTeacher : ShowTeacher.QueryTeacher(ShowTeacher.java:42)
15:22:19.224 DEBUG org.nutz.ioc.impl.NutIoc - Get 'showTeacher'<class team.chance.simple.module.web.controller.ShowTeacher>
15:22:19.224 DEBUG org.nutz.ioc.impl.NutIoc - >> Load definition name=showTeacher
15:22:19.224 DEBUG org.nutz.ioc.loader.combo.ComboIocLoader - Found IocObject(showTeacher) in AnnotationIocLoader(packages=[team.chance.simple.module])
15:22:19.224 DEBUG org.nutz.ioc.impl.NutIoc - >> Make...'showTeacher'<class team.chance.simple.module.web.controller.ShowTeacher>
15:22:19.227 DEBUG org.nutz.ioc.impl.NutIoc - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
15:22:19.228 DEBUG org.nutz.ioc.impl.NutIoc - >> Load definition name=txREAD_COMMITTED
15:22:19.228 DEBUG org.nutz.ioc.loader.map.MapLoader - Loading define for name=txREAD_COMMITTED
15:22:19.239 DEBUG org.nutz.ioc.loader.combo.ComboIocLoader - Found IocObject(txREAD_COMMITTED) in TransIocLoader@1506363121
15:22:19.239 DEBUG org.nutz.ioc.impl.NutIoc - >> Make...'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
15:22:19.239 DEBUG org.nutz.ioc.impl.ScopeContext - Save object 'txREAD_COMMITTED' to [app]
15:22:19.263 DEBUG org.nutz.ioc.impl.NutIoc - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
15:22:19.265 DEBUG org.nutz.ioc.impl.NutIoc - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
15:22:19.272 DEBUG org.nutz.aop.asm.AsmClassAgent - AsmClassAgent will define class in Version 50
15:22:19.306 DEBUG org.nutz.ioc.impl.ScopeContext - Save object 'showTeacher' to [app]
15:22:19.350 DEBUG org.nutz.ioc.impl.NutIoc - Get 'dao'<interface org.nutz.dao.Dao>
15:22:19.580 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT t.id,t.tchname,l.lesname FROM teacher t INNER JOIN lesson l ON t.lessId=l.id

2017-07-12 15:22:22[INFO ]org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/][203]-15:22:22.039 DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [GET] path=/toaddstudent : ShowTeacher.toAddStudent(ShowTeacher.java:117)
15:22:22.040 DEBUG org.nutz.ioc.impl.NutIoc - Get 'showTeacher'<class team.chance.simple.module.web.controller.ShowTeacher>
15:22:22.065 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT * FROM lesson

2017-07-12 15:22:29[INFO ]org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/][203]-15:22:29.798 DEBUG org.nutz.mvc.impl.UrlMappingImpl - Search mapping for [GET] path=/addstruts : NOT Action match
前端报错
HTTP Status 404 - /addstruts

type Status report

message /addstruts

description The requested resource is not available.
Apache Tomcat/8.0.38

抱歉哈 前辈 我这个路径写错了 不好意思哈 谢谢您的指导

我看了一下路径 然后我才看见 是路径写错了 实在抱歉哈

已经ok了 感谢 感谢 您的指导

添加回复
请先登陆
回到顶部