全注解方式整合spring+mybatis模拟springboot整合

时间:2021-03-24 05:49:39

实习工作中的项目使用到了springboot。虽然springboot使用简单但是默认配置和注解挺多。如果boot想使用熟练前提要对ssm整合比较熟悉。以前开发习惯使用xml配置文件,但是使用boot后才发现注解才是王道。现在将spring+mybatis使用全注解完成整合,并且配置事务。文件中的注解与Xml进行了比对。直接上代码。

@Repository
public interface EmpDao {
Emp selectById(String id);
void addSalary(String id);
void updateSalary(String id);
}

<?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">
<!-- EmpMapper.xml -->
<mapper namespace="cn.rjx.spring.smanno.EmpDao">
<select id="selectById" resultType="cn.rjx.spring.smanno.Emp">
select * from emp where id=#{id}
</select>
<update id="addSalary">
update emp set salary='6000' where id=#{id}
</update>
<update id="updateSalary">
update emp set salary='4000' where id=#{id}
</update>
</mapper>


@Servicepublic class EmpService {	@Autowired	EmpDao empDao;	public Emp selectById(String id){		return empDao.selectById(id);	}	@Transactional()	public void accountChange() {		empDao.addSalary("1");		int a= 10/0;		empDao.updateSalary("2");	}	}


@MapperScan(basePackages="cn.rjx.spring.smanno")
/**
* 替代:
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 -->
<property name="basePackage" value="cn.rjx.spring.sm" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
*
*/
@EnableTransactionManagement
/** 替代:
* <tx:annotation-driven transaction-manager="transactionManager" />
*/
@Configuration
public class MybatisConfig {
/** 替代
* <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
*/
@Bean
public DruidDataSource dataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
}
/**
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:cn/rjx/spring/sm/*Mapper.xml" />
</bean>
*/
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception{
final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource());
sqlSessionFactory.setMapperLocations(getResource("", "cn/rjx/spring/smanno/*Mapper.xml"));
return sqlSessionFactory.getObject();
}
/**
* 替代:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
*/
@Bean
public PlatformTransactionManager transactionManager(){
return new DataSourceTransactionManager(dataSource());
}


public Resource[] getResource(String basePackage, String pattern) throws IOException {
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(new StandardEnvironment().resolveRequiredPlaceholders(basePackage)) + "/" + pattern;
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(packageSearchPath);
return resources;
}
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:cn/rjx/spring/smanno/bean.xml"}) //加载配置文件
@Component
@ComponentScan(basePackages="cn.rjx.spring.smanno")
/**替代:
* <context:component-scan base-package="cn.rjx.spring.sm"></context:component-scan>
*/
public class EmpAction {

@Autowired
EmpService service;
@Test
public void test01(){
Emp emp = service.selectById("1");
System.out.println(emp);
}
@Test
public void test02(){
System.out.println("------");
service.accountChange();
}

}

我的bean.xml只加入了测试的那个类,作为spring加载的入口

<bean id="empAction" class="cn.rjx.spring.smanno.EmpAction"></bean>


测试查询方法可用.事务可用!pom文件。

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- springAop -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- spring事务 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>

<!-- spring整合mybatis -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>3.1.14</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc 配置数据源需要-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>


</dependencies>