spring对junit的支持
主要是自动从spring容器中获取bean。不用手动获取了。
spring对junit4的支持
1.引入spring和junit4的依赖- org.springframework spring-context 6.0.4 org.springframework spring-test 6.0.4 junit junit 4.13.2 test
复制代码 2.编写测试类- @RunWith(SpringJUnit4ClassRunner.class)// 加载spring.xml配置文件 加载spring容器@ContextConfiguration("classpath:spring.xml")public class SpringJunit4Test { @Autowired private Account account; @Test public void testSpringJunit4(){ System.out.println(account.getActno()); }}
复制代码 spring对junit5的支持
- org.springframework spring-context 6.0.4 org.springframework spring-test 6.0.4 org.junit.jupiter junit-jupiter 5.9.2 test
复制代码 2.编写测试类- @ExtendWith(SpringExtension.class)@ContextConfiguration("classpath:spring.xml")public class SpringJunit5Test { @Autowired private Account account; @org.junit.jupiter.api.Test public void testSpringJunit5(){ System.out.println(account.getActno()); }}
复制代码 spring集成mybatis
- org.springframework spring-context 6.0.21 org.springframework spring-jdbc 7.0.2 mysql mysql-connector-java 8.0.30 org.mybatis mybatis 3.5.19 org.mybatis mybatis-spring 4.0.0 com.alibaba druid 1.1.20 junit junit 4.13.2 test
复制代码
- 创建三层架构需要的包:mapper、pojo、service
- 编写pojo类
- public class Account { private String actno; private double balance; @Override public String toString() { return "Account{" + "actno='" + actno + '\'' + ", balance=" + balance + '}'; } public Account() { } public Account(String actno, double balance) { this.actno = actno; this.balance = balance; } public String getActno() { return actno; } public void setActno(String actno) { this.actno = actno; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}
复制代码- // 该接口不需要写实现类,mybatis会根据动态代理机制自动生成实现类public interface AccountMapper { int update(Account getAccount); int insert(Account getAccount); int delete(String actno); Account getAccount(String actno); List getAllAccount();}
复制代码- insert into t_act(actno, balance) values(#{actno}, #{balance}) delete from t_act where actno = #{actno} update t_act set name = #{name}, money = #{money} where actno = #{actno} select actno, balance from t_act where actno = #{actno} select actno, balance from t_act
复制代码- public interface AccountService { int addAccount(Account account); int updateAccount(Account account); int deleteAccount(String actno); Account getAccount(String actno); List getAllAccount();}
复制代码- @Service("accountService")public class AccountServiceImpl implements com.ali.service.AccountService { @Autowired private AccountMapper accountMapper; @Override public int addAccount(Account account) { return accountMapper.insert(account); } @Override public int updateAccount(Account account) { return accountMapper.update(account); } @Override public int deleteAccount(String actno) { return accountMapper.delete(actno); } @Override public Account getAccount(String actno) { return accountMapper.getAccount(actno); } @Override public List getAllAccount() { return accountMapper.getAllAccount(); }}
复制代码- jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring6?useSSL=false&serverTimezone=UTCjdbc.username=rootjdbc.password=yourpassword
复制代码 在spring配置文件中引入子配置文件
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |