根据个人需求选择依赖,选择好后会直接在pom.xml中
mybatisplus依赖
com.baomidou
mybatis-plus-boot-starter
3.1.2
项目架构
application.yml
server:
port: 5000
spring:
datasource: #数据库连接池
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql:///homecrud?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
devtools: #热部署
restart:
enabled: true
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5
因为用了Mybatisplus代码生成器 根据数据库表反向生成pojo ,mapper,mapper.xml service,serviceimpl,controller,具体代码生成器使用见 mybatisplus篇
@TableName("real_eseate")
public class RealEseate extends Model {
private static final long serialVersionUID=1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("project_name")
private String projectName;
@TableField("address")
private String address;
@TableField("house_type")
private String houseType;
@TableField("area")
private Integer area;
@TableField("build_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime buildTime;
@TableField("user_id")
private Long userId;
//表示这个属性不会对应数据库中的列
@TableField(exist = false)
private User user;
}
mapper
public interface RealEseateMapper extends BaseMapper {
//高级分页查询
IPage queryPage(Page page, @Param("query") Requery query);
}
因为涉及到分页,使用Mybatisplus必须配置一个分页bean
@EnableTransactionManagement
@Configuration
@MapperScan("cn.leilei.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// paginationInterceptor.setLimit(你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制);
return paginationInterceptor;
}
}
mapper.xml
and
( u.username like concat('%',#{query.keywordName},'%')
)
and
(
u.card_id like concat('%',#{query.keywordCard},'%')
)
为考虑到前端框架分页所需数据的特殊性 封装一个分页对象类 (前端需要 total ,rows)
public class PageResult {
private Long total = 0L; //每页条数
private List rows = new ArrayList(); //每页数据
public PageResult(Long total, List rows) {
this.total = total;
this.rows = rows;
}
public PageResult() {
}
//get set方法
}
service
public interface IRealEseateService extends IService {
PageResult queryBypage(Requery requery);
}
serviceimpl
@Service
public class RealEseateServiceImpl extends ServiceImpl implements IRealEseateService {
//关联用户分页查询
@Override
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public PageResult queryBypage(Requery query) {
Page page = new Page<>(query.getPage(),query.getRows());
IPage ip = baseMapper.queryPage(page, query);
return new PageResult<>(ip.getTotal(),ip.getRecords());
}
}
controller
@Controller
public class RealEseateController {
@Autowired
private IRealEseateService realEseateService;
@RequestMapping("/index")
public String index(org.springframework.ui.Model model, Requery requery) {
PageResult realEseatePageResult = realEseateService.queryBypage(requery);
model.addAttribute("reals", realEseatePageResult.getRows());
return "ye";
}
//添加的跳转
@RequestMapping("/addIndex")
public String addIndex(){
return "add";
}
//添加
@RequestMapping("/add")
public String add(RealEseate user){
realEseateService.save(user);
return "redirect:/index";
}
//跳到修改页面
@RequestMapping("/toUpdate")
public String toUpdate(Integer id, org.springframework.ui.Model model){
RealEseate real = realEseateService.getById(id);
model.addAttribute("real",real);
return "update";
}
//修改
@RequestMapping("/udpate")
public String update(RealEseate user){
realEseateService.updateById(user);
return "redirect:/index";
}
//删除
@RequestMapping("/delete")
public String delete(Integer id){
realEseateService.remove(new QueryWrapper().eq("id", id));
return "redirect:/index";
}
}
前端页面(简单弄弄) html
ye.html
id
projectName
address
houseType
新增
projectName
address
houseType
修改
删除
add.html
update.html