项目
github
目录结构
idea创建最简单的web项目
运行项目
配置pom.xml
mysql mysql-connector-java 5.1.42 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
配置config
MybatisConfig
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 注解版 * */@Configurationpublic class MybatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return new ConfigurationCustomizer() { @Override public void customize(org.apache.ibatis.session.Configuration configuration) { // 设置驼峰命名规则 configuration.setMapUnderscoreToCamelCase(true); } }; }}
SwaggerConfig
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@EnableSwagger2public 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(); }}
WebConfiguration
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 = 8888spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/liang?useUnicode=true&characterEncoding=utf-8&useSSL=falsespring.datasource.username=rootspring.datasource.password=123456
model实体类
对应数据库字段
package com.javaproject5.demo.model;public class User { private int no; private String name; private String email;}
get,set
生成完整代码
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; }}
mapper(sql操作)
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;@Mapperpublic 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 getUser();}
service
interface接口
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 getUser();}
impl 实现
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;@Servicepublic 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 getUser() { List userList = userMapper.getUser(); // lamdba表达式的使用 // List userListOrderBy= userList.stream().filter((User u) -> u.getName() == "1212").collect(Collectors.toList()); Collections.sort(userList,(user1, user2) -> user1.getNo() - user2.getNo()); return userList; }}
controller
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 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); }}
curd
查找