springboot mybatis
来源:互联网 发布:常见的网络诈骗有 编辑:程序博客网 时间:2024/05/16 23:58
本例采用springboot+mybtais技术对数据库表进行查询等操作,适合初学者。
结构:
连接配置使用:application.properties application.yml
sql:
CREATE TABLE `person` ( `id` varchar(11) NOT NULL, `name` varchar(255) NOT NULL, `age` int(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mapper:
import tk.mybatis.mapper.common.Mapper;import tk.mybatis.mapper.common.MySqlMapper;/** * Created by Jack on 2017/7/20. */public interface MyMapper<T> extends Mapper<T> , MySqlMapper<T>{ //FIXME 特别注意,该接口不能被扫描到,否则会出错}
/** * Created by Jack on 2017/7/20. */public interface PersonMapper extends MyMapper<Person>{}
service:
import com.github.pagehelper.PageHelper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/** * Created by Jack on 2017/7/20. */@Servicepublic class PersonService { @Autowired private PersonMapper personMapper; public List<Person> getAll(Person person){ if(person.getPage()!=null && person.getRows()!=null){ PageHelper.startPage(person.getPage(),person.getRows()); } return personMapper.selectAll(); }}
controller:
import com.fasterxml.jackson.databind.JavaType;import com.fasterxml.jackson.databind.ObjectMapper;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.List;/** * Created by Jack on 2017/7/20. */@RestController@RequestMapping(value = "/persons")public class PersonController { private Logger logger = Logger.getLogger(PersonController.class); @Autowired private PersonService personService; //获取person所有信息 @GetMapping public Object get(Person person){ List<Person> list = new ArrayList<>(); try { list = personService.getAll(person); } catch (Exception e) { e.printStackTrace(); } return new ResponseEntity<>(new Result(HttpStatus.OK.value(), list, HttpStatus.OK), HttpStatus.OK); } }
在postman模拟请求:
浏览器端:
下载:http://download.csdn.net/detail/carl_jiang/9905322 ,
请注意,使用tomcat8运行本例。
阅读全文
0 0
- springBoot--mybatis
- springboot + mybatis
- mybatis+springboot
- springboot + mybatis
- springboot mybatis
- springboot+mybatis
- Springboot+Mybatis
- springboot-mybatis
- SpringBoot学习:SpringBoot整合mybatis
- springboot(四、springboot+mybatis)
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springBoot集成mybatis
- springboot+mybatis 事务管理
- springboot+mybatis+dubbo整合
- SpringBoot集成mybatis
- springboot+mybatis实现security
- springboot集成mybatis
- 【数据结构】有序树和无序树的区别
- 1.文档流(标准流)、浮动布局
- 原型继承1.2.8
- ubuntu 任务栏 放到下面
- RHEL版本与内核对应表
- springboot mybatis
- Android_Data_Binding-----------------(1)
- 【PAT】【Advanced Level】1028. List Sorting (25)
- Inteface Buidler can't determine the type of "xx.xib" this may be due to a missing SDK.
- QT多线程同步 互斥缩 信号量 读写所 ...
- java使用泛型
- hdu1498—50 years, 50 colors(最小点覆盖)
- 极光推送
- javascript