Spring Boot 整合 Mybatis (一) mybatis-spring-boot-starter

mybatis-spring-boot-autoconfigurehttp://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html

mybatis:http://www.mybatis.org/mybatis-3/zh/index.html

1.maven依赖



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.3.RELEASE
		 
	
	priv.gitonlie.mybatis
	mybatis
	0.0.1-SNAPSHOT
	springboot-mybatis
	mybatis project for Spring Boot

	
		1.8
		1.3.2
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			${mybatis.springboot.version}
		
		
		
			mysql
			mysql-connector-java
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
		        org.apache.maven.plugins
		        maven-surefire-plugin
		        
		          true
		        
		     
		
	


2.application.yml配置

spring:
  application:
    name: mybatis-server
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/flowable_demo?useUnicode=true&characterEncoding=utf-8
    username: root
    password: admin123456
 
server:
  port: 8081

mybatis:
  type-aliases-package: priv.gitonlie.mybatis.entity
  mapper-locations:
  - classpath:mapper/*.xml

3.实体类

public class User {
	
	private String id;
	
	private String name;
	
	private String password;
	
	private String token;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

}

4.Mapper接口

public interface UserMapper {
	
	List queryAllUser();
}

5.mapper.xml




	

6.service服务层

@Service
public class UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	public List queryAll(){
		return userMapper.queryAllUser();
	}
}

7.controller控制层

@RestController
public class UserController {
	
	@Autowired
	private UserService service;
	
	@GetMapping("demo")
	public List queryAll(){
		List list = service.queryAll();
		return list;
	}
}

8.启动访问

你可能感兴趣的:(java,java,后端)