springboot项目指南
properties
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.username=scott
spring.datasource.password=tiger
spring.datasource.url = jdbc:oracle:thin:@localhost:1521:orcl
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
logging.level.root = INFO
logging.file.name="D:/logger/house.log"
server.port=8081
pom.xml
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
pojo
@Data 相当于@Getter@Setter@NoArgsConstructor@AllArgsConstructor@ToString
的汇总
@Data
public class House {
private int houseid;
private String houseimg;
private String houseaddr;
private int housearea;
private int houseprice;
private LoginUser user;
}
mapper
public interface HouseMapper {
@Delete("delete from house where houseid=#{houseid}")
public int deleteHouseById(@Param("houseid") int houseid);
public int deleteHouseByIds(int[] houseids);
@Select("select * from house")
public List<House> selectAllHouse();
@Insert("insert into house values(house_seq.nextval,#{h.houseaddr},#{h.housearea},#{h.houseprice},#{h.userid})")
public int insertHouse(@Param("h")House house);
@Update("update house set houseaddr = #{h.houseaddr},housearea = #{h.housearea},houseprice=#{h.houseprice},userid=#{h.userid} where houseid=#{h.houseid}")
public int updateHouse(@Param("h")House house);
}
service
@Service
public class HouseService<main> {
@Autowired
private HouseMapper hm;
public int deleteById(int houseid){
return hm.deleteHouseById(houseid);
}
public List<House> selectAll(){
System.out.println("selectall service");
return hm.selectAllHouse();
}
public int updateHouse(House house){
return hm.updateHouse(house);
}
public int addHouse(House house){
return hm.insertHouse(house);
}
public int deleteHouseByIds(int [] depts){
return hm.deleteHouseByIds(depts);
}
controller
@RestController
public class HouseController {
private Logger log = LoggerFactory.getLogger(DemoApplication.class);
@Autowired
private HouseService hs;
@GetMapping("/showall")
public Map<String,Object> showAll(){
Map<String,Object> hm = new HashMap<>();
List<House> houselist = hs.selectAll();
if(!houselist.isEmpty()){
hm.put("statecode",200);
hm.put("houselist",houselist);
}else{
hm.put("statecode",400);
}
return hm;
}
@DeleteMapping(value="/deleteHouseById/{houseid}")
public Map<String,Object> deleteById(@PathVariable("houseid") int houseid){
Map<String,Object> hm = new HashMap<>();
int num = hs.deleteById(houseid);
if(num>0){
hm.put("statecode",200);
}else{
hm.put("statecode",400);
}
return hm;
}
@Transactional
@DeleteMapping(value="/deleteHouseByIds")
public Map<String,Object> deleteByIds(int[] houseid){
log.info("删除操作");
Map<String,Object> hm = new HashMap<>();
int num = hs.deleteHouseByIds(houseid);
if(num>0){
hm.put("statecode",200);
}else{
hm.put("statecode",400);
}
return hm;
}
}
DemoApplication
@SpringBootApplication
@Configuration
@MapperScan("com.zretc.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
private CorsConfiguration buildConfig(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOriginPattern("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",buildConfig());
return new CorsFilter(source);
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/imctemp-rainy/**").addResourceLocations("file:E:/yinyifei_etc/housessm/src/main/resources/static/img/");
}
}