SpringMVC:简单易用,上手快,项目配置部分很少,注重代码开发层面
Hibernate:ORM(对象关系映射,英语:Object Relation Mapping,简称ORM,或O/RM,或O/R mapping)框架,轻量级的数据库封装
下面直接上项目:
如图,缺少web.xml文件,如下图示,加入:
下载地址:SpringMVC下载
SpringMVC_Hibernate
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
springMVC
/
5.1 写一个controller类
package com.fzx.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class HelloController {
@RequestMapping(value = "/hello")
public ModelAndView hello() {
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
}
5.2 写一个jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
hello!!!
5.3测试下,测试成功
6.1 加入hibernate的jar包 + mysql的jar包,我使用的是:hibernate-release-4.2.21.Final
下载地址:hibernate_4.2.21的jar包 MySQL的jar包
6.2 创建表
CREATE TABLE `people` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
6.3 创建hibernate.cfg.xml文件,该文件名称是系统默认获取的hibernate配置文件
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
root
root
true
true
org.hibernate.dialect.MySQLDialect
6.4 创建以实体类,后面会与数据库的表字段映射,实现hibernate对数据库表的封装
package com.fzx.Domain;
public class People {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
6.5 创建People.hbm.xml文件,写入表与People实体类的映射关系
6.6 写入测试,查看数据库,测试成功
package com.fzx.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.jupiter.api.Test;
import com.fzx.Domain.People;
public class JunitTest {
@Test
public void save() {
// 自动加载hibernate.cfg.xml内容,返回Configuration对象
Configuration configuration = new Configuration().configure();
// 创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
// 创建会话工厂对象
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// 创建会话对象
Session session = sessionFactory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction();
// 准备数据
People people = new People();
people.setName("哇咔咔");
people.setAge(10);
// 插入数据
session.save(people);
// 准备数据
people = new People();
people.setName("哇咔咔");
people.setAge(20);
// 插入数据
session.save(people);
// 提交事务
transaction.commit();
// 关闭会话
session.close();
sessionFactory.close();
}
}
最喜欢看到这个东西了哦,其他的:查询、更新、删除,可以自己摸索下,看看咋写咯~
提示:需要加入junit包
7.1 先写一个会话工厂对象获取的公共类
package com.fzx.Util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class SessionUtil {
private static Configuration configuration;
private static ServiceRegistry serviceRegistry;
static {
// 自动加载hibernate.cfg.xml内容,返回Configuration对象
configuration = new Configuration().configure();
// 创建服务注册对象
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
}
public static Session getSession() {
return configuration.buildSessionFactory(serviceRegistry).openSession();
}
public static SessionFactory getSessionFactory() {
return configuration.buildSessionFactory(serviceRegistry);
}
}
7.2 在HelloController类里面再写入一个方法,获取数据库中所有数据值
@RequestMapping(value = "/listAll")
public ModelAndView listAll() {
ModelAndView mv = new ModelAndView();
Session session = SessionUtil.getSession();
// createQuery里面写入HSQL,其中People是表名,里面的字段都得写People里面对应的属性
Query query = session.createQuery("from People");
mv.addObject("list", query.list());
mv.setViewName("listAll");
return mv;
}
7.3 再写一个listAll.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Insert title here
编号
姓名
年龄
${item.id }
${item.name }
${item.age }
7.4 用Tomcat启动项目,浏览器地址栏输入:http://localhost:8080/SpringMVC_Hibernate/listAll
OK,大功告成,over......
最后附送完整项目一只
下载地址:SpringMVC_Hibernate项目下载