小知识插曲:
在MyEclipse中如何将源码配置到相关的jar包:
Jar包---propreties------JavaSourceAttachment------External File ----fileof source.jar
1、 编写jdbc.properties配置jdbc的相关参数
[html]
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sms
jdbc.username=root
jdbc.password=root
2、编写Javabean和映射文件:
[java]
JavaBean:
package com.spring.model;
public class User {
private int id;
private String name;
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;
}
}
映射文件:
[html]
< xml version="1.0" >
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
3、 配置spring容器初始化sessionFactory
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
4、编写测试类:
[java]
package com.spring.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.UserService;
public class SpringTest {
@Test
public void test01() {
BeanFactory applicationContext = new ClassPathXmlApplicationContext(
"beans.xml");
UserService user = (UserService) applicationContext.getBean("userService");
user.save();
}
}
需要的hibernate Jar包:

需要的spring jar包:

作者:zhang6622056