1、创建项目 引入对应的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
2、配置数据库和Mapper映射 application.yml
server:
port: 8088
spring:
datasource:
password: 12345678
username: root
url: jdbc:mysql://localhost:3306/zzrTest?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
redis:
port: 6379
host: 127.0.0.1
mybatis:
mapper-locations: classpath:mapper/*.xml
在启动类上面加上MapperScan注解
@SpringBootApplication
@MapperScan("com.example.mybatis.demos.web.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
3、写对应的Mapper接口 (注解方式)
package com.example.mybatis.demos.web.mapper;
import com.example.mybatis.demos.web.User;
import org.apache.ibatis.annotations.*;
import java.security.PrivateKey;
import java.util.List;
@Mapper
public interface UserMapper {
String TABLE="user"; // 表名
String SelectColum="id,name,age";
String InsertColumn="name,age";
@Insert("insert into "+TABLE+" ("+InsertColumn+" )" +"values (#{name},#{age} )")
int insertUser(User user);
@Results({@Result(property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "age",column = "age"),
@Result(property = "createTime",column = "create_time"),
@Result(property = "updateTime",column = "update_time"),
})
//@Lang(MybatisExtendedLanguageDriver.class)
@Select("select "+SelectColum+" from "+TABLE)
List<User> listUser();
@Update("update "+TABLE +" set name=#{name} where id=#{id}")
int updateUser(User user);
/**
* 更新指定的字段
* */
@Update("update user set name=#{userName} where id=#{userId}")
int updateUser2(@Param("userName")String userName,@Param("userId")Integer userId);
@Update("update "+TABLE +" set "+
" name=#{name},"+
" age=#{age},"+
" update_time=#{updateTime}" +
" where id=#{id}")
int updateUser3(User user) ;
}
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.example.mybatis.demos.web.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.example.mybatis.demos.web.model.Student">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="age" jdbcType="BIGINT" property="age"/>
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
</resultMap>
<!--基础查询字段 -->
<sql id="Base_Column_List">
id, name, age, create_time, update_time
</sql>
<select id="getStudent" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from student
<!-- where id = #{id,jdbcType=BIGINT}-->
</select>
</mapper>