pom.xml中导入mysql和mybatisplus相关jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
在application.properties中配置mybatisplus
#mysql驱动包
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jdbc数据库地址
spring.datasource.url=jdbc:mysql://192.168.237.134:3306/mybatis_tutorial?characterEncoding=UTF-8
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#xml中用于存放sql语句
mybatis-plus.mapper-locations=classpath:/mapper/*Mapper.xml
#使控制台能打印sql
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
配置mybatis分页插件及mapper接口扫描包
package com.xbb.springboot.tutorial;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
@Configuration
@MapperScan("com.xbb.springboot.tutorial.mapper")
public class MybatisPlusConfig {
/**
* mybatis plus分页插件配置
* @return
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setDbType(DbType.MYSQL);
List<InnerInterceptor> innerInterceptors = new ArrayList<>();
innerInterceptors.add(paginationInnerInterceptor);
mybatisPlusInterceptor.setInterceptors(innerInterceptors);
return mybatisPlusInterceptor;
}
}
创建数据库sql语句
create database mybatis_tutorial;
use mybatis_tutorial;
create table tb_user(
id varchar(36) primary key,
username varchar(50),
password varchar(100),
birthday date
);
insert into tb_user(id,username,password,birthday)
values('11111','admin','666666','2020-02-20');
在User实体中添加TableName注释和数据库表建立映射关系
建立Mapper接口用于访问数据库
package com.xbb.springboot.tutorial.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xbb.springboot.tutorial.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
创建用户业务逻辑处理接口UserService
package com.xbb.springboot.tutorial.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xbb.springboot.tutorial.entity.User;
public interface UserService extends IService<User> {
}
创建用户业务逻辑处理实现类UserServiceImpl
package com.xbb.springboot.tutorial.service.impl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbb.springboot.tutorial.entity.User;
import com.xbb.springboot.tutorial.mapper.UserMapper;
import com.xbb.springboot.tutorial.service.UserService;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
修改LoginController为访问数据库登录
package com.xbb.springboot.tutorial.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbb.springboot.tutorial.entity.User;
import com.xbb.springboot.tutorial.service.UserService;
@Controller
public class LoginController {
// @RequestMapping("login")
// public String login(HttpServletRequest request, String username, String password) {
// if ("admin".equals(username) && "666666".equals(password)) {
// request.getSession().setAttribute("username", "admin");
// return "redirect:/view/index"; //客户端跳转
// } else {
// request.setAttribute("errorMsg", "用户名或密码有误");
// return "login";//服务器跳转
// }
// }
/**
* 以下代码作用和上面完全相同
* @return
*/
// @RequestMapping("login")
// public ModelAndView login(HttpServletRequest request, User user) {
// ModelAndView mav = new ModelAndView();
//
// if ("admin".equals(user.getUsername()) && "666666".equals(user.getPassword())) {
// request.getSession().setAttribute("user", user);
// mav.setViewName("redirect:/view/index");//客户端跳转
// return mav;
// } else {
// mav.addObject("errorMsg", "用户名或密码有误");
// mav.setViewName("login");//服务器跳转
// return mav;
// }
// }
@Autowired
UserService userService;
@RequestMapping("login")
public ModelAndView login(HttpServletRequest request, User user) {
ModelAndView mav = new ModelAndView();
//select * from tb_user where username=? and password=?
QueryWrapper<User> qw = new QueryWrapper<>();
qw.eq("username", user.getUsername());
qw.eq("password", user.getPassword());
User dbUser = userService.getOne(qw);
if (null != dbUser) {
request.getSession().setAttribute("user", dbUser);
mav.setViewName("redirect:/view/index");//客户端跳转
return mav;
} else {
mav.addObject("errorMsg", "用户名或密码有误");
mav.setViewName("login");//服务器跳转
return mav;
}
}
}
访问登录页面测试
http://localhost:8080/view/login