第一步:创建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. 解决service层无法注入

    练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...

  2. 转-Windows路由表配置:双网卡路由分流

    原文链接:http://www.cnblogs.com/lightnear/archive/2013/02/03/2890835.html 一.windows 路由表解释 route print -4 ...

  3. 在 ios 中的日期格式

    var d="2017-1-1" ; new Date(d) //生成一个日期对象 这样写在 Android 中没有问题,但是在 ios 中,d  的格式不对,应该设为 2017- ...

  4. 完美解决打开github速度慢的问题

    摘抄自知乎. 修改hosts(HOSTS文件路径:C:\Windows\System32\drivers\etc\hosts) 1.打开Dns检测|Dns查询 - 站长工具 2.在检测输入栏中输入ht ...

  5. Centos7.3下mysql5.7.18安装并修改初始密码的方法

    Centos7.3下mysql5.7.18安装并修改初始密码的方法 原文链接:http://www.jb51.net/article/116032.htm 作者:Javen205 字体:[增加 减小] ...

  6. 【转】shell字符串截取

    shell字符串的截取的问题: 一.Linux shell 截取字符变量的前8位,有方法如下: 1.expr substr “$a” 1 8 2.echo $a|awk ‘{print substr( ...

  7. mysql与nagios的结合使用

    一. 对mysql建库建表,并测试数据 基本信息:库名:nh_nagios表名:nagios_alerts [root@nhserver2 ~]# mysql -u root -pEnter pass ...

  8. 【转】nagios使用带url的check_http检测主机

    前一段时间在Cu论坛发现一个提问,问题是nagios关于检测主机http服务的.原帖地址http://bbs.chinaunix.net /forum.php?mod=viewthread&t ...

  9. 华硕笔记本电脑Win10改Win7设置U盘启动

    华硕笔记本电脑Win10改Win7设置U盘启动 尝试开机按ESC选择前面没有UEFI项的USB启动: 1,在BIOS设置里advanced菜单,把 Lgeacy USB support选择为enabl ...

  10. Oracle 修改表操作

    如题:    --增加列操作: alert table 表名 add 列名 列的类型 eg:alter table EMP1 add pwd varchar2(10); --删除列操作: alert ...