Spring boot笔记5

 整合MyBatis

1. 加入依赖

需要在pom.xml加入以下依赖

com.github.abel533

mapper

2.3.4

com.github.pagehelper

pagehelper

3.7.5

com.github.jsqlparser

jsqlparser

0.9.1


2. 修改配置文件

在application.properties添加配置

#spring集成Mybatis环境

#pojo别名扫描包

mybatis.type-aliases-package=cn.cnn.info.pojo

mybatis.mapper-locations=classpath:mapper/*Mapper.xml

mybatis.config-location=classpath:mybatis/SqlMapConfig.xml

#spring.datasource.type=com.jolbox.bonecp.BoneCPDataSource


在src\main\resources\mapper路径下加入UserMapper.xml配置文件

select * from user


加载通用Mapper和分页助手

  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  "http://mybatis.org/dtd/mybatis-3-config.dtd">




3. 编写Mapper

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Select;

import cn.cnn.info.pojo.User;


//extends com.github.abel533.mapper.Mapper:需要继承通用Mapper

@Mapper

public interface UserMapper extends com.github.abel533.mapper.Mapper {


@Select("select * from user where name like '%${value}%'")

public List queryUserByName(String name);


// 使用UserMapper.xml配置文件

public List queryAll();

}


4. 编写Service和Controller

Service编写

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import cn.cnn.info.dao.UserDao;

import cn.cnn.info.dao.UserMapper;

import cn.cnn.info.pojo.User;

import cn.cnn.info.service.UserService;


@Service

public class UserServiceImpl implements UserService {


@Autowired

private UserDao userDao;


@Autowired

private UserMapper userMapper;


@Override

public List findAll() {

List list = this.userDao.findAll();

return list;

}


@Override

public List queryUserByName(String name) {

List list = this.userMapper.queryUserByName(name);

return list;

}


// 调用使用UserMapper.xml的Mapper

@Override

public List queryAll() {

List list = this.userMapper.queryAll();

return list;

}


// 使用通用Mapper和分页助手

@Override

public List queryUserByPage(Integer page, Integer rows) {

// 设置分页

PageHelper.startPage(page, rows);

// 查询数据

List list = this.userMapper.select(null);

return list;

}

}


Controller编写

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import cn.cnn.info.pojo.User;

import cn.cnn.info.service.UserService;


@RestController

@RequestMapping("user")

public class UserControlelr {


@Autowired

private UserService userService;


@RequestMapping("list")

public List queryUserAll() {

List list = this.userService.findAll();

return list;

}


@RequestMapping("list/{name}")

public List queryUserAll(@PathVariable String name) {

List list = this.userService.queryUserByName(name);

return list;

}


@RequestMapping("list/query")

public List queryUserAll2() {

List list = this.userService.queryAll();

return list;

}


@RequestMapping("list/{page}/{rows}")

public List queryUserAll(@PathVariable Integer page, @PathVariable Integer rows) {

List list = this.userService.queryUserByPage(page, rows);

return list;

}


浏览器地址栏输入:http://127.0.0.1:8080/user/list/query

你可能感兴趣的:(Spring boot笔记5)