图片

数据表
drop table teacher;
create table teacher(
id number primary key,
name varchar2(22),
age number,
salary number
);
create sequence my_seq;
bean
package com.briup.spring.bean;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Teacher {
private int id;
private String name;
private int age;
private double salary;
}
dao
package com.briup.spring.dao;
import com.briup.spring.bean.Teacher;
public interface ITeacherDao {
void save(Teacher teacher);
}
service
package com.briup.spring.service;
import com.briup.spring.bean.Teacher;
public interface ITeacherService {
void save(Teacher teacher);
}
impl
package com.briup.spring.service.impl;
import com.briup.spring.bean.Teacher;
import com.briup.spring.dao.ITeacherDao;
import com.briup.spring.service.ITeacherService;
import lombok.Setter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
@Setter
public class TeacherServiceImpl implements ITeacherService {
private ITeacherDao teacherDao;
@Override
public void save(Teacher teacher) {
teacherDao.save(teacher);
}
}
resources
mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.briup.spring.dao.ITeacherDao">
<insert id="save">
insert into TEACHER(id, name, age, salary)
values (my_seq.nextval,#{name},#{age},#{salary})
</insert>
</mapper>
applicaitonContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
</beans>
dataSource.properties
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
jdbc.username=P
jdbc.password=P
log4j.properties
<?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">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
</beans>
spring-dao.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http:
http:
http:
https:
<!--外部绑定文件-->
<context:property-placeholder location="classpath:dataSource.properties"/>
<!--dataSource 配置-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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>
<!--SqlSessionFactory对象 ioc-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--包别名-->
<property name="typeAliasesPackage" value="com.briup.spring.bean"/>
<!--mapper映射文件扫描-->
<property name="mapperLocations" value="classpath:com/briup/spring/dao/*.xml"/>
</bean>
<!--Mapper接口实现类 代理类-->
<!--<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"/>
</bean>-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.briup.spring.dao"/>
</bean>
</beans>
spring-service.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: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:
http:
http:
https:
http:
https:
http:
http:
<!--切面类 事务类 默认使用的环绕通知-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--目标类-->
<bean class="com.briup.spring.service.impl.TeacherServiceImpl">
<!--代理类在容器的名字 和接口一致-->
<property name="teacherDao" ref="ITeacherDao"/>
</bean>
<!--事务advice通知 配置事务的属性-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--事务属性-->
<tx:attributes>
<!--精确配置 具体方法的属性-->
<tx:method name="sa*"
propagation="REQUIRED"
isolation="DEFAULT"
timeout="-1"
read-only="false"
rollback-for="Exception"/>
<tx:method name="find*"
propagation="REQUIRED"
isolation="DEFAULT"
timeout="-1"
read-only="true"
rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!--aop 动态增强事务 切面-目标类-->
<aop:config>
<!--切入点 需要添加事务的方法-->
<aop:pointcut id="point" expression="execution(* com.briup.spring.service..*.*(..))"/>
<!--切面-->
<!-- <aop:aspect>
<aop:before method=""
</aop:aspect>-->
<!--advisor 拦截器(事务类)-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
</aop:config>
</beans>
Test
import com.briup.spring.bean.Teacher;
import com.briup.spring.dao.ITeacherDao;
import com.briup.spring.service.ITeacherService;
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:applicaitonContext.xml")
public class TestService {
@Autowired
private ITeacherService teacherService;
@Test
public void test(){
System.out.println(teacherService.getClass());
Teacher teacher = Teacher.builder()
.name("briup_service")
.age(18)
.salary(30)
.build();
teacherService.save(teacher);
}
}