第一步:创建maven项目并添加spring boot依赖:

<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>1</groupId>
<artifactId>11</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

第二步:添加mybatis依赖

    <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

添加内置的H2数据库

        <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

第三步:创建数据库连接XML文件SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default = "development">
<environment id = "development">
<transactionManager type = "JDBC"/> <dataSource type = "POOLED">
<property name = "driver" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql://192.168.1.6:3306/yuanzhen"/>
<property name = "username" value = "root"/>
<property name = "password" value = "123"/>
</dataSource> </environment>
</environments> </configuration>

第四步:创建数据库:

/*
Navicat MySQL Data Transfer Source Server : yuanzhen
Source Server Version : 50713
Source Host : 192.168.1.6:3306
Source Database : yuanzhen Target Server Type : MYSQL
Target Server Version : 50713
File Encoding : 65001 Date: 2016-08-08 14:52:10
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for STUDENT
-- ----------------------------
DROP TABLE IF EXISTS `STUDENT`;
CREATE TABLE `STUDENT` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) NOT NULL,
`BRANCH` varchar(255) NOT NULL,
`PERCENTAGE` int(3) NOT NULL,
`PHONE` int(11) NOT NULL,
`EMAIL` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of STUDENT
-- ----------------------------
INSERT INTO `STUDENT` VALUES ('1', 'Mohammad', 'It', '80', '984803322', 'Mohammad@gmail.com');
INSERT INTO `STUDENT` VALUES ('2', 'Shyam', 'It', '75', '984800000', 'shyam@gmail.com');
INSERT INTO `STUDENT` VALUES ('3', 'zara', 'EEE', '90', '123412341', 'zara@gmail.com');
INSERT INTO `STUDENT` VALUES ('4', 'zara', 'EEE', '90', '123412341', 'zara@gmail.com');
INSERT INTO `STUDENT` VALUES ('5', 'zara', 'EEE', '90', '123412341', 'zara@gmail.com');
INSERT INTO `STUDENT` VALUES ('6', 'zara', 'EEE', '90', '123412341', 'zara@gmail.com');

第五步:创建

Student.java

package mybatis;

public class Student {
private int id;
private String name;
private String branch;
private int percentage;
private int phone;
private String email; public Student(int id, String name, String branch, int percentage, int phone, String email) {
super();
this.id = id;
this.name = name;
this.branch = branch;
this.percentage = percentage;
this.phone = phone;
this.email = email;
} public Student() {} 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;
} public int getPhone() {
return phone;
} public void setPhone(int phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getBranch() {
return branch;
} public void setBranch(String branch) {
this.branch = branch;
} public int getPercentage() {
return percentage;
} public void setPercentage(int percentage) {
this.percentage = percentage;
} }

Annotations_Example.java

package mybatis;

import java.io.IOException;
import java.io.Reader; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Annotations_Example { public static void main(String args[]) throws IOException{ Reader reader = Resources.getResourceAsReader("mybatis/SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
session.getConfiguration().addMapper(Student_mapper.class); Student_mapper mapper = session.getMapper(Student_mapper.class); //Create a new student object
Student student = new Student(); //Set the values
student.setName("zara");
student.setBranch("EEE");
student.setEmail("zara@gmail.com");
student.setPercentage(90);
student.setPhone(123412341); //Insert student data
mapper.insert(student);
System.out.println("record inserted successfully");
session.commit();
session.close(); } }

Student_mapper.java

package mybatis;

import java.util.List;

import org.apache.ibatis.annotations.*;

public interface Student_mapper {

   final String getAll = "SELECT * FROM STUDENT";
final String getById = "SELECT * FROM STUDENT WHERE ID = #{id}";
final String deleteById = "DELETE from STUDENT WHERE ID = #{id}";
final String insert = "INSERT INTO STUDENT (NAME, BRANCH, PERCENTAGE, PHONE, EMAIL ) VALUES (#{name}, #{branch}, #{percentage}, #{phone}, #{email})";
final String update = "UPDATE STUDENT SET EMAIL = #{email}, NAME = #{name}, BRANCH = #{branch}, PERCENTAGE = #{percentage}, PHONE = #{phone} WHERE ID = #{id}"; @Select(getAll)
@Results(value = {
@Result(property = "id", column = "ID"),
@Result(property = "name", column = "NAME"),
@Result(property = "branch", column = "BRANCH"),
@Result(property = "percentage", column = "PERCENTAGE"),
@Result(property = "phone", column = "PHONE"),
@Result(property = "email", column = "EMAIL")
}) List getAll(); @Select(getById)
@Results(value = {
@Result(property = "id", column = "ID"),
@Result(property = "name", column = "NAME"),
@Result(property = "branch", column = "BRANCH"),
@Result(property = "percentage", column = "PERCENTAGE"),
@Result(property = "phone", column = "PHONE"),
@Result(property = "email", column = "EMAIL")
}) Student getById(int id); @Update(update)
void update(Student student); @Delete(deleteById)
void delete(int id); @Insert(insert)
@Options(useGeneratedKeys = true, keyProperty = "id")
void insert(Student student);
}

下载本项目源码:http://pan.baidu.com/s/1eSFh3Dg

mybatis下载:http://www.java2s.com/Code/Jar/m/Downloadmybatis302jar.htm

注解配置相关链接:mybatishttp://www.tutorialspoint.com/mybatis/mybatis_annotations.htm

spring boot与ibatis整合:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

Spring boot 整合mybatis的更多相关文章

  1. Spring Boot整合Mybatis并完成CRUD操作

    MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...

  2. spring boot 整合 mybatis 以及原理

    同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...

  3. Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题

    现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...

  4. Spring Boot整合Mybatis报错InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider

    Spring Boot整合Mybatis时一直报错 后来发现原来主配置类上的MapperScan导错了包 由于我使用了通用Mapper,所以应该导入通用mapper这个包

  5. Spring Boot整合MyBatis(非注解版)

    Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...

  6. Spring Boot整合Mybatis完成级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  7. Spring Boot系列(三):Spring Boot整合Mybatis源码解析

    一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...

  8. 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?

    Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...

  9. Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...

  10. Spring boot整合Mybatis

    时隔两个月的再来写博客的感觉怎么样呢,只能用“棒”来形容了.闲话少说,直接入正题,之前的博客中有说过,将spring与mybatis整个后开发会更爽,基于现在springboot已经成为整个业界开发主 ...

随机推荐

  1. crontab执行带参数的php脚本,并取得参数[转]

    现在越来越喜欢用linux了,程序当中也去掉了很多触发性判断,改用了借用linux的crontab的特性来进行,这样程序效率确实是高了很多. 比如我们每月1号清空月点击,比如每天凌晨统计上一天的访问报 ...

  2. node.js核心模块

    全局对象 global 是全局变量的宿主 全局变量 在最外层定义的 全局对象的属性 隐士定义的变量(未定义直接赋值的变量) 当定义一个全局变量时 这个变量同时也会成为全局对象的属性 反之亦然 注意: ...

  3. git 签出(恢复)指定文件

    在项目开发中,偶尔会因为误删文件或其他原因需要从git仓库中恢复某些文件.此篇文章将介绍如何通过git从历史提交记录.分支记录恢复指定文件. 1. git checkout 说明:使用git chec ...

  4. maven系列--maven目录

    我们在玩maven,首先就是利用maven来管理我们的项目.其实maven并不难,它无非是一种目录结构.所以在本系列开始之前,我们要细致的了解下maven的目录,其实也就是maven的约定. 约定优于 ...

  5. TCP/IP详解 卷1 第二十章 TCP的成块数据流

    先补充一个知识: 1.停止等待协议:是tcp保证传输可靠的重要途径,"停止等待"就是指发送完一个分组就停止发送,等待对方确认之后,才能继续发送下一个分组 停止等待协议的优点是简单, ...

  6. java基础-静态,非静态(构造)代码块,类加载

    static block and non-static block(constructor block) [toc] 想来想去,先来一题比较好 public class Foo { public st ...

  7. tomcat调优(三)

    标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 1.安全优化 降权启动 telnet管理端口保护 ajp连接端口保护 禁用管理端 关闭本地默认 ...

  8. Hyperledger Fabric Endorsement policies——背书策略

    背书策略 背书策略用于指导peer如何确定交易是否得到了的认可.当一个peer接收到一个事务时,它会调用与事务的Chaincode相关联的VSCC(验证系统链代码),作为事务验证流程的一部分,以确定交 ...

  9. smokeping一键安装脚本

    #!/bin/bash #Date 2017/11/11 #mail caoyf1992@163.com [ $(id -u) != "0" ] && echo & ...

  10. DOM中对象的获得

    DOM的所有对象会在页面打开时,由浏览器页面创建. 浏览器把dom定点对象Document对像的引用交给了window对象. 1.document对象的获得    var doc = window.d ...