备注:本人写博客就是想_让一些技术点和配置 遵循官方标准配置和更容易理解 为基础
以最精简例子入门,更容易的掌握一些东西.
整合结构图
1.添加spring支持配置spring的applicationContext.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:c="http://www.springframework.org/schema/c"
- xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
- <!-- 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 加载多个资源配置文件 -->
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:frame_jdbc.properties</value>
- </list>
- </property>
- </bean>
- <!-- 使用注解的方式装配置bean -->
- <context:annotation-config />
- <context:component-scan base-package="com.frame"></context:component-scan>
- <!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
- <mvc:annotation-driven />
- <!-- 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 配置dbcp数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
- </beans>
- <?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="format_sql">true</property>
- <property name="show_sql">true</property>
- <mapping resource="com/frame/student/bean/Student.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
//配置jdbc
- ################Oracle_JDBC################
- jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
- jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
- jdbc.username=oracle
- jdbc.password=123456
//配置basedao
- package com.frame.base.dao;
- import com.frame.student.bean.Student;
- public interface BaseDao {
- public void save(Student transientInstance);
- }
- package com.frame.base.dao;
- import javax.annotation.Resource;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.transaction.annotation.Transactional;
- import com.frame.student.bean.Student;
- @Transactional
- public class BaseDaoImpl implements BaseDao{
- private static final Logger log = LoggerFactory.getLogger(BaseDaoImpl.class);
- @Resource
- private SessionFactory sessionFactory;
- private Session getCurrentSession() {
- return sessionFactory.getCurrentSession();
- }
- @Override
- public void save(Student transientInstance) {
- log.debug("saving Student instance");
- try {
- getCurrentSession().save(transientInstance);
- log.debug("save successful");
- } catch (RuntimeException re) {
- re.printStackTrace();
- }
- }
- }
- package com.frame.student.bean;
- /**
- * Student entity. @author MyEclipse Persistence Tools
- */
- public class Student implements java.io.Serializable {
- // Fields
- private String stuid;
- private String stuname;
- private String stutime;
- // Constructors
- /** default constructor */
- public Student() {
- }
- /** minimal constructor */
- public Student(String stuid) {
- this.stuid = stuid;
- }
- /** full constructor */
- public Student(String stuid, String stuname, String stutime) {
- this.stuid = stuid;
- this.stuname = stuname;
- this.stutime = stutime;
- }
- // Property accessors
- public String getStuid() {
- return this.stuid;
- }
- public void setStuid(String stuid) {
- this.stuid = stuid;
- }
- public String getStuname() {
- return this.stuname;
- }
- public void setStuname(String stuname) {
- this.stuname = stuname;
- }
- public String getStutime() {
- return this.stutime;
- }
- public void setStutime(String stutime) {
- this.stutime = stutime;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.frame.student.bean.Student" table="STUDENT" schema="ORACLE">
- <id name="stuid" type="java.lang.String">
- <column name="STUID" length="32" />
- <generator class="assigned" />
- </id>
- <property name="stuname" type="java.lang.String">
- <column name="STUNAME" length="32" />
- </property>
- <property name="stutime" type="java.lang.String">
- <column name="STUTIME" length="32" />
- </property>
- </class>
- </hibernate-mapping>
- package com.frame.student.dao;
- import com.frame.base.dao.BaseDao;
- public interface StudentDao extends BaseDao{
- }
- package com.frame.student.dao;
- import org.springframework.stereotype.Repository;
- import org.springframework.transaction.annotation.Transactional;
- import com.frame.base.dao.BaseDaoImpl;
- @Repository
- public class StudentDaoImpl extends BaseDaoImpl implements StudentDao{
- }
//测试类
- package com.frame.student.test;
- import org.junit.Before;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import com.frame.student.bean.Student;
- import com.frame.student.dao.StudentDao;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext.xml"})
- public class Test {
- @Autowired
- StudentDao dao;
- Student student;
- @Before
- public void setUp() throws Exception {
- //做一些初始化操作 比如创建对象等
- student=new Student();
- student.setStuid("99");
- student.setStuname("zhagnsan");
- student.setStutime("2017-1-31");
- }
- @org.junit.Test
- public void insertStudent() {
- try {
- dao.save(student);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
//源代码
http://pan.baidu.com/s/1hsvKafa