sql 语句
--创建用户及授权 CREATE USER stuDB IDENTIFIED BY 123456; GRANT CONNECT,RESOURCE to stuDB; --drop user stuDB cascade --班级信息表 create table classes( id number(10) primary key not null,--班级编号 cname varchar2(30) not null--班级名称 ) --学生信息表 create table student( id number(10) primary key not null,--学员编号 sname varchar2(30) not null,--学员姓名 birthday date not null,--生日 gender varchar2(10) not null,--性别 telephone varchar2(20) not null,--电话 email varchar2(50) not null,--Email classid number(10) not null,--班级编号 foreign key(classid) references classes(id) ) --创建序列 CREATE SEQUENCE seq_student INCREMENT BY 1 START WITH 1 CACHE 10; CREATE SEQUENCE seq_classes INCREMENT BY 1 START WITH 1 CACHE 10; --插入班级信息表 insert into classes (id, cname) values (seq_classes.nextval, 'Y2E315'); insert into classes (id, cname) values (seq_classes.nextval, 'S2E234'); insert into classes (id, cname) values (seq_classes.nextval, 'S1E271'); insert into classes (id, cname) values (seq_classes.nextval, 'Y2E123'); insert into classes (id, cname) values (seq_classes.nextval, 'Y2E695'); --插入学生信息表 insert into student (id, sname, birthday, gender, telephone, email, classid) values (seq_student.nextval, '景临境', to_date('1991-10-18','yyyy-mm-dd'), '男', '15123456789', 'chaoyi@qq.com', 2); insert into student (id, sname, birthday, gender, telephone, email, classid) values (seq_student.nextval, '何开', to_date('1992-5-4','yyyy-mm-dd'), '男', '15123456789', 'chaoyi@qq.com', 3); insert into student (id, sname, birthday, gender, telephone, email, classid) values (seq_student.nextval, '赵柔', to_date('1989-5-4','yyyy-mm-dd'), '女', '15123456789', 'chaoyi@qq.com', 4); insert into student (id, sname, birthday, gender, telephone, email, classid) values (seq_student.nextval, '左阻', to_date('1989-5-4','yyyy-mm-dd'), '男', '15123456789', 'chaoyi@qq.com', 5); insert into student (id, sname, birthday, gender, telephone, email, classid) values (seq_student.nextval, '陆梦', to_date('1996-5-4','yyyy-mm-dd'), '女', '15123456789', 'chaoyi@qq.com', 2); --查看 select id, cname from classes; select id, sname, birthday, gender, telephone, email, classid from student
Student 实体类及注解
package cn.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.SequenceGenerator; import javax.persistence.Table; /** * 学生 实体类及注解 * */ @Entity @Table public class Student { @Id @GeneratedValue(generator="seq_student",strategy=GenerationType.SEQUENCE) @SequenceGenerator(name="seq_student",sequenceName="seq_student",allocationSize=1,initialValue=1) private Integer id;//学员编号 @Column(nullable=false) private String sname;//学员姓名 @Column(nullable=false) private Date birthday;//生日 @Column(nullable=false) private String gender;//性别 @Column(nullable=false) private String telephone;//电话 @Column(nullable=false) private String email;//Email @ManyToOne(targetEntity=Classes.class) @JoinColumn(name="classid") private Classes classes;//班级编号,多对一,多个学生对一个班级 public Student() { } public Student(String sname, Date birthday, String gender, String telephone, String email, Classes classes) { this.sname = sname; this.birthday = birthday; this.gender = gender; this.telephone = telephone; this.email = email; this.classes = classes; } public Student(Integer id, String sname, Date birthday, String gender, String telephone, String email, Classes classes) { this.id = id; this.sname = sname; this.birthday = birthday; this.gender = gender; this.telephone = telephone; this.email = email; this.classes = classes; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Classes getClasses() { return classes; } public void setClasses(Classes classes) { this.classes = classes; } }
Classes 实体类及注解
package cn.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; import javax.persistence.Table; /** * 班级 实体类及注解 * */ @Entity @Table public class Classes { @Id @GeneratedValue(generator="seq_classes",strategy=GenerationType.SEQUENCE) @SequenceGenerator(name="seq_classes",sequenceName="seq_classes",allocationSize=1,initialValue=1) private Integer id;//班级编号 @Column(nullable=false) private String cname;//班级名称 public Classes() { } public Classes(String cname) { this.cname = cname; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
StudentDao 数据访问层接口与实现
package cn.dao; import java.util.List; import org.hibernate.HibernateException; import cn.entity.Student; /** * 学生 数据访问层接口 * */ public interface StudentDao { /** * 获取全部的学生列表,包括班级 * @return * @throws HibernateException */ public List<Student> getAll() throws HibernateException; /** * 根据 id 查找学生信息 * @param id * @return * @throws HibernateException */ public Student getStudentById(Integer id) throws HibernateException; /** * 修改学生信息 * @param student * @return * @throws HibernateException */ public int update(Student student) throws HibernateException; }
package cn.dao.impl; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import cn.dao.StudentDao; import cn.entity.Student; import cn.util.HibernateSessionFactory; /** * 学生 数据访问层实现 * */ public class StudentDaoImpl implements StudentDao { /** * 获取全部的学生列表,包括班级 * @return * @throws HibernateException */ @SuppressWarnings("unchecked") public List<Student> getAll() throws HibernateException { String hql="from Student"; List<Student> students=null; Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); students=query.list(); HibernateSessionFactory.closeSession(); return students; } /** * 根据 id 查找学生信息 * @param id * @return * @throws HibernateException */ public Student getStudentById(Integer id) throws HibernateException { String hql="from Student where id =:id"; Student student=null; Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); query.setInteger("id", id); student = (Student) query.uniqueResult(); HibernateSessionFactory.closeSession(); return student; } /** * 修改学生信息 * @param student * @return * @throws HibernateException */ public int update(Student student) throws HibernateException { Session session = HibernateSessionFactory.getSession(); Transaction tx = null; int id =0; try { tx = session.beginTransaction(); session.update(student); tx.commit(); id =1; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateSessionFactory.closeSession(); } return id; } }
ClassesDao 数据访问层接口与实现
package cn.dao; import java.util.List; import org.hibernate.HibernateException; import cn.entity.Classes; /** * 班级 数据访问层接口 * */ public interface ClassesDao { /** * 获取班级的列表 * @return * @throws HibernateException */ public List<Classes> getClassesList() throws HibernateException; /** * 根据 id 获取班级 * @param id * @return * @throws HibernateException */ public Classes getClassesById(Integer id) throws HibernateException; }
package cn.dao.impl; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import cn.dao.ClassesDao; import cn.entity.Classes; import cn.util.HibernateSessionFactory; /** * 班级 数据访问层实现 * */ public class ClassesDaoImpl implements ClassesDao { /** * 获取班级的列表 * @return * @throws HibernateException */ @SuppressWarnings("unchecked") public List<Classes> getClassesList() throws HibernateException { String hql="from Classes"; List<Classes> classes=null; Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); classes=query.list(); HibernateSessionFactory.closeSession(); return classes; } /** * 根据 id 获取班级 * @param id * @return * @throws HibernateException */ public Classes getClassesById(Integer id) throws HibernateException { String hql="from Classes where id =:id"; Classes classes=null; Session session=HibernateSessionFactory.getSession(); Query query=session.createQuery(hql); query.setInteger("id", id); classes = (Classes) query.uniqueResult(); HibernateSessionFactory.closeSession(); return classes; } }
StudentBiz 业务逻辑层接口与实现
package cn.biz; import java.util.List; import org.hibernate.HibernateException; import cn.entity.Classes; import cn.entity.Student; /** * 学生 业务逻辑层接口 * */ public interface StudentBiz { /** * 获取全部的学生列表,包括班级 * @return * @throws HibernateException */ public List<Student> getAll() throws HibernateException; /** * 根据 id 查找学生信息 * @param id * @return * @throws HibernateException */ public Student getStudentById(Integer id) throws HibernateException; /** * 修改学生信息 * @param student * @return * @throws HibernateException */ public int update(Student student) throws HibernateException; /** * 获取班级的列表 * @return * @throws HibernateException */ public List<Classes> getClassesList() throws HibernateException; /** * 根据 id 获取班级 * @param id * @return * @throws HibernateException */ public Classes getClassesById(Integer id) throws HibernateException; }
package cn.biz.impl; import java.util.List; import org.hibernate.HibernateException; import cn.biz.StudentBiz; import cn.dao.ClassesDao; import cn.dao.StudentDao; import cn.dao.impl.ClassesDaoImpl; import cn.dao.impl.StudentDaoImpl; import cn.entity.Classes; import cn.entity.Student; /** * 学生 业务逻辑层实现 * */ public class StudentBizImpl implements StudentBiz { //学生 数据访问层 private StudentDao studentDao = new StudentDaoImpl(); //班级 数据访问层 private ClassesDao classesDao = new ClassesDaoImpl(); /** * 获取全部的学生列表,包括班级 * @return * @throws HibernateException */ public List<Student> getAll() throws HibernateException { return studentDao.getAll(); } /** * 根据 id 查找学生信息 * @param id * @return * @throws HibernateException */ public Student getStudentById(Integer id) throws HibernateException { return studentDao.getStudentById(id); } /** * 修改学生信息 * @param student * @return * @throws HibernateException */ public int update(Student student) throws HibernateException { return studentDao.update(student); } /** * 获取班级的列表 * @return * @throws HibernateException */ public List<Classes> getClassesList() throws HibernateException { return classesDao.getClassesList(); } /** * 根据 id 获取班级 * @param id * @return * @throws HibernateException */ public Classes getClassesById(Integer id) throws HibernateException { return classesDao.getClassesById(id); } }
相关推荐
ASP.NET、WCF(Windows Communication Foundation)和Entity Framework是微软.NET框架下的三个核心技术,用于构建分布式企业级应用程序。在本文中,我们将深入探讨这三个组件以及它们如何协同工作。 **ASP.NET** ...
return jdbcTemplate.update(sql, new Object[]{entity.getColumn1(), entity.getColumn2()}); } ``` 5. **事务管理**:由于数据库操作往往涉及到事务,因此需要在Service层进行事务控制。Spring提供声明式事务...
最后,`Entity Framework`(EF)是微软提供的一款ORM(Object-Relational Mapping)工具,它使得开发者可以使用.NET语言直接操作数据库,而无需编写大量的SQL语句。EF通过映射对象到数据库表,减少了数据访问层的...
这个主题"springboot+mybatis逆向生成controller+service+mapper+entity"是关于如何利用工具自动化创建CRUD(Create、Read、Update、Delete)操作的相关代码,包括Controller、Service、Mapper以及Entity类。...
微信+MVC+EntityFramework Demo
修改webconfig的sql密碼後 遠行項目 隨便輸入什麼點登錄 會自動生成數據庫 生成數據庫之後執行下面的語句 添加種子數據 --數據庫種子數據 use EXTMVC insert [dbo].[users] values('tomoto','tomoto',1,'2014-05-06 ...
WCF+Silverlight+EntityFramework+Sqlite所做的学生信息管理系统,自己闲暇时间做着玩的,页面什么的比较丑,不过不要在意那些细节……数据库采用Sqlite,非常小的一个数据库,我就不提供了,网上一大堆,表结构什么...
我的第一个ASP.NET MVC(WebApi) + Entity FrameWork+ Unity+Redis+Sql Server-Other架构的web开发架构设计_EFMvcFrameApp
它允许开发者使用面向对象的方式来操作数据库,无需关注底层SQL语句,大大简化了数据访问层的开发工作。EF的核心概念包括实体(Entity)、上下文(DbContext)、数据模型(Data Model)和映射(Mapping)。 在CMS...
NHinbernate+LINQ+Entity Framework完成的课程设计平台,分为website和project两个部分,project主要是nhibernate,linq和ef在website的app_Code中。 数据库使用sql 2008.
Entity Framework (EF)是.NET框架下的一个ORM工具,它允许开发者使用面向对象的方式来操作数据库,而无需关注底层SQL语句。EF Core是其轻量级、跨平台的版本,与ASP.NET Core完美配合。在这个学习过程中,你将学习到...
NHinbernate+LINQ+Entity Framework完成的课程设计平台,分为website和project两个部分,project主要是nhibernate,linq和ef在website的app_Code中。 数据库使用sql 2008 该部分是nhibernate
ASP.NET MVC+EntityFramework项目实例,功能 页面登录,分页,用户添加,编辑以及关键词查询等
自己搭建的 .Net系列框架-Dapper+EntityFrameworkCore+Autofac+WebApi+Web 详细介绍见:https://blog.csdn.net/zhangjiankun880/article/details/106540475
**标题:“WPF+EntityFramework6Demo”** 这个标题表明我们正在探讨一个使用Windows Presentation Foundation (WPF) 和 Entity Framework 6 (EF6) 的演示项目。WPF 是微软提供的一个用于构建桌面应用程序的框架,它...
WPF+Entity Frame的Database first模式,使用用户指定的连接字符串的方法,我也曾在网上找了很久,基本没看到有明确说明的,也没有想过示例。 终于结合相关人的描述让我找到了修改的地方。压缩包内为原版工程代码,...
而Entity Framework(EF)则是一种对象关系映射(ORM)工具,它允许开发者使用面向对象的编程方式来操作数据库,减少了对SQL语句的直接依赖。 Ado.Net主要由四个关键组件构成:Connection(连接)、Command(命令)...
**MVC3与Entity Framework简介** MVC3(Model-View-Controller)是Microsoft推出的一种用于构建可维护性和测试性的Web应用程序的框架。它将应用程序的业务逻辑、数据访问和用户界面分离,使得开发人员可以更高效地...
编程实体框架Code First是一种基于微软Entity Framework(简称EF)架构的开发方法。Entity Framework是.NET框架中用于操作数据源的ORM(对象关系映射)框架。Code First则是Entity Framework提供的一种使用方法,它...
### LINQ to SQL与Entity Framework对比分析 #### 一、概述 随着软件开发技术的不断发展,数据访问技术也在不断地更新换代。LINQ to SQL 和 Entity Framework 作为两种主流的对象关系映射(Object-Relational ...