springboot【Spring Boot中使用JdbcTemplate访问数据库】

http://blog.didispace.com/springbootdata1/

1.添加依赖

springboot【Spring Boot中使用JdbcTemplate访问数据库】

2.配置mysql

springboot【Spring Boot中使用JdbcTemplate访问数据库】 

 

3. 建

springboot【Spring Boot中使用JdbcTemplate访问数据库】 

springboot【Spring Boot中使用JdbcTemplate访问数据库】

springboot【Spring Boot中使用JdbcTemplate访问数据库】

4.  测试

  • 创建对UserService的单元测试用例,通过创建、删除和查询来验证数据库操作的正确性。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {

	@Autowired
	private UserService userSerivce;

	@Before
	public void setUp() {
		// 准备,清空user表
		userSerivce.deleteAllUsers();
	}

	@Test
	public void test() throws Exception {
		// 插入5个用户
		userSerivce.create("a", 1);
		userSerivce.create("b", 2);
		userSerivce.create("c", 3);
		userSerivce.create("d", 4);
		userSerivce.create("e", 5);

		// 查数据库,应该有5个用户
		Assert.assertEquals(5, userSerivce.getAllUsers().intValue());

		// 删除两个用户
		userSerivce.deleteByName("a");
		userSerivce.deleteByName("e");

		// 查数据库,应该有5个用户
		Assert.assertEquals(3, userSerivce.getAllUsers().intValue());

	}

}