SpringBoot整合MyBatis+MySQL

一、添加mysql驱动


mysql
mysql-connector-java

二、添加MyBatis依赖

org.mybatis.spring.boot
mybatis-spring-boot-starter
3.0.1

三、添加配置
spring:
datasource:
name: xx-datasource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xx-db?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowMultiQueries=true&useAffectedRows=true
username: xxx
password: xxxxxx
hikari:
pool-name: hikariCP
minimum-idle: 20
maximum-pool-size: 50
auto-commit: true
idle-timeout: 10000
max-lifetime: 30000
connection-timeout: 30000
connection-test-query: SELECT 1

四、编写实体
@Data
public class AdminUser {
private Integer adminUserId;

private String loginUserName;

private String loginPassword;

private String nickName;

private Byte locked;

}
五、编写Mapper.xml

admin_user_id , login_user_name, login_password, nick_name, locked
<select id="login" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tb_newbee_mall_admin_user
    where login_user_name = #{userName,jdbcType=VARCHAR} AND login_password=#{password,jdbcType=VARCHAR} AND locked
    = 0
</select>

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tb_newbee_mall_admin_user
    where admin_user_id = #{adminUserId,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.lwl.mall.entity.AdminUser">
    insert into tb_newbee_mall_admin_user (admin_user_id, login_user_name, login_password,
                                           nick_name, locked)
    values (#{adminUserId,jdbcType=INTEGER}, #{loginUserName,jdbcType=VARCHAR}, #{loginPassword,jdbcType=VARCHAR},
            #{nickName,jdbcType=VARCHAR}, #{locked,jdbcType=TINYINT})
</insert>

六、编写Mapper接口DAO数据访问层

import com.lwl.mall.entity.AdminUser;
import org.apache.ibatis.annotations.Param;

public interface AdminUserMapper {
int insert(AdminUser record);

int insertSelective(AdminUser record);

/**
 * 登陆方法
 *
 * @param userName
 * @param password
 * @return
 */
AdminUser login(@Param("userName") String userName, @Param("password") String password);

AdminUser selectByPrimaryKey(Integer adminUserId);

int updateByPrimaryKeySelective(AdminUser record);

int updateByPrimaryKey(AdminUser record);

}

七、编写Service服务层代码
public interface AdminUserService {

AdminUser login(String userName, String password);

/**
 * 获取用户信息
 *
 * @param loginUserId
 * @return
 */
AdminUser getUserDetailById(Integer loginUserId);

/**
 * 修改当前登录用户的密码
 *
 * @param loginUserId
 * @param originalPassword
 * @param newPassword
 * @return
 */
Boolean updatePassword(Integer loginUserId, String originalPassword, String newPassword);

/**
 * 修改当前登录用户的名称信息
 *
 * @param loginUserId
 * @param loginUserName
 * @param nickName
 * @return
 */
Boolean updateName(Integer loginUserId, String loginUserName, String nickName);

}

Service实现类代码

@Service
public class AdminUserServiceImpl implements AdminUserService {

@Resource
private AdminUserMapper adminUserMapper;

@Override
public AdminUser getUserDetailById(Integer loginUserId) {
    return adminUserMapper.selectByPrimaryKey(loginUserId);
}

}

八、编写控制层代码

@Controller
@RequestMapping("/admin")
public class AdminController {

@Resource
private AdminUserService adminUserService;

@GetMapping("/profile")
public String profile(HttpServletRequest request) {
    Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
    AdminUser adminUser = adminUserService.getUserDetailById(loginUserId);
    if (adminUser == null) {
        return "admin/login";
    }
    request.setAttribute("path", "profile");
    request.setAttribute("loginUserName", adminUser.getLoginUserName());
    request.setAttribute("nickName", adminUser.getNickName());
    return "admin/profile";
}

@PostMapping("/profile/name")
@ResponseBody
public String nameUpdate(HttpServletRequest request, @RequestParam("loginUserName") String loginUserName,
                         @RequestParam("nickName") String nickName) {
    if (StrUtil.isEmpty(loginUserName) || StrUtil.isEmpty(nickName)) {
        return "参数不能为空";
    }
    Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
    if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
        return ServiceResultEnum.SUCCESS.getResult();
    } else {
        return "修改失败";
    }
}

}

九、启动测试
需要指定Mapper扫包路径
@MapperScan(basePackages = "com.lwl.mall.dao")
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(MallApplication.class, args);
}
}

十、效果如下

posted @   vello  阅读(311)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示