1.导包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> 2.controller层代码 @SpringBootApplication public class SBTestApplication public static void main(String[] args) SpringApplication.run(SBTestApplication.class);
3.测试代码--test @RunWith(SpringRunner.class) @SpringBootTest(classes = SBTestApplication.class) //classes路径是Application的字节码文件 public class SBTest @Test public void method() System.out.println("1111111");
2.Dao.Impl层 @Repository public class JDBCDaoImpl implements JDBCDao //注入数据库操作对象JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate;
@Override public List<Employee> findAll() String sql="select id,username,password from t_employee"; List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class)); return list;
3.service层 public interface JDBCService List<Employee> findAll();
4.service.impl层 @Service public class JDBCServiceImpl implements JDBCService @Autowired private JDBCDao jdbcDao;
@Override public List<Employee> findAll() return jdbcDao.findAll();
5.controller层 @Controller public class JDBCController @Autowired private JDBCService jdbcService; @RequestMapping("/findAll") public void findAll() jdbcService.findAll();
6.Application类 @SpringBootApplication //ComponentScan:配置注解扫描,扫描@service/@Repository等 @ComponentScan("cn.itsource") public class JDBCApplication public static void main(String[] args) SpringApplication.run(JDBCApplication.class);
7.测试类 @RunWith(SpringRunner.class) @SpringBootTest(classes = JDBCApplication.class) public class JdbcTest @Autowired private JDBCService jdbcService; @Test public void method() //注入service层调用方法 List<Employee> employees = jdbcService.findAll(); for (Employee e:employees) System.out.println(e);