博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot--mybatis,实现curd
阅读量:5834 次
发布时间:2019-06-18

本文共 8163 字,大约阅读时间需要 27 分钟。

项目

github

目录结构

clipboard.png

idea创建最简单的web项目

clipboard.png

clipboard.png

clipboard.png

clipboard.png

clipboard.png

运行项目

clipboard.png

clipboard.png

配置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

clipboard.png

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实体类

clipboard.png

对应数据库字段

package com.javaproject5.demo.model;public class User {    private int no;    private String name;    private String email;}

get,set

clipboard.png

clipboard.png

clipboard.png

生成完整代码

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操作)

clipboard.png

clipboard.png

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

clipboard.png

interface接口

clipboard.png

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 实现

clipboard.png

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

clipboard.png

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

查找

clipboard.png

clipboard.png

转载地址:http://xkycx.baihongyu.com/

你可能感兴趣的文章
重构第10天:提取方法(Extract Method)
查看>>
Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
查看>>
MySQL出现Access denied for user ‘root’@’localhost’ (using password:YES)
查看>>
通过Roslyn构建自己的C#脚本(更新版)(转)
查看>>
红黑树
查看>>
第四章 mybatis批量insert
查看>>
Java并发框架——什么是AQS框架
查看>>
【数据库】
查看>>
WindowManager.LayoutParams 详解
查看>>
find的命令的使用和文件名的后缀
查看>>
Android的Aidl安装方法
查看>>
Linux中rc的含义
查看>>
asp.net怎样在URL中使用中文、空格、特殊字符
查看>>
实现跨交换机VLAN间的通信
查看>>
python例子
查看>>
环境变量(总结)
查看>>
Java基础之String,StringBuilder,StringBuffer
查看>>
1月9日学习内容整理:爬虫基本原理
查看>>
安卓中数据库的搭建与使用
查看>>
AT3908 Two Integers
查看>>