第一步:创建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/