第一步:创建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. mysql的水平拆分和垂直拆分

    转:http://www.cnblogs.com/sns007/p/5790838.html 1,水平分割: 例:QQ的登录表.假设QQ的用户有100亿,如果只有一张表,每个用户登录的时候数据库都要从 ...

  2. 解决publish不编译问题

    突然发现上午写的博客没了,是我没保存吗?再写一遍 eclipse下将项目部署到tomcat,run之后页面访问出现404,看日志没异常,但出现了No mapping found for HTTP re ...

  3. test for python urllib

    #!/usr/bin/python import urllib2 import time import logging import threading succCount = 0 failCount ...

  4. 【转】5 Best Place to Learn Linux – Linux Tutorial Sites

    Linux have amazed every tech guy and make them curious to hands on Linux. Many of us not feel Linux ...

  5. 【java学习】spring mvc 公共dao的实现,定义基本的增删改查

    接口类: package com.blog.db.dao; import com.blog.util.Pagination; import java.util.List; public interfa ...

  6. Python基础篇(七)

    加上两个下划线变量或者方法变为私有. >>> class Bird: ...    __song = "spark" ...    def sing(self): ...

  7. BZOJ 1982: [Spoj 2021]Moving Pebbles [博弈论 对称]

    给你N堆Stone,两个人玩游戏. 每次任选一堆,首先拿掉至少一个石头,然后移动任意个石子到任意堆中. 谁不能移动了,谁就输了... 以前在poj做过已经忘记了... 构造对称,选最多的一堆往其他堆分 ...

  8. ubuntu14.04安装、NVIDIA显卡驱动安装及CUDA8.0、Cudnn5.1的环境搭建

    安装环境:hp-Z440工作站.64位Ubuntu14.04(64位Ubuntu16.04).Cuda8.0.Cudnn5.1.Nvidia GeForce GT 705.Tesla K40c 本文可 ...

  9. SDN第二次作业

    作业链接 阅读文章<软件定义网络(SDN)研究进展>,并根据所阅读的文章,书写一篇博客,回答以下问题(至少3个): 为什么需要SDN?SDN特点? 随着网络规模的不断扩大,封闭的网络设备内 ...

  10. 【IT人】如何提高阅读源代码的效率

    1.最近刚到公司,公司就发一架构代码自己看,看了几天看的想吐,也在网上找了下相关的技巧吧,不是有句话叫做:成功必有方法,失败总是借口! 2.借鉴别人的方法来看看如下: 记得在开源流行之前,我看过的代码 ...