public class Country {
//实体类参考本人博客地址:https://blog.csdn.net/yl_hahha/article/details/80208161
private long id;
private String countryname;
private String countrycode;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
@Override
public String toString() {
return "Country [id=" + id + ", countryname=" + countryname + ", countrycode=" + countrycode + "]";
}
}
/**
* service层是给controller调用,如果想写需要将dao包装成service,然后实现类来调用dao 来提供给controller调用
*/
@Mapper
public interface CountryMapper {
@Insert(“insert into country(countryname, countrycode) values(#{countryname}, #{countrycode})”)
int add(@Param(“countryname”) String countryname, @Param(“countrycode”) String countrycode);
@Select("select id, countryname as countryname, countrycode as countrycode from country")
List<Country> findCounList();
}
@SpringBootApplication
public class DemomybatisApplication {
public static void main(String[] args) {
SpringApplication.run(DemomybatisApplication.class, args);
}
}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemomybatisApplicationTests {
@Autowired(required = false)
private CountryMapper mapper;
@Test
public void contextLoads() {
}
@Test
public void testFindAll(){
List<Country> counList = mapper.findCounList();
Assert.assertTrue(counList.size()>1);
}
//
@Test
public void testInsert(){
int add = mapper.add("shenzhen", "sz");
Assert.assertTrue(add>0);
}
}