1、SpringBoot+Mybatis整合------简单CRUD的实现
编译工具:STS
代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9e5c095ded585c90566
一、项目的建立
选择依赖:Mybatis,Web,MySql,JDBC
SpringBoot版本:2.0.3
项目生成结构:
pom依赖:
<?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.xm</groupId>
<artifactId>demo005_Mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo005_Mybatis</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.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-jdbc</artifactId>
</dependency>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
pom.xml
二、创建数据库
三、项目配置
1.添加yml配置文件:application.yml
#配置mybatis
mybatis:
#配置xml映射路径
mapper-locations: classpath:mapper/*.xml
#配置实体类的别名
type-aliases-package: com.xm.pojo
configuration:
#开启驼峰命名法
map-underscore-to-camel-case: true #配置mysql连接
spring:
datasource:
url: jdbc:mysql://10.1.51.31:3306/xm
username: root
password: cube1501
driver-class-name: com.mysql.jdbc.Driver
application.yml
2.在source文件夹下建立mapper文件夹
3.在Springboot启动类添加@MapperScan注解
package com.xm; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(value="com.xm.mapper")
@SpringBootApplication
public class Demo005MybatisApplication { public static void main(String[] args) {
SpringApplication.run(Demo005MybatisApplication.class, args);
}
}
Demo005MybatisApplication.java
注意:添加@MapperScan注解后,每个mapper都会自动扫描成为Bean。否则,需要在每个mapper接口上添加@Mapper接口
四、代码实现
1.实体类Student
package com.xm.pojo; /**
* name:学生实体
* @author xxm
*
*/
public class Student {
/**
* content:主键id
*/
private int id;
/**
* content:姓名
*/
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
Student.java
2.数据操作层StudentMapper
package com.xm.mapper; import java.util.List; import com.xm.pojo.Student; public interface StudentMapper { /**
* 根据id查询
* @param id
* @return
*/
public Student getById(Integer id); /**
* 查询全部
* @return
*/
public List<Student> list(); /**
* 插入
* @param student
*/
public void insert(Student student); /**
* 根据student的id修改
* @param student
*/
public void update(Student student); /**
* 根据id删除
* @param id
*/
public void delete(Integer id); }
StudentMapper.java
3.mapper映射Studentmapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xm.mapper.StudentMapper"> <!-- 根据id查询 -->
<select id="getById" parameterType="int" resultType="student">
select * from student where id=#{id}
</select> <!-- 查询所有 -->
<select id="list" parameterType="int" resultType="student">
select * from student
</select> <!-- 插入一个学生 -->
<insert id="insert" parameterType="student">
insert into student(name) values(#{name})
</insert> <!-- 根据id修改学生信息 -->
<update id="update" parameterType="student">
update student set name=#{name} where id=#{id}
</update> <!-- 根据id删除学生 -->
<delete id="delete" parameterType="int">
delete from student where id=#{id}
</delete>
</mapper>
StudentMapper.xml
4.控制层StudentController
package com.xm.controller; import java.util.List; import javax.websocket.server.PathParam; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController; import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RestController
public class StudentController {
@Autowired
private StudentMapper studentMapper; /**
* 根据id查询学生
* @param id
* @return
*/
@GetMapping("/student/{id}")
public Student getById(@PathVariable("id") Integer id) { Student student = studentMapper.getById(id);
return student; } /**
* 查询全部
* @return
*/
@GetMapping("/students")
public List<Student> list(){
List<Student> students = studentMapper.list();
return students;
} /**
* 插入
* @param student
*/
@PostMapping("/student")
public void insert( Student student) {
studentMapper.insert(student);
} /**
* 修改
* @param student
*/
@PutMapping("/student/{id}")
public void update(Student student,@PathVariable("id")Integer id) {
studentMapper.update(student);
} /**
* 根据id删除
* @param id
*/
@DeleteMapping("/student/{id}")
public void delete(@PathVariable("id") Integer id) {
studentMapper.delete(id);
} }
StudentController.java
2018-06-1517:31:14
1、SpringBoot+Mybatis整合------简单CRUD的实现的更多相关文章
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- SpringBoot第十一篇:SpringBoot+MyBatis+Thymelaf实现CRUD
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10936304.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 总结前面几 ...
- SpringBoot+Mybatis整合入门(一)
SpringBoot+Mybatis 四步整合 第一步 添加依赖 springBoot+Mybatis相关依赖 <!--springBoot相关--> <parent> < ...
- 7、SpringBoot+Mybatis整合------PageHelper简单分页
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/1d30d2a573ce6784149a28af9b ...
- SpringBoot+Mybatis整合实例
前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...
- 2、SpringBoot+Mybatis整合------一对一
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/93398da60c647573645917b2 ...
- Vue+SpringBoot+Mybatis的简单员工管理项目
本文项目参考自:https://github.com/boylegu/SpringBoot-vue 为了完成此项目你需要会springBoot,mybatis的一些基本操作 运行界面 第一步:搭建前端 ...
- springboot/Mybatis整合
正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper ...
- springboot+mybatis整合(单元测试,异常处理,日志管理,AOP)
我用的事IDEA,jdk版本是1.7.新建项目的时候这个地方的选择需要注意一下,springboot版本是1.5的,否则不支持1.7的jdk pom.xml <dependency> &l ...
随机推荐
- Mybatis学习笔记2 - 解析config
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- maya 安装失败/出错/卸载 2018/2017/2016/2015/2013/2012
AUTO Uninstaller 更新下载地址 1.选择软件 2.选择版本 3.点击[开始卸载]
- SQL内外连接的区别
项目当中,需要将SQL server中的部分数据导入mongo中,由于SQL是关系型数据库的原因,需要联合多表进行查询,因此,了解了下SQL中内外连接的相关概念,以作备注: 1.内联接(典型的联接运算 ...
- jqGrid方法整理
一.colModel 表体结构配置 name 必要的属性,具有唯一标识性,如在弹出的editform窗体中,将作为input的name属性 index 为排序用,最方便的是设为数据库字段 ...
- siteserver学习笔记
1.安装 安装前的准备工作 参考https://docs.siteserver.cn/getting-started/#/how-to-install-siteserver-cms官网的文档写的很详细 ...
- jq学习总结之方法
三.方法 1.length 2.index()3.get() reverse()4.not()5.filter()6.find()7.each()8.addBack()9.attr()10.toggl ...
- JetBrains IDE激活
License server(服务器地址为http://idea.iteblog.com/key.php) Active Code:生成网址:http://idea.iteblog.com/
- SpringBoot JPA注解详解
1.@OneToOne 2.@OneToManytargetEntity: 默认关联的实体类型.如果集合类中指定了具体类型了,不需要使用targetEntity.否则需要targetEntity指定C ...
- mybatis问题记录
问题:2019年3月29日 23:46:24 org.apache.ibatis.builder.IncompleteElementException: Could not find result m ...
- css 两大特性:继承性和层叠性
css 有两大特性: 继承性和层叠性, 继承性 面向对象语言都会存在继承的概念,在面向对象的语言中,继承的特点:继承了父类的属性和方法.那么我们现在主要研究css,css中没有方法,所以我们仅仅继承属 ...