Spring+SpringMVC+Hibernate+Mysql简单Demo

本文通过IntelliJ IDEA创建一个Spring+SpringMVC+Hibernate的项目,介绍了项目配置、数据库表设计、XML配置、接口实现、控制器编写以及网页展示的全过程,最后展示了项目的运行结果。
摘要由CSDN通过智能技术生成

工具用的是Inteli ideaJ 2017.2

先放个完整目录



还有所有用到的jar包,idea自动下载的包就不放了,都在lib里



数据库表



新建项目的时候,记得把Spring,SpringMVC和Hibernate都勾上,进去后自带了这些xml文件


在src目录下建立包



StudentInterface代码如下

package com.limbo.service;

import com.limbo.entity.Student;

public interface StudentInterface {
    public void add(Student student);

}


StudentImpl代码如下

package com.limbo.service;

import com.limbo.entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class StudentImpl implements StudentInterface{
    @Qualifier("sessionFactory")
    @Autowired
    private SessionFactory sessionFactory;
    @Override
    public void add(Student student) {
        Session s = sessionFactory.openSession();
        Transaction tx= s.beginTransaction();
        s.save(student);
        tx.commit();
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

之后写xml文件

applicationContext代码(数据库用户名密码请自行修改,其中数据库url后面不跟时区属性的话我这里会报错,请注意)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- data connection -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"> <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值