1.添加依赖
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
2.写springboot配置文件
我这里连接的是阿里云的数据库
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://公网地址.mysql.rds.aliyuncs.com/数据库名称?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: 用户名
password: 密码
3.写接口类和查询语句的xml文档
// 接口类
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
<!-- 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="cn.edu.njtech.mapper.UserMapper">
<resultMap id="BaseResultMap" type="cn.edu.njtech.domain.dao.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="authority" jdbcType="TINYINT" property="authority" />
<result column="status" jdbcType="TINYINT" property="status" />
</resultMap>
<sql id="Base_Column_List">
id, user_id, password, authority, status
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="cn.edu.njtech.domain.dao.User">
insert into t_user (id, user_id, password,
authority, status)
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{authority,jdbcType=TINYINT}, #{status,jdbcType=TINYINT})
</insert>
<insert id="insertSelective" parameterType="cn.edu.njtech.domain.dao.User">
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userId != null">
user_id,
</if>
<if test="password != null">
password,
</if>
<if test="authority != null">
authority,
</if>
<if test="status != null">
status,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="userId != null">
#{userId,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="authority != null">
#{authority,jdbcType=TINYINT},
</if>
<if test="status != null">
#{status,jdbcType=TINYINT},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="cn.edu.njtech.domain.dao.User">
update t_user
<set>
<if test="userId != null">
user_id = #{userId,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="authority != null">
authority = #{authority,jdbcType=TINYINT},
</if>
<if test="status != null">
status = #{status,jdbcType=TINYINT},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="cn.edu.njtech.domain.dao.User">
update t_user
set user_id = #{userId,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
authority = #{authority,jdbcType=TINYINT},
status = #{status,jdbcType=TINYINT}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
4.在springboot启动类上面添加注解@MapperScan(basePackages = “*****”)
@SpringBootApplication
@MapperScan(basePackages = "cn.edu.njtech.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}