Add mybaits and mysql to Maven POM.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Copy the code
Set the mysql address and connection information application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test? serverTimezone=UTC&useUnicode=true&characterEncoding=utf- 8 -&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
server.port=9091
Copy the code
Query the database usermapper.java
import java.util.List;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Results;
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
List<User> getUser(int id);
}
Copy the code
The service layer UserServiceImpl. Java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.stereotype.Service;
import com.example.demo.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUser(int id) {
returnuserMapper.getUser(id); }}Copy the code
UserService.java
import java.util.List;
import com.example.demo.entity.User;
public interface UserService {
List<User> getUser(int id);
}
Copy the code
Accept the request userController.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUser")
public List<User> getUser(@RequestParam("id") int id) {
returnuserService.getUser(id); }}Copy the code
Define the database model user.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String telephone;
public User(a) {// A constructor is required to create a database
}
public int getId(a) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone(a) {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString(a) {
return "User{" +
"id=" + id +
", name='" + name + '\' ' +
", telephone='" + telephone + '\' ' +
'} '; }}Copy the code
Configure boot file SpringDemoApplication. Java
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@MapperScan("com.example.demo.mapper")
public class SpringDemoApplication {
public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); }}Copy the code