Spring ORM

ORM(Object Relational Mapping)对象关系映射,是一种程序设计技术,用于实现面向对象编程里不同类型程序的转换之间的转换。
OMR提供了实现持久化层的另一种模式,它采用了映射元数据来描述对象关系映射,使得ORM能够在如何应用的业务逻辑层和数据库层之间充当桥梁。

ORM的方法论基于三个核心原则:

  • 简单:以基本的形式建模数据
  • 传达性:数据库结构被任何人理解的语言文档化
  • 精确性:基于数据模型创建正确的标准化结构
Spring ORM

创建项目

添加所需的maven

    <dependency>
    	<groupId>org.springframework</groupId>
         <artifactId>spring-websocket</artifactId>
         <version>4.3.10.RELEASE</version>
    </dependency>

		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

配置spring-orm文件


<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
		
		
		<!-- 导入配置文件 -->
		<context:property-placeholder  location = "classpath:config/db.properties"/>
		
		<!-- 数据源 -->
		<bean class = "com.mchange.v2.c3p0.ComboPooledDataSource" id = "dataSource">
			<property name="jdbcUrl" value="${jdbc.url}"/>
			<property name="driverClass" value="${jdbc.driver}"/>
			<property name="user" value="${jdbc.username}"/>
			<property name="password" value="${jdbc.password}"/>
		</bean>
		
		<!--自动扫描包下的类 在spring容器里面创建bean-->
		<context:component-scan base-package="com.dao.impl"/>
		
		<!-- 配置事物管理器 -->
		<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name = "dataSource" ref = "dataSource"/>
		</bean>
		
		<!-- 通知!映射到上面的事物管理器 -->
		<tx:advice transaction-manager = "transactionManager" id="transactionInterceptor">
			<tx:attributes>
				<tx:method name="add*"/>
				<tx:method name="select*"/>
				<tx:method name="update*"/>
				<tx:method name="delete*"/>
				<tx:method name="insert*"/>
			</tx:attributes>
		</tx:advice>
		
		<!-- apo切入点切入进行事物管理 -->
		<aop:config>
			<aop:advisor advice-ref="transactionInterceptor" pointcut="execution(* com.service.impl.*.*(..))"/>			
		</aop:config>
</beans>

配置db.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=GMT%2B8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123...

创建映射类
与数据字段一致

package erentry;

public class User {

	
	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;
	}
	
}

在这里插入图片描述

创建UserDao

package com.dao;

import java.util.List;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;

import erentry.User;


public interface UserDao {
	
	public List<User> selectAll(String sql);
}

创建UserDaoImpl

package com.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.dao.UserDao;

import erentry.User;
//dao层创建bean的注解
@Repository	
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
	
	@Resource	//dataSource bean注入
	public void init(DataSource dataSource){
		super.setDataSource(dataSource);
	}

	@Override
	public List<User> selectAll(String sql) {
		List<User> list = this.getJdbcTemplate().query(sql, new BeanPropertyRowMapper<User>(User.class));
		return list;
	}

}

单元测试(不会的看我Spring Test文章)

import org.junit.Test;
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;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:config/spring-orm.xml"})
public class tsp {

	@Autowired
    private UserDao userDAO;
	
	@Test
	public void test1(){
		//查询全部
		List<User> all = userDAO.selectAll("select * from user");
		for (int i = 0; i < all.size(); i++) {
			User user = all.get(i);
			System.out.println("名字:"+user.getName());
			System.out.println("年龄:"+user.getAge());
		}
	}
	
}

结果:

名字:wore
年龄:22
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值