SpringBoot and MyBatis and MySQL, the hello world example
Introduction
This post would demo how to run a hello world example on springboot+MyBatis+MySQL.
Environments
- SpringBoot 1.5.12
- MySQL 5.1.38
- Java 1.8
- mybatis-spring-boot-starter 1.3.2
The Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<artifactId>test-springboot-mybatis</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>Main</start-class>
<mysql.version>5.1.38</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.12.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
The database table:
CREATE TABLE `tbl_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(10) NOT NULL,
`EMAIL` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And insert a record named **jack** for test.
The Classes
Springboot Main entry class:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
The domain class:
public class Student {
private int id;
private String name;
private String branch;
private int percentage;
private int phone;
private String email;
...
The Mapper interface:
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM tbl_student WHERE id = #{id}")
Student findById(@Param("id") int id);
}
Here we use a @Mapper to annotate an interface named StudentMapper, which has a method findById, this method is annotated by @Select with a sql statement, just like traditional MyBatis mapper files, but when you use springboot, you don’t need xml now.
The testcase
Here we write a junit test case to test the dao:
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestStudentDao {
@Autowired
private StudentMapper studentMapper;
@Test
public void testHello1() {
Student s = studentMapper.findById(1);
System.out.println("student name:"+s.getName());//for console output
assertNotNull(s);
}
}
As you can see, we just @Autowired the studentMapper, and then use the findById method, then we run the test, we got the green bar and the result:
student name:jack
It’s so easy, do you think so? we would continue to supply more complicated examples some days later.
You can find detail documents about the springboot and unit testing here: