Spring boot 整合mybatis
第一步:创建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的更多相关文章
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- spring boot 整合 mybatis 以及原理
同上一篇文章一样,spring boot 整合 mybatis过程中没有看见SqlSessionFactory,sqlsession(sqlsessionTemplate),就连在spring框架整合 ...
- Spring Boot 整合mybatis时遇到的mapper接口不能注入的问题
现实情况是这样的,因为在练习spring boot整合mybatis,所以自己新建了个项目做测试,可是在idea里面mapper接口注入报错,后来百度查询了下,把idea的注入等级设置为了warnin ...
- Spring Boot整合Mybatis报错InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider
Spring Boot整合Mybatis时一直报错 后来发现原来主配置类上的MapperScan导错了包 由于我使用了通用Mapper,所以应该导入通用mapper这个包
- Spring Boot整合MyBatis(非注解版)
Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...
- Spring Boot整合Mybatis完成级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- Spring Boot系列(三):Spring Boot整合Mybatis源码解析
一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...
- 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?
Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...
- Spring boot整合Mybatis
时隔两个月的再来写博客的感觉怎么样呢,只能用“棒”来形容了.闲话少说,直接入正题,之前的博客中有说过,将spring与mybatis整个后开发会更爽,基于现在springboot已经成为整个业界开发主 ...
随机推荐
- linux_用户和组
linux用户分为3类: 超级用户:root, UID为0, GID为0 普通用户: 500 -65535, 由root创建 虚拟用户: 1-499 - 系统里傀儡,不能使用,固定存在,满足linux ...
- js_1_变量类型
js中有哪些变量类型? 数字(包括int和float),字符串,数组(字典,js没有字典类型,把字典看成一个对象) 如何把字符转成数字呢? obj.parseInt() // 转化成 ...
- vi使用手册
VI是unix上最常用的文本编辑工具,我自己电脑上面也装了VIM编辑器,这个据称是程序员码字神器我实在没觉得那里舒服了,所以又用回了自己的Sublime.这里整理下vi常用操作,如果以后直接在Linu ...
- Linux指令--chown
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- 【javaweb学习笔记】WEB01_HTML
案例一:网站信息显示页面1.什么是HTML?(Hyper Text Markup Language:超文本标记语言) 超文本:功能比普通文本更加强大 标记语言:使用一组标签对内容进行描述的一门语言(它 ...
- PHP与XML
代码: <?php $dom= new DomDocument('1.0'); $books=$dom->appendChild($dom->createElement_x_x('b ...
- angular中要注意的指令
1.ng-repeat 遍历集合,给每个元素生成模板实例,每个实例的作用域中可以用一些特殊属性,如下: $index //遍历集合的下标 $first //遍历集合中的第一个对象 $last //遍历 ...
- 自动化安装DHCP配置脚本
DHCP配置脚本: #!/bin/sh NET=192.168.6.0 MASK=255.255.255.0 RANGE="192.168.6.50 192.168.6.100" ...
- windows 7 wifi热点配置
自我总结,有什么不足或更好的解决方案,请告知,感激不尽! 目的:闲来无事的童鞋,可以试一试自己配置wifi热点. ps:其实wifi热点配置是系统存在的功能,只不过需要配置. 现在win桌面wifi热 ...
- samephore()信号量跨线程通信
samephore1: #include <stdio.h> #include <stdlib.h> #include <Windows.h> ] = " ...