6.SpringBoot整合mybatis实现登录

注意:由于内容有点多,在看本文章前,一定要按照下面菜单中的教程,一步一步来

-->SpringBoot企业级开发精讲系列教程

本章带大家使用springboot + mybaits整合,实现一个简单的登录实例

准备:

1.安装mysql

2.创建springboot数据库

3.创建s_user表

DROP TABLE IF EXISTS `s_user`;
CREATE TABLE `s_user` (
  `_id` int(11) NOT NULL AUTO_INCREMENT,
  `nickname` varchar(30) DEFAULT NULL,
  `password` varchar(30) DEFAULT NULL,
  `create_time` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

这里创建用户表user,加前戳s_其意义是区分系统表和具体事务表,在此项目中使用到的前戳有:

 

前戳全称描述
s_system系统表前戳
b_business事务表前戳


1.创建User数据库表映射实体类

@Data
public class User {
    private int _id;
    private String nickname;
    private String password;
    private long create_time;
}

注意字段名和变量名保持一致

2.创建UserDao类

@Mapper
public interface UserDao {
    User login(@Param("nickname") String nickname, @Param("password")String password);
}

3.创建service

@Slf4j
@Service
public class UserService {

    @Autowired
    private UserDao mUserDao;
    
    public LoginRPTO login(LoginRQTO login){
        User user = mUserDao.login(login.getNickname(),login.getPassword());
        if(user == null){
            return null;
        }
        LoginRPTO rpto = new LoginRPTO();
        rpto.setUser_id(user.get_id());
        rpto.setNickname(login.getNickname());
        return rpto;
    }
}

4.修改UserController

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService mUserService;

    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public RPTO login(@RequestBody @Valid LoginRQTO login) {
        LoginRPTO rpto = mUserService.login(login);
        if(rpto == null){
            return new RPTO<>("用户名密码错误");
        }
        return new RPTO<>(ActionCode.SUCCESS,rpto);
    }
}

5.在resource目录下创建mapper目录,并新建UserMapper.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.fcibook.springboot.dao.UserDao">
    <select id="login" resultType="com.fcibook.springboot.core.system.User">
        SELECT * FROM s_user WHERE nickname = #{nickname} and password = #{password}
    </select>
</mapper>

6.修改application.properties

server.port=8080

#日志配置
logging.level.root=warn
logging.level.com.fcibook.springboot=DEBUG
#springframework.web日志以DEBUG级别输出
logging.level.org.springframework.web=warn
#hibernate日志以ERROR级别输出
logging.level.org.hibernate=warn
logging.file=/logs/my.log
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n

#数据库配置
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456

#配置.xml文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
#配置模型路径
mybatis.type-aliases-package=com.fcibook.springboot.dao

7.运行,测试

在数据库中,手动添加一条记录

然后打开网址

http://localhost:8080/login.html

登录测试成功!

但是值得一提的是mybatis有多种方法来操作数据库,我们访问mybatis官网

http://www.mybatis.org/mybatis-3/zh/getting-started.html

看到如下文档解释:

 

----------------------------------------------------------------------

源码下载

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Spring Boot中整合MyBatis-Plus实现Presto分页,可以按照以下步骤进行操作: 1. 首先,确保已经在项目的pom.xml文件中添加了MyBatis-Plus和Presto的依赖。 2. 在application.properties或application.yml文件中配置Presto的连接信息,包括URL、用户名和密码等。 3. 创建一个Presto分页查询的方法,可以使用MyBatis-Plus提供的Page对象来实现分页功能。在该方法中,使用@Select注解定义SQL查询语句,并使用@Param注解指定方法参数。 4. 在方法中,使用Page对象的setRecords方法将查询结果设置到Page对象中,并使用Page对象的setTotal方法设置总记录数。 5. 在方法中,使用MyBatis-Plus的selectPage方法执行分页查询,并将Page对象作为参数传递给该方法。 6. 在Controller层调用Presto分页查询的方法,并将查询结果返回给前端。 下面是一个示例代码,演示了如何在Spring Boot中整合MyBatis-Plus实现Presto分页: ```java // 引入相关的包和注解 @Service public class PrestoService { @Autowired private PrestoMapper prestoMapper; public Page<PrestoEntity> getPrestoPage(int pageNum, int pageSize) { Page<PrestoEntity> page = new Page<>(pageNum, pageSize); List<PrestoEntity> records = prestoMapper.getPrestoPage(page); page.setRecords(records); return page; } } @Mapper public interface PrestoMapper { @Select("SELECT * FROM table_name") List<PrestoEntity> getPrestoPage(Page<PrestoEntity> page); } @RestController public class PrestoController { @Autowired private PrestoService prestoService; @GetMapping("/presto/page") public Page<PrestoEntity> getPrestoPage(@RequestParam int pageNum, @RequestParam int pageSize) { return prestoService.getPrestoPage(pageNum, pageSize); } } ``` 请注意,上述代码仅为示例,实际使用时需要根据具体的表名、字段名和查询条件进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值