使用eclipse 简单的搭建一个 springboot 整合mybatis
或者使用ctrl + n
里面下面部分全部要选中,这里没有截图了
个人是比较喜欢yml 这个格式的 里面是可以配置一些属性 注意:配置文件只能application*.properties 或者 application*.yml 或者application*.yaml 如果属性相同那么后面的配置文件会覆盖前面的属性
我们需要导入mybatis的maven坐标
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
配置到pom.xml中
现在配置一下yml的配置
注意这个是个文件夹一定不要选错了 要不然会爆 接口为绑定映射错误 文件夹名称为 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();
}
}
直接运行引导类
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