SpringBoot and 2. SpringBoot integration Mybatis examples in Java
Sep 16, 2017 am 10:08 AM1. Create the Springboot configuration file: application.properties, and add the MyBatis dependency
SpringApplication will start from
application.properties
Load configuration information in the file. The following is the file directory sequence for adding Spring configuration information:
In the /config subdirectory under the current directory
In the current directory
In the
/config
directory under a classpath package-
classpath root directory
You can follow your own habits.
(1) application.properties file configuration is as follows:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1/test spring.datasource.username=root spring.datasource.password=root
(2) Add MyBatis and MYSQL
<!-- SpringBoot - MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2. Create POJO, Mapper, Service and Controller
(1)User.java
package org.rcddup.app.domain;public class User { private Long id; private String name; private Short age; private Short status; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } public Short getStatus() { return status; } public void setStatus(Short status) { this.status = status; } }
(2)UserMapper .java
package org.rcddup.app.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.rcddup.app.domain.User; @Mapperpublic interface UserMapper { @Select("select id, name, age, status from user") List<User> listUser(); @Select("select id, name, age, status from user where id=#{id}") User getUserById(Long id); }
(3) IUserService.java
package org.rcddup.app.service; import java.util.List; import org.rcddup.app.domain.User; public interface IUserService { List<User> listUser(); User getUserById(Long id); }
(4) UserService.java
package org.rcddup.app.service.impl; import java.util.List; import org.rcddup.app.dao.UserMapper; import org.rcddup.app.domain.User; import org.rcddup.app.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Servicepublic class UserService implements IUserService{ @Autowired private UserMapper userMapper; @Override public List<User> listUser() { return userMapper.listUser(); } @Override public User getUserById(Long id) { return userMapper.getUserById(id); } }
(5) UserController.java
package org.rcddup.app.web.controller; import java.util.List; import org.rcddup.app.domain.User; import org.rcddup.app.service.IUserService;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user")public class UserController { @Autowired private IUserService userService; @RequestMapping("/list") public List<User> list() { return userService.listUser(); } @RequestMapping("/get") public User list(Long id) { return userService.getUserById(id); } }
Restart App.java
Enter: http:// /localhost:8080/user/get?id=1
Input: http://localhost:8080/user/list
At this point, SpringBoot has successfully integrated Mybatis.
The above is the detailed content of SpringBoot and 2. SpringBoot integration Mybatis examples in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

iBatis vs. MyBatis: Which one is better for you?

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

Various ways to implement batch deletion operations in MyBatis

Comparative analysis of the functions and performance of JPA and MyBatis

Detailed explanation of how to use MyBatis batch delete statements

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency?

MyBatis Generator configuration parameter interpretation and best practices

Analyze the caching mechanism of MyBatis: compare the characteristics and usage of first-level cache and second-level cache
