springboot整合mybatis(注解)
springboot整合mybatis(注解)
1.pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example.mapper</groupId>
<artifactId>springboot_mybatis</artifactId>
<version>1.0</version>
<packaging>jar</packaging> <name>springboot_mybatis</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.sql:
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`age` int(10) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`role` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`phone` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8
4.User:
package com.example.mapper.mybatisMap.entity; /**
* @author:
* @date: 2018/8/13
* @description:
*/
public class User {
private int id;
private int age;
private String name;
private String role;
private String email;
private String phone; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getRole() {
return role;
} public void setRole(String role) {
this.role = role;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
}
}
5.UserMapper:
package com.example.mapper.mybatisMap.dao; import com.example.mapper.mybatisMap.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; /**
* @author:
* @date: 2018/8/13
* @description:
*/
@Mapper
public interface UserMapper { @Select("SELECT * FROM USER WHERE NAME = #{name}")
User findByName(@Param("name") String name); @Insert("INSERT INTO USER(NAME, AGE, ID) VALUES(#{name}, #{age}, #{id})")
int insert(@Param("name") String name, @Param("age") Integer age, @Param("id") Integer id); }
6.Controller:
package com.example.mapper.mybatisMap.controller; import com.example.mapper.mybatisMap.dao.UserMapper;
import com.example.mapper.mybatisMap.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author:
* @date: 2018/8/13
* @description:
*/
@RestController
public class Controller { @Autowired
UserMapper userMapper; private Logger logger = LoggerFactory.getLogger(Controller.class); /**
* 查询数据
*/
@RequestMapping("/get")
public User getName(HttpServletRequest request
, HttpServletResponse response
, @RequestParam(value="name") String name){
User user = new User();
user = userMapper.findByName(name);
return user;
} /**
* 插入数据
*/
@RequestMapping("/insert")
public String insertName(HttpServletRequest request, HttpServletResponse response){
String name = request.getParameter("name");
int age = Integer.valueOf(request.getParameter("age"));
int id = Integer.valueOf(request.getParameter("id"));
int number = userMapper.insert(name, age, id);
return "插入数据:" + number + "条";
} }
7.MybatisMapApplication:
package com.example.mapper.mybatisMap; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class MybatisMapApplication { public static void main(String[] args) {
SpringApplication.run(MybatisMapApplication.class, args);
}
}
8.访问url:
http://localhost:8080/get?name=迪丽热巴 → {"id":6,"age":25,"name":"迪丽热巴","role":"浙江路93号-18-1","email":"88xja7gs@hotmail.com","phone":"15107232275"}
http://localhost:8080/insert?id=22&name=迪丽热巴&age=25 → 插入数据:1条
声明:本文为作者原创,转载需声明!
springboot整合mybatis(注解)的更多相关文章
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- SpringBoot整合MyBatis(注解版)
详情可以参考Mybatis官方文档 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ (1). ...
- SpringBoot整合Mybatis之Annotation
首先需要下载前面一篇文章的代码,在前一章代码上进行修改. SpringBoot整合Mybatis(注解方式) 复制前一个项目,修改配置文件,mybatis的相关配置为: mybatis: type-a ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- SpringBoot整合Mybatis多数据源 (AOP+注解)
SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...
- SpringBoot 2.x (9):整合Mybatis注解实战
SSM框架再熟悉不过了,不过以前通常是使用XML写SQL语句 这里用SpringBoot整合Mybatis并且使用注解进行开发 依赖: <!-- Mybatis --> <depen ...
- SpringBoot数据访问之整合mybatis注解版
SpringBoot数据访问之整合mybatis注解版 mybatis注解版: 贴心链接:Github 在网页下方,找到快速开始文档 上述链接方便读者查找. 通过快速开始文档,搭建环境: 创建数据库: ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
随机推荐
- 修改 jupyter notebook的默认文件夹位置
安装好Anaconda 3以后,就可以使用Jupyter notebook了,但是我们打开Jupyter notebook后,发现界面是一个默认的目录,这个目录在哪里?如果想把自己写的程序文件保存在自 ...
- Jersey用户指南学习笔记1
Jersey用户指南是Jersey的官方文档, 英文原版在这:https://jersey.github.io/documentation/latest/index.html 中文翻译版在这:http ...
- powerdesign进军(一)--安装破解
目录 资源下载地址 安装powerdesign 破解powerdesign 汉化 总结 IT行业不管是web开发还是客户端开发都需要数据库,因为现在是数据时代能够拥有强大的数据就是行业的王者.目前一些 ...
- 工作中常见的五种技术leader
力不从心型 在工作中有种技术leader,总认为自己是最好的.在方案设计的时候,自己有一种方案,下属有一种方案.leader非要别人听他的.如果两种方案没有优劣之分,比较建议的做法是让真正实施的人按照 ...
- Java相关|Code Review Checklist(Server)
安全 所有入参均经过校验,包括验证参数数据类型.范围.长度,尽可能采用白名单形式验证所有的输入.对于非法请求,记录WARN log.参考Input Validation Cheat Sheet:前后端 ...
- 讲解开源项目:5分钟搭建私人Java博客系统
本文适合刚学习完 Java 语言基础的人群,跟着本文可了解和运行 Tale 项目.示例均在 Windows 操作系统下演示 本文作者:HelloGitHub-秦人 HelloGitHub 推出的< ...
- Spring参数的自解析--还在自己转换?你out了!
背景前段时间开发一个接口,因为调用我接口的同事脾气特别好,我也就不客气,我就直接把源代码发给他当接口定义了. 没想到同事看到我的代码问:要么 get a,b,c 要么 post [a,b,c]. ...
- 携程PMO--如何召开卓有成效的回顾会
话题介绍 回顾会提供团队反思迭代过程并提出改进措施的机会.回顾会是团队成员共同进行的协作活动,让团队成员跟进并落实改进措施,使团队在下一个冲刺中更高效,这是相当重要的. 我们给出了回顾会的 ...
- 使用docker快速搭建本地环境
在平时的开发中工作中,环境的搭建其实一直都是一个很麻烦的事情 特别是现在,系统越来越复杂,所需要连接的一些中间件也越来越多. 然而要顺利的安装好这些中间件也是一个比较费时费力的工作. 俗话说" ...
- 第一个基于ArcGIS的Android应用
使用Android Studio创建第一个工程 打开Android Studio,新建工程.在Application name处填写项目名称,company domain是公司地址,将来作为包名,点 ...