Spring整合Mybatis:
注解整合MyBatis分析
- 业务类使用注解形式声明bean,属性采用注解注入
- 建立独立的配置管理类,分类管理外部资源,根据功能进行分类,并提供对应的方法获取bean
- 使用注解形式启动bean扫描,加载所有注解配置的资源(bean)
- 使用AnnotationConfigApplicationContext对象加载所有的启动配置类,内部使用导入方式进行关联
注解整合MyBatis步骤:
1.修改mybatis外部配置文件格式为注解格式
2.业务类使用@Component声明bean,使用@Autowired注入对象
3.建立配置文件JDBCConfig与MyBatisConfig类,并将其导入到核心配置类SpringConfig
4.开启注解扫描
5.使用AnnotationConfigApplicationContext对象加载配置项
pom文件
dao.AccountDao
public interface AccountDao {@Insert("insert into account(name,money)values(#{name},#{money})")void save(Account account);@Delete("delete from account where id = #{id} ")void delete(Integer id);@Update("update account set name = #{name} , money = #{money} where id = #{id} ")void update(Account account);@Select("select * from account")List<Account> findAll();@Select("select * from account where id = #{id} ")Account findById(Integer id);
}
domain.Account
public class Account implements Serializable {private Integer id;private String name;private Double money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getMoney() {return money;}public void setMoney(Double money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
service.AccountService
public interface AccountService {void save(Account account);void delete(Integer id);void update(Account account);List<Account> findAll();Account findById(Integer id);
}
service.impl.AccountServiceImpl
@Service("accountService")
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;@Overridepublic void save(Account account) {accountDao.save(account);}@Overridepublic void update(Account account) {accountDao.update(account);}@Overridepublic void delete(Integer id) {accountDao.delete(id);}@Overridepublic Account findById(Integer id) {return accountDao.findById(id);}@Overridepublic List<Account> findAll() {return accountDao.findAll();}
}
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db2
jdbc.username=root
jdbc.password=itzhuzhu
config.JDBCConfig
public class JDBCConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Bean("dataSource")public DataSource getDataSource(){System.out.println(driver);DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}
config.MyBatisConfig
public class MyBatisConfig {@Beanpublic SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage("com.itzhuzhu.domain");ssfb.setDataSource(dataSource);return ssfb;}@Beanpublic MapperScannerConfigurer getMapperScannerConfigurer() {MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itzhuzhu.dao");return msc;}
}
config.SpringConfig
@Configuration
@ComponentScan("com.itzhuzhu")
@PropertySource("classpath:jdbc.properties")
@Import({JDBCConfig.class, MyBatisConfig.class})
public class SpringConfig {
}
测试:
@Testpublic void Test2() {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);AccountService as = (AccountService) ctx.getBean("accountService");System.out.println(as.findById(1));}
Spring整合JUnit:
- Spring接管Junit的运行权,使用Spring专用的Junit类加载
- 为Junit测试用例设定对应的spring容器
- 从Spring5.0以后,要求Junit的版本必须是4.12及以上
- Junit仅用于单元测试,
不能将Junit的测试类配置成spring的bean
,否则该配置将会被打包进入工程中
导入Spring整合Junit坐标
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.9.RELEASE</version>
</dependency>
Spring整合Junit测试用例注解格式
// 配置Spring专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class)
// 加载Spring配置类
@ContextConfiguration(classes = SpringConfig.class)
public class ServiceTest {@Autowiredprivate AccountService accountService;@Testpublic void testFindById() {Account ac = accountService.findById(2);
// System.out.println(ac);Assert.assertEquals("Jock1", ac.getName());}@Testpublic void testFindAll() {List<Account> list = accountService.findAll();Assert.assertEquals(3, list.size());}
}