首先需要添加依赖
<!--添加mybatisplus包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
其次 需要一个能接受数据的实体类
/**
* @description 接受表Studyweb数据的实体类
* @author wangshiqin
*/
@Data//lombok依赖
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)//开启链式加载
@TableName("studyweb") //表名与pojo类的关系
public class StudyWeb_Do {
@TableId(type = IdType.AUTO)//主键自增
private Integer id;
private String name;
private String version;
private String addr;
private String bankcard;
private String username;
private String password;
private String logaddr;
private String db1;
private String db1user;
private String db1password;
private String db2;
private String db2user;
private String db2password;
}
接着创建一个JdbcManager接口 用于继承mybatisplus的CRUD基础类
/**
* @description 对数据库连接的处理
* @author wangshiqin
*/
@Mapper
public interface JdbcMapper extends BaseMapper<StudyWeb_Do>{
}
创建service 接口 并创建实现类 serviceimpl继承 service接口
/**
* @author wangshiqin
*/
public interface PersonService {
List<StudyWeb_Do> findStudyweb();
}
**
* @description 继承JDBC的接口方法,重写service接口中的方法
* @author wangshiqin
*/
@Service
public class ServiceImpl implements PersonService {
@Autowired
private JdbcMapper mapper;
/**
* 查询studyweb表所有数据
* @return
*/
@Override
public List<StudyWeb_Do> findStudyweb() {
return mapper.selectList(null);
}
}
QueryWrapper<> 条件构造器 用来拼接where条件
- 常见逻辑运算符: = eq, > gt , < lt * >= ge, <= le.like(“name”, “精”);
- //%在左侧选择likeleft,
- 在右侧选择likeRright.orderByDesc(“id”)依据id倒序
- .in()批量查询
创建控制层拦截请求,响应数据
/**
* @description 拦截页面请求
* @author wangshiqin
*/
@Controller
@Slf4j
@RequestMapping("")
public class PersonController {
@Autowired
private PersonService service;
/**
* @Description: 初始页面
* @Param: [model]
* @return: java.lang.String
* @Author: wangshiqin
* @Date: 2021/6/7
*/
@RequestMapping("/studyweb")
public String firstPage(String version, Model model){
List<StudyWeb_Do> studyweb = service.findStudyweb();
model.addAttribute("list", studyweb);
return "first";
}
}
最后在配置文件中 定义 打印sql 语句和结果
#Mybatis-pulus 配置日志 查看sql
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#驼峰映射=关闭(自动将personName 变成 person_name)
mybatis-plus.configuration.map-underscore-to-camel-case=false