**
**
以springboot项目为基础,所以首先创建springboot项目
pom.xml – (导入相关依赖)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.1.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>mybatis-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>mybatis-demoname>
<description>Demo project for mybatisdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
<scope>providedscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
application.yml – (添加配置)
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: 1234
#配置mybatis映射文件的路径
mybatis:
mapper-locations: classpath*:/mapper/*.xml
server:
port: 8081
TUserMapper.java – (创建mybatis映射接口)
package com.example.mybatis.mapper;
import com.example.mybatis.domain.TUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface TUserMapper{
List<TUser> findAll();
void insert(@Param("user")TUser user);
}
TUserMapper.xml – (在resource包mapper包下创建映射文件)
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis.mapper.TUserMapper">
<resultMap id="BaseResultMap" type="com.example.mybatis.domain.TUser">
<id column="id" property="id" />
<result column="nick_name" property="nickName" />
<result column="company_name" property="companyName" />
<result column="company_id" property="companyId" />
resultMap>
<select id="findAll" resultMap="BaseResultMap">
select * from t_user;
select>
<insert id="insert" parameterType="com.example.mybatis.domain.TUser">
insert into t_user(id,name,nick_name,company_name,company_id,password,status)values (
#{user.id},
#{user.name,jdbcType=VARCHAR},
#{user.nickName,jdbcType=VARCHAR},
#{user.companyName,jdbcType=VARCHAR},
#{user.companyId,jdbcType=VARCHAR},
#{user.password,jdbcType=VARCHAR},
'0'
)
insert>
mapper>
TUser.java – (针对要操作的表创建对应的实体类)
package com.example.mybatis.domain;
import lombok.Data;
/**
* @Classname TUser
* @Description TODO
* @Date 2021/11/2 10:53
* @Created by wang
*/
@Data
public class TUser {
private Integer id;
private String name;
private String nickName;
private String companyName;
private String companyId;
private String password;
private String status;
}
UserController.java – (创建Controller,引入对应的mapper,对外提供接口)
package com.example.mybatis.controller;
import com.example.mybatis.domain.TUser;
import com.example.mybatis.mapper.TUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Classname UserController
* @Description TODO
* @Date 2021/11/2 11:19
* @Created by wang
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private TUserMapper tUserMapper;
@RequestMapping("/findAll")
public List<TUser> findAll(){
return tUserMapper.findAll();
}
@RequestMapping("/insert")
public ResponseEntity<Void> insert(@RequestBody TUser user){
tUserMapper.insert(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}