Springboot整合Mybatis

一 数据库

数据库大家自己去创建就可以了,按照大家的实际情况来写sql语句和数据库的账号,密码


二 目录结构

Springboot整合Mybatis_第1张图片


三 配置pom.xml文件

新建maven文件并配饰pom.xml文件


	org.springframework.boot
	spring-boot-starter-parent
	1.5.9.RELEASE



	
		org.springframework.boot
		spring-boot-starter-web
	

	
		org.springframework.boot
		spring-boot-starter-tomcat
	

	
		junit
		junit
		3.8.1
		test
	

	
		javax.servlet
		javax.servlet-api
	

	
		javax.servlet
		jstl
	

	
		org.apache.tomcat.embed
		tomcat-embed-jasper
	

	
		org.springframework.boot
		spring-boot-devtools
		true
	

	
		org.mybatis.spring.boot
		mybatis-spring-boot-starter
		1.1.1
	

	
		mysql
		mysql-connector-java
		5.1.21
	




	1.8



	
		
			org.springframework.boot
			spring-boot-maven-plugin
		
	


四 创建对应的类

User.java

package com.yuyi.pojo;

import java.util.Date;

public class User {

	private Integer id;
	private String username;
	private Date birthday;
	private Integer sex; 
	private String  address;
	private String  uuuid;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Integer getSex() {
		return sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getUuuid() {
		return uuuid;
	}
	public void setUuuid(String uuuid) {
		this.uuuid = uuuid;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday=" + birthday + ", sex=" + sex + ", address="
				+ address + ", uuuid=" + uuuid + "]";
	}
	
	
}

UserMapper.java

package com.yuyi.dao;

import org.apache.ibatis.annotations.Mapper;

import com.yuyi.pojo.User;

@Mapper
public interface UserMapper {

	User getUserById(int id);
}

UserMapper.xml



    
    
    
UserMapper.java
package com.yuyi.dao;

import org.apache.ibatis.annotations.Mapper;

import com.yuyi.pojo.User;

@Mapper
public interface UserMapper {

	User getUserById(int id);
}

UserService.java

package com.yuyi.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyi.dao.UserMapper;
import com.yuyi.pojo.User;
@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;
	
	public User getUserById(int id){
		User user=new User();
		user=userMapper.getUserById(id);
		return  user;
				
	}
}

UserController.java

package com.yuyi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyi.pojo.User;
import com.yuyi.service.UserService;

@RestController
public class UserController {

	@Autowired
	UserService userService;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public User getUserById(@PathVariable("id") int id) {
		
		User user=userService.getUserById(id);
		System.out.println(user);
		return userService.getUserById(id);
	}
}

Application.java

package com.yuyi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
	SpringApplication.run(Application.class, args);
}
}

application.properties

spring.datasource.username=root
spring.datasource.password=628081
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///mybatis

mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.yuyi.pojo

测试

Springboot整合Mybatis_第2张图片

你可能感兴趣的:(Springboot,Springboot,Mybatis)