當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring整合Mybatis和JUnit
生活随笔
收集整理的這篇文章主要介紹了
Spring整合Mybatis和JUnit
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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=itzhuzhuconfig.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:
- 從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());} }總結
以上是生活随笔為你收集整理的Spring整合Mybatis和JUnit的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 删除单个创建文件夹,Linu
- 下一篇: SpringMVC配置静态资源加载, 中