添加依赖
mysql;数据源druid;mybatis
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
配置文件 application.properties
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=net.img.entity
#mybatis.type-handlers-package=com.example.typehandler
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30
mybatis.executor-type=REUSE
配置文件 application-dev.properties
配置开发环境的数据库账号密码
spring.datasource.druid.url=jdbc:mysql://localhost/blog
spring.datasource.druid.username=begincode
spring.datasource.druid.password=begincode123
#spring.datasource.druid.driver-class-name= #æ spring.datasource.driver-class-name=
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
配置文件 application-prod.properties
线上自己复制一份即可
创建mapper.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="net.img.mapper.SysUserMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="net.img.entity.SysUserEntity">
<result column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="password" property="password"/>
<result column="nick_name" property="nickName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="uuid" property="uuid"/>
<result column="avatar" property="avatar"/>
<result column="remark" property="remark"/>
<result column="user_status" property="userStatus"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, user_id, password, nick_name, email, gender, uuid, avatar, remark, user_status, create_time, update_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Long">
select
<include refid="Base_Column_List"/>
from sys_user
where user_id = #{userId}
</select>
</mapper>
mapper接口
package net.img.mapper;
import net.img.entity.SysUserEntity;
/**
* <p>
* 系统用户 Mapper 接口
* @since 2023-10-11
*/
public interface SysUserMapper {
/**
* 根据主键查询系统用户
*/
SysUserEntity selectByPrimaryKey(Long userId);
}
用户实体,忽略了get set 方法
package net.img.entity;
import java.io.Serializable;
import java.util.Date;
/**
* 系统用户
* @since 2023-10-11
*/
public class SysUserEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Long userId;
private String password;
private String nickName;
private String email;
private Byte gender;
private String uuid;
private String avatar;
private String remark;
private String userStatus;
private Date createTime;
private Date updateTime;
}
启动文件增加mapper扫描
@MapperScan(value="net.img.mapper")
红框为新增mapper扫描路径
写个controller,获取用户信息
@Controller
public class HelloController {
@Resource
private SysUserMapper sysUserMapper;
@ResponseBody
@RequestMapping("/getUser")
public SysUserEntity getUser(Long userId) {
return sysUserMapper.selectByPrimaryKey(userId);
}
}
启动吧朋友们
启动后访问url 获取用户信息