天天看點

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

使用eclipse 簡單的搭建一個 springboot  整合mybatis

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

或者使用ctrl + n

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

裡面下面部分全部要選中,這裡沒有截圖了

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

個人是比較喜歡yml 這個格式的 裡面是可以配置一些屬性    注意:配置檔案隻能application*.properties   或者  application*.yml  或者application*.yaml  如果屬性相同那麼後面的配置檔案會覆寫前面的屬性

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

我們需要導入mybatis的maven坐标  

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.1.1</version>
</dependency>
           

配置到pom.xml中

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

現在配置一下yml的配置

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

注意這個是個檔案夾一定不要選錯了 要不然會爆  接口為綁定映射錯誤 檔案夾名稱為 mapping   在檔案夾裡面建立一個跟資料通路層名稱一緻的xml  我的資料通路層是 UserMapper.java   那麼這裡就是 UserMapper.xml

/**
 * 
 */
package com.he.pojo;

import java.io.Serializable;

/**@Title:  User.java
 * @author  hexiaofeng
 * @date  2019年7月24日 下午9:11:06
 * @Description: 使用者的實體類
 */
public class User implements Serializable {
	/**
	 * 使用者ID
	 */
	private String userId;
	/**
	 * 使用者名稱
	 */
	private String userName;
	/**
	 * 使用者位址
	 */
	private String addrese;
	/**
	 * 使用者年齡
	 */
	private Integer age;
	/**
	 * 使用者密碼
	 */
	private String password;
	/**
	 * @return the userId
	 */
	public String getUserId() {
		return userId;
	}
	/**
	 * @param userId the userId to set
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * @return the address
	 */
	public String getAddress() {
		return addrese;
	}
	/**
	 * @param address the address to set
	 */
	public void setAddress(String addrese) {
		this.addrese = addrese;
	}
	/**
	 * @return the age
	 */
	public Integer getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(Integer age) {
		this.age = age;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
}
           
package com.he.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.he.pojo.User;

/**@Title:  UserMapper.java
 * @author  hexiaofeng
 * @date  2019年7月24日 下午9:15:42
 * @Description: 使用者接口類   注意一定要加mapper注解
 */
@Mapper
public interface UserMapper {
	List<User> findAllUser();
}
           
<!-- UserMapper.xml 檔案-->
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.he.mapper.UserMapper">
	<resultMap type="User" id="resultMap">
		<id column="user_id" property="userId" javaType="String" />
		<result column="user_name" property="userName"
			javaType="String" />
		<result column="addrese" property="addrese" javaType="String" />
		<result column="age" property="age" javaType="Integer" />
		<result column="password" property="password" javaType="String" />
	</resultMap>
	<select id="findAllUser" resultMap="resultMap">
		select  user_id , user_name , addrese , age ,password from user
	</select>
</mapper>
           
/**
 * 
 */
package com.he.controller;


import java.util.List;

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

import com.he.mapper.UserMapper;
import com.he.pojo.User;

/**@Title:  UserController.java
 * @author  hexiaofeng
 * @date  2019年7月24日 下午9:23:20
 * @Description: 使用者的控制層
 */
@RestController
@RequestMapping("/user")
public class UserController {
	/**
	 * 依賴注入
	 */
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 查詢所有的使用者
	 */
	@GetMapping("/list")
	public List<User> list(){
		System.out.println(userMapper);
		return userMapper.findAllUser();
				
	}
}
           

直接運作引導類

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++
CREATE TABLE `user` (
  `user_id` varchar(50) NOT NULL,
  `user_name` varchar(50) DEFAULT NULL,
  `addrese` varchar(100) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
           

最後可以通路  http://localhost:8080/user/list

eclipse 搭建 spring boot 整合 mybatis +++++++++++++++++++++

繼續閱讀