SSM整合(接口测试)
src/main/test 下创建 com.xxx.service.BookServiceTest
package com.xxx.service;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig. class)
public class BookServiceTest {
@Autowired
private BookService bookService;
@Test
public void testGetById(){
Book book = bookService.getById(1);
System.out.println(book);
}
@Test
public void testGetAll(){
List<Book> all = bookService.getAll();
System.out.println(all);
}
}
添加事务管理
SpringConfig.java
package com.xxx.config;
@Configuration
@Component({"com.xxx..service"})
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
@EnableTransactionManagement
public class SpringConfig {
}
JdbcConfig.java
package com.xxx.config;
public class JdbcConfig {
@Value("${jdbc.drive}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager ds = new DataSourceTransactionManager();
ds.setDataSource(dataSource);
}
}
BookService.java
package com.xxx.service;
@Transactionl
public interface BookService {
public boolean save(Book book);
public boolean update(Book book);
public boolean delete(Integer id);
public Book getById(Integer id);
public List<Book> getAll();
}
