Spring

配置文件:

 

mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.jr.entity"/>
<package name="com.jr.vo"/>
</typeAliases>
</configuration>

spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!-- 1 ~ 4: 配置mybatis -->

<!-- 1.加载外部属性文件,获得连接数据库四要素 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 2.配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClass}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>

<!-- 3.管理SqlSessionFactory对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>

<!-- 4.配置mapper映射文件的扫描器对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jr.mapper"/>
<property name="sqlSessionFactoryBeanName" value="factory"/>
</bean>

<!-- 5.配置注解扫描器-->
<context:component-scan base-package="com.jr"/>

<!-- 6.配置自动创建aop代理类 -->
<aop:aspectj-autoproxy/>

<!-- 7.声明事务管理的对象 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 8.配置事务的传播策略 -->
<!--<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
&lt;!&ndash; name: 使用通配符 给方法设置策略 &ndash;&gt;
<tx:method name="add*" propagation="REQUIRED"/> &lt;!&ndash; 有事务就加入,没有就开启一个事务 &ndash;&gt;
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="SUPPORTS"/> &lt;!&ndash; 有事务就加入,没有就在非事务下执行 &ndash;&gt;
</tx:attributes>
</tx:advice>-->

<!-- 9.配置xml方式的声明式事务 -->
<!--<aop:config>
<aop:pointcut id="txpointcut" expression="execution(* com.jr.service.DeptServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/>
</aop:config>-->

<!-- 配置事务支持注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

实体类注解:
/**
* @Component == 代替bean标签
* @Data == 自动生成get/set/toString
* @AllArgsConstructor == 自动生成全参构造器
* @NoArgsConstructor == 自动生成无参构造器
*/
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor


实现类注解:

 

 

 代理类:

@Component
@Aspect
public class Advice {

@Before("execution(* com.jr.service.*ServiceImpl.*(..))")
public void check() {
System.out.println("权限检查");
}

@After("execution(* com.jr.service.*ServiceImpl.*(..))")
public void log() {
System.out.println("日志记录");
}
}

测试类:
ApplicationContext applicationContext;
DeptService deptService;
Dept dept;

@Before
public void before() {
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
deptService = applicationContext.getBean("deptServiceImpl", DeptService.class);
dept = applicationContext.getBean("dept", Dept.class);
}

@Test
public void getTest() {
List<Dept> list = deptService.getAll();
for (Dept dept : list) {
System.out.println(dept);
}
}


 

posted @ 2023-10-06 15:54  JJJ毓  阅读(17)  评论(0)    收藏  举报