mysql增删改查分页项目_spring boot+mybatis+mysql增删改查分页

server:

port:8081servlet:

context-path: /springBootMybatis

spring:

datasource:

name: test

url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=true

username: root

password: root

#新版数据库驱动使用com.mysql.cj.jdbc.Driver 旧版com.mysql.jdbc.Driver

driver-class-name: com.mysql.cj.jdbc.Driver

type: com.alibaba.druid.pool.DruidDataSource

mybatis:

#这里是实体类的位置,#实体扫描,多个package用逗号或者分号分隔

#typeAliasesPackage: com.hegg.springboot.model

type-aliases-package: com.hegg.springboot.model

#把xml文件放在com.XX.mapper.*中可能会出现找不到的问题,这里把他放在resource下的mapper中

mapper-locations: classpath:mapper/*.xml

#分页插件

pagehelper:

helperDialect: mysql #分页插件方言选择

reasonable: true #合理化参数,设为true时pageNum<=0 时会查第一页, pageNum>pages(超过总数时),会查询最后一页

supportMethodsArguments: true

1、先附上application.yml的配置

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.3.RELEASE

com.hegg

springboot

0.0.1-SNAPSHOT

springboot

Demo project for Spring Boot

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.0.0

mysql

mysql-connector-java

org.springframework.boot

spring-boot-starter-test

com.alibaba

druid

1.1.3

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.10

org.springframework.boot

spring-boot-maven-plugin

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.2

${basedir}/src/main/resources/generator/generatorConfig.xml

true

true

2、pom.xml的代码,其中三段代码是自己加的

com.alibaba

druid

1.1.3

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.10

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.2

${basedir}/src/main/resources/generator/generatorConfig.xml

true

true

3、新建generatorConfig.xml用来自动生成实体类和对应的*Mapper.xml和*Mapper.java(*Mapper.java是接口interface)

/p>

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

aad91135bda7e0d15e9ba95c54c93c11.png

4、编译运行生成代码点击run>Edit Configuration,如下图找到maven在标注的位置输入对应的代码运行即可generator和

mybatis-generator:generate -e。千万不要重复运行,不然在*Mapper.xml中就会重复生成方法,还有

generatorConfig.xml文件中连接数据使用的jar包不要使用太高的版本。

7d1903492e8f66854be9ff6577c3398c.png

5、下面是生成的接口代码UserMapper.java,其中后面三个方法是自己加的,其他的都是自动生成的,

importcom.github.pagehelper.Page;importcom.github.pagehelper.PageInfo;importcom.hegg.springboot.model.User;importjava.util.List;public interfaceUserMapper {intdeleteByPrimaryKey(Integer id);intinsert(User record);intinsertSelective(User record);

User selectByPrimaryKey(Integer id);intupdateByPrimaryKeySelective(User record);intupdateByPrimaryKey(User record);

ListgetAllUser();

PagefindByPage();

PageInfofindByPageInfo();

}

UserMapper.xml中后面三个方法是自己加的

id, name, sex, age

selectfrom user

where id = #{id,jdbcType=INTEGER}

delete from user

where id = #{id,jdbcType=INTEGER}

insert into user (id, name, sex,

age)

values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER},

#{age,jdbcType=INTEGER})

insert into user

id,

name,

sex,

age,

#{id,jdbcType=INTEGER},

#{name,jdbcType=VARCHAR},

#{sex,jdbcType=INTEGER},

#{age,jdbcType=INTEGER},

update user

name = #{name,jdbcType=VARCHAR},

sex = #{sex,jdbcType=INTEGER},

age = #{age,jdbcType=INTEGER},

where id = #{id,jdbcType=INTEGER}

update user

set name = #{name,jdbcType=VARCHAR},

sex = #{sex,jdbcType=INTEGER},

age = #{age,jdbcType=INTEGER}

where id = #{id,jdbcType=INTEGER}

selectfrom USER

selectfrom USER

selectfrom USER

贴出UserService.java、UserServiceImpl.java、UserController.java的代码

importcom.github.pagehelper.Page;importcom.github.pagehelper.PageInfo;importcom.hegg.springboot.model.User;importjava.util.List;public interfaceUserService {intdeleteByPrimaryKey(Integer id);intinsert(User record);intinsertSelective(User record);

User selectByPrimaryKey(Integer id);intupdateByPrimaryKeySelective(User record);intupdateByPrimaryKey(User record);

ListgetAllUser();

PagefindByPage(Integer pageNum, Integer pageSize);

PageInfofindByPageInfo(Integer pageNum, Integer pageSize);

}

importcom.github.pagehelper.Page;importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importcom.hegg.springboot.mapper.UserMapper;importcom.hegg.springboot.model.User;importcom.hegg.springboot.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;

@Servicepublic class UserServiceImpl implementsUserService {

@AutowiredprivateUserMapper userMapper;//这个里会报红,但不影响

@Overridepublic intdeleteByPrimaryKey(Integer id) {returnuserMapper.deleteByPrimaryKey(id);

}

@Overridepublic intinsert(User record) {returnuserMapper.insert(record);

}

@Overridepublic intinsertSelective(User record) {returnuserMapper.insertSelective(record);

}

@OverridepublicUser selectByPrimaryKey(Integer id) {returnuserMapper.selectByPrimaryKey(id);

}

@Overridepublic intupdateByPrimaryKeySelective(User record) {returnuserMapper.updateByPrimaryKeySelective(record);

}

@Overridepublic intupdateByPrimaryKey(User record) {returnuserMapper.updateByPrimaryKey(record);

}

@Overridepublic ListgetAllUser() {returnuserMapper.getAllUser();

}

@Overridepublic PagefindByPage(Integer pageNum, Integer pageSize) {

PageHelper.startPage(pageNum, pageSize);returnuserMapper.findByPage();

}

@Overridepublic PageInfofindByPageInfo(Integer pageNum, Integer pageSize) {

PageHelper.startPage(pageNum, pageSize);

List userList =userMapper.findByPage();

PageInfo pageInfo = new PageInfo<>(userList);returnpageInfo;

}

}

importcom.github.pagehelper.Page;importcom.github.pagehelper.PageInfo;importcom.hegg.springboot.common.MyResponse;importcom.hegg.springboot.model.User;importcom.hegg.springboot.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;importjava.util.List;

@RestController

@RequestMapping("/userController")public classUserController {

@AutowiredprivateUserService userService;

@RequestMapping(value= "/hello", method = RequestMethod.GET, produces = "application/json; charset=utf-8")

@ResponseBodypublicString getHello() {return "Hello,Word!!!世界5";

}

@RequestMapping(value= "/selectByPrimaryKey", method =RequestMethod.POST)

@ResponseBodypublicMyResponse selectByPrimaryKey(Integer id){

User user= this.userService.selectByPrimaryKey(id);

MyResponse response= newMyResponse();if(null !=user){

response.setCode("success");

response.setMsg("成功");

response.setData(user);

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@RequestMapping(value= "/getAllUser", method =RequestMethod.POST)

@ResponseBodypublicMyResponse getAllUser(){

List list = this.userService.getAllUser();

MyResponse response= newMyResponse();if(null != list && list.size() > 0){

response.setCode("success");

response.setMsg("成功");

response.setData(list);

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@RequestMapping(value= "/findByPage", method =RequestMethod.POST)

@ResponseBodypublicMyResponse findByPage(Integer pageNum, Integer pageSize){

Page list = this.userService.findByPage(pageNum, pageSize);

MyResponse response= newMyResponse();if(null != list && list.size() > 0){

response.setCode("success");

response.setMsg("成功");

response.setData(list);

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@RequestMapping(value= "/findByPageInfo", method =RequestMethod.POST)

@ResponseBodypublicMyResponse findByPageInfo(Integer pageNum, Integer pageSize){

PageInfo list = this.userService.findByPageInfo(pageNum, pageSize);

MyResponse response= newMyResponse();if(null != list && list.getList().size() > 0){

response.setCode("success");

response.setMsg("成功");

response.setData(list);

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@ResponseBody

@RequestMapping(value= "/insert", produces = {"application/json;charset=UTF-8"})publicMyResponse insert(User user){int result =userService.insert(user);

MyResponse response= newMyResponse();if(result == 1){

response.setCode("success");

response.setMsg("成功");

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@ResponseBody

@RequestMapping(value= "/insertSelective", produces = {"application/json;charset=UTF-8"})publicMyResponse insertSelective(User user){int result =userService.insertSelective(user);

MyResponse response= newMyResponse();if(result == 1){

response.setCode("success");

response.setMsg("成功");

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@ResponseBody

@RequestMapping(value= "/deleteByPrimaryKey", produces = {"application/json;charset=UTF-8"})publicMyResponse deleteByPrimaryKey(Integer id){int result =userService.deleteByPrimaryKey(id);

MyResponse response= newMyResponse();if(result == 1){

response.setCode("success");

response.setMsg("成功");

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@ResponseBody

@RequestMapping(value= "/updateByPrimaryKeySelective", produces = {"application/json;charset=UTF-8"})publicMyResponse updateByPrimaryKeySelective(User user){int result =userService.updateByPrimaryKeySelective(user);

MyResponse response= newMyResponse();if(result == 1){

response.setCode("success");

response.setMsg("成功");

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

@ResponseBody

@RequestMapping(value= "/updateByPrimaryKey", produces = {"application/json;charset=UTF-8"})publicMyResponse updateByPrimaryKey(User user){int result =userService.updateByPrimaryKey(user);

MyResponse response= newMyResponse();if(result == 1){

response.setCode("success");

response.setMsg("成功");

}else{

response.setCode("faild");

response.setMsg("失败");

}returnresponse;

}

}

最后启动类的代码,在类的前面一定要加注解@MapperScan,里面是*Mapper.java所在包名

importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan({"com.hegg.springboot.mapper"})public classSpringbootApplication {public static voidmain(String[] args) {

SpringApplication.run(SpringbootApplication.class, args);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值