整合数据库 @TOC
数据库准备 1 2 3 4 5 6 7 8 9 10 11 create table user ( id varchar (20 ) not null comment '编号' primary key, name varchar (20 ) null comment '用户名' , age int null comment '年龄' , sex int default 1 null comment '性别,1为男,2为女,默认为男' );INSERT INTO `hao- blog`.user (id, name, age, sex) VALUES ('1001001' , 'Jack' , 23 , 1 );INSERT INTO `hao- blog`.user (id, name, age, sex) VALUES ('10011002' , 'Kity' , 22 , 2 );
开发 1.pom依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-boot-starter</artifactId > <version > 3.4.3.4</version > </dependency > <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-core</artifactId > <version > 3.4.3.4</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency >
2.application.yml 1 2 3 4 5 6 7 8 9 10 11 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/hao-blog?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8 username: root password: aaaa
3.Java User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.hao.model;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Builder;import lombok.Data;import java.io.Serializable;@Data @Builder @TableName(value = "User") public class User implements Serializable { private static final long serialVersionUID = 1L ; @TableId(value = "id") private String id; private String name; private Integer age; private String sex; }
Mapper
1 2 3 4 5 6 7 8 9 10 11 package com.hao.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.hao.model.User;import org.apache.ibatis.annotations.Mapper;@Mapper public interface UserMapper extends BaseMapper <User> { }
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.hao.service;import com.hao.mapper.UserMapper;import com.hao.model.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Service public class UserService { @Autowired private UserMapper userMapper; public List<User> queryAll () { return userMapper.selectList(null ); } }
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.hao.controller;import com.hao.model.User;import com.hao.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller @RequestMapping("user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") @ResponseBody public List<User> getUserList () { return userService.queryAll(); } }
测试 启动项目。 浏览器输入地址:http://127.0.0.1:9080/user/list
,结果如下: 注:因为本人安装了JSON格式化插件,故显示结果和上述在效果上不一致。
其他操作 insert操作: 1 2 3 4 5 6 7 8 9 10 public int add (User user) { return userMapper.insert(user); }public void add (List<User> users) { for (User user : users) { add(user); } }
执行添加操作,直接调用insert方法传入实体即可。
select操作: 查询全部
1 2 3 public List<User> queryAll () { return userMapper.selectList(null ); }
通过id查询
1 2 3 public User queryById (User user) { return userMapper.selectById(user.getId()); }
通过姓名模糊查询
1 2 3 4 5 public List<User> queryByName (String name) { QueryWrapper<User> userQueryWrapper = new QueryWrapper <>(); userQueryWrapper.like("name" , name); return userMapper.selectList(userQueryWrapper); }
通过姓名精确查询
1 2 3 4 5 public List<User> queryByName2 (String name) { QueryWrapper<User> userQueryWrapper = new QueryWrapper <>(); userQueryWrapper.eq("name" , name); return userMapper.selectList(userQueryWrapper); }
注:还可以使用map来实现相同的效果
1 2 3 4 5 public List<User> queryByNameMap (String name) { Map<String, Object> map = new HashMap <>(); map.put("name" , name); return userMapper.selectByMap(map); }
通过Id查询批量查询
1 2 3 4 5 6 public List<User> queryByIds () { List<String> idList = new ArrayList <>(); idList.add("10011002" ); idList.add("10011001" ); return userMapper.selectBatchIds(idList); }
计数
1 2 3 4 public Long count () { QueryWrapper<User> userQueryWrapper = new QueryWrapper <>(); return userMapper.selectCount(userQueryWrapper); }
注:也可以和上面的一样加入各种条件
其他操作类似,不一一列举。