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



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})

六、编写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 @ 2024-07-18 14:16  vello  阅读(464)  评论(0)    收藏  举报