预算编报管理系统 Day4 Spring Hibernate

本文介绍如何在MyEclipse环境中配置Spring与Hibernate框架,并通过具体示例演示如何使用这两个框架进行对象关系映射及数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2018/7/5

一. Spring

配置:

myeclipse 2017:

右击项目->configure facets->install Spring facets

安装好spring

配置 新建class com.test.User:

package com.test.bean;
public class Person {
    
    private String name;
    private int age;
    private String fatherName;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setfatherName(int haha){
    	fatherName = name +" "+haha;
    }
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void info(){
        System.out.println("一起来吃麻辣烫!");
        System.out.println("name:"+getName()+" age:"+getAge());
        System.out.println("Father : "+fatherName);
    }
}

注解:Person 属性可以为private 对每一个属性都有get和set方法 其他的不做要求


在com.test.bean 里新建bean.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="gn" class="com.test.bean.Person">
        <property name="name" value="xingoo"/>
        <property name="age" value="12"/>
        <property name="fatherName" value="2"/>
    </bean>
</beans>

注解: bean.xml 文件前5行不用管,默认即可

以下 id标签用作下面获取对象,里面的class=要注入的类名

property是类中的各个属性,name为属性名,value为属性值


新建package testSpring

在里面新建class Test:

package testSpring;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.bean.Person;

public class Test {
    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("com/test/bean/bean.xml");//读取bean.xml中的内容
        Person p = ctx.getBean("gn",Person.class);//创建bean的引用对象
        p.setAge(1000);
        p.info();
        
    }
}
注解 : 
 new ClassPathXmlApplicationContext("com/test/bean/bean.xml");

方法获得用于获取派人送对象的context , 其中输入参数为配置文件bean的路径

接下来通过

ctx.getBean("gn",Person.class)

获取bean.mxl注入的对象

另一种方法是(User)ctx.getBean("gn")

这两种方法的gn都是bean.xml中的id的值,当id改变时,getBean的参数也要改变

这样,就可以通过Spring 的getBean获取到在xml中注入的对象


二.hibernate

配置:

跟spring配置方法一样 :右击项目->configure facets->install Hibernate facets

但是在配置之前需要进行数据库的配置,这里用到mysql



点击database browser

出现左边视窗 DB Browser


新建一个连接,这里的mysql配置就不讲了,连接建立也很简单不讲了

在建立连接时会用到一个connector jar 包,可以从这里下载 :connector.jar

现在返回项目视窗(点击右上角me图标如下图)

执行右击项目->configure facets->install Hibernate facets


点击next


出现以上错误,将create session class 取消勾选

点击next


选择DB driver 然后connect url driver class username 等信息会自动填充,只需要输入password即可,点击finishing即可

在数据中创建一个表User(name char(5), password char(10))

在src中新建包 com.entity

在com.entity 包中新建class User

package com.entity;

public class User {
	private String name;
	private String password;
	
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name =name; 
	}
	public String getPassword(){
		return password;
	}
	public void setPassword(String password){
		this.password = password;
	}

}

在 com.entity 中新建User.hdm.xml 

<?xml version="1.0"?>    
<!DOCTYPE hibernate-mapping PUBLIC     
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
<hibernate-mapping>    
    <class name="com.entity.User" table="user">    
         
        <id name="name" /> 
        
        <property name="password"/>    
    </class>    
</hibernate-mapping>  

注解: class name 应为我们要映射的class类 table应为数据库中的对用表名

id为主键 对用的属性 name为属性名,其中有时会有generator 标签标示id生成方式,一般不用写

 property 为其他属性名,如果里面包含colum标签,对应的name为数据库表中对应的属性名,不写标示类中的属性名与数据库表中的属性名一样


在src文件夹下的hibernate.cfg.xml 文件配置:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="myeclipse.connection.profile">myTest</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.password">5573</property>
        <property name="connection.username">roc</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC</property>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    	<mapping resource="com/entity/User.hdm.xml"/>
    </session-factory>

</hibernate-configuration>

着重检查标红的两行代码,

第一行中"&"符号可能会报错,此时将"&"改为"&amp;",

第二行可能会没有,若要将User类映射到数据库的user表,需要加上该行代码,否则会报错

在src中新建test包

在test包中新建class Test

class Test{
public static void main(String[] args) {     
        //读取hibernate.cfg.xml文件    
        Configuration cfg = new Configuration().configure();    
            
        //建立SessionFactory    
        SessionFactory factory = cfg.buildSessionFactory();   
        
        Session sess = factory.openSession();
        try{
        User user = new User();
        user.setName("l");
        user.setPassword("2121");
        
        sess.beginTransaction();
        sess.save(user);
        sess.getTransaction().commit();
        }catch(Exception e){}
     
        
        String sql = "delete from user where name='l'";
        sql = "select *from user where name='l'";
        Query q =  sess.createSQLQuery(sql);
//        q.executeUpdate();
        q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        List<HashMap> list = q.list();
        for (Map u:list){
        	System.out.println(u.get("name")+"\t"+u.get("password"));
        }
        
       
    } 
}
这样就可以利用sess进行数据库操作






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值