https://github.com/1134506391...html
<!--添加mysql依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> <!--添加Mybatis依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
package com.javaproject5.demo.config; import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * mybatis 註解版 * */ @Configuration public class MybatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return new ConfigurationCustomizer() { @Override public void customize(org.apache.ibatis.session.Configuration configuration) { // 設置駝峯命名規則 configuration.setMapUnderscoreToCamelCase(true); } }; } }
package com.javaproject5.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.*; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.List; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2構建RESTful APIs") .description("圖書子系統") .termsOfServiceUrl("更多請關注http://www.baidu.com") .contact("liangwei") .version("1.0") .build(); } }
package com.javaproject5.demo.config; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; @Configuration @AutoConfigureAfter(DispatcherServletAutoConfiguration.class) public class WebConfiguration extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } /** * CROS跨域的處理 */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH") .allowCredentials(true).maxAge(3600); } }
application.properties
server.port = 8888 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/liang?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456
package com.javaproject5.demo.model; public class User { private int no; private String name; private String email; }
package com.javaproject5.demo.model; public class User { private int no; private String name; private String email; public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package com.javaproject5.demo.mapper; import com.javaproject5.demo.model.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface UserMapper { @Insert({"insert into user(no, name, email) values(#{no}, #{name}, #{email})"}) void AddUser(User userInfo); @Delete("delete from user where no=#{no}") void delUserById(@Param("no") int no); @Update("update user set name = #{name}, email = #{email} where no = #{no}") void updateUserByNo(@Param("name") String name, @Param("email") String email, @Param("no") int no); @Select("select * from user") List<User> getUser(); }
package com.javaproject5.demo.service; import com.javaproject5.demo.model.User; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserService { void AddUser(User user); void delUserById(@Param("no") int no); void updateUserByNo(User user); List<User> getUser(); }
package com.javaproject5.demo.service.impl; import com.javaproject5.demo.mapper.UserMapper; import com.javaproject5.demo.model.User; import com.javaproject5.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Collections; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public void AddUser(User user) { userMapper.AddUser(user); } @Override public void delUserById(int no) { userMapper.delUserById(no); } @Override public void updateUserByNo(User user) { userMapper.updateUserByNo(user.getName(),user.getEmail(),user.getNo()); } @Override public List<User> getUser() { List<User> userList = userMapper.getUser(); // lamdba表達式的使用 // List<User> userListOrderBy= userList.stream().filter((User u) -> u.getName() == "1212").collect(Collectors.toList()); Collections.sort(userList,(user1, user2) -> user1.getNo() - user2.getNo()); return userList; } }
package com.javaproject5.demo.controller; import com.javaproject5.demo.model.User; import com.javaproject5.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api") public class UserController { @Autowired private UserService userService; @RequestMapping(value ="/user", method = RequestMethod.GET) public List<User> UserQry() { return userService.getUser(); } @RequestMapping(value ="/user", method = RequestMethod.POST) public void UserAdd(User user){ userService.AddUser(user); } @RequestMapping(value ="/user", method = RequestMethod.PUT) public void UserUpdate(User user){ userService.updateUserByNo(user); } @RequestMapping(value ="/user", method = RequestMethod.DELETE) public void UserDel(@RequestParam(value="no") int no){ userService.delUserById(no); } }