1. 程式人生 > >springboot整合mybatis添加註解事務

springboot整合mybatis添加註解事務

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呼叫
* Invalid bound statement (not found)
* 解決方法: @MapperScan(“com.demomybatistransational.demo”)
*/
public interface CountryMapper {

public int update(@Param("countryname") String countryname,@Param("id")long id);

}

@Service
public class CountryServiceImpl {
    @Autowired(required = false)
    private CountryMapper mapper;
    @Transactional
    public void testUpdate(){
        long id =2;
         mapper.update("dq",id);
         int i=1/0;
         mapper.update("hh",id);
    }
}

@SpringBootApplication
@MapperScan(“com.demomybatistransational.demo”)
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.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

mybatis.type-aliases-package=com.demomybatistransational.demo