1.创建数据库&新建项目
-
新建数据库
- 字符集 utf8mb4
-
IDEA新建项目
-
Spring initializr
-
dependencies
-
Developer --lombok
-
Web --Spring Web
-
SQL --MyBatis Framework
--MySQL Driver
-
-
2.修改application.properties为application.yml
配置:端口配置、数据库连接配置
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/demo0603
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
not-empty: true
logging:
level:
com.feifei.demo.mapper: debug
3.创建controller包
建UserController类
package com.feifei.demo.controller;
import com.feifei.demo.domain.User;
import com.feifei.demo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;
@Slf4j
@RequestMapping("user")
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("login") //以get方式请求
public String login(String name,String password){
if(ObjectUtils.isEmpty(name)){
return "用户名不能为空";
}
if(ObjectUtils.isEmpty(password)){
return "密码不能为空";
}
User user=userMapper.selectUser(name);
if(user==null){
return "登录失败";
}
if(Objects.equals(password, user.getPassword())){
return "登陆成功";
}
return "密码错误,登录失败";
}
@GetMapping("register")
public String register(String name,String password){
log.info("name:{}",name); //打印
log.info("password:{}",password);
if(ObjectUtils.isEmpty(name)){
return "用户名不能为空";
}
if(ObjectUtils.isEmpty(password)){
return "密码不能为空";
}
User user=userMapper.selectUser(name); //注册前判断是否存在
if(user!=null){
return "登录失败,用户已经存在";
}
int resultCount=userMapper.saveUser(name,password);//将数据保存到数据库
if(resultCount==0){
return "注册失败";
}
return "注册成功";
}
}
4.连接数据库
新建domain包,底下建User类
package com.feifei.demo.domain;
import lombok.Data;
import javax.persistence.*;
@Data
@Table(name = "tab_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id") //字段和数据库对应
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
}
5.新建UserMapper
新建mapper.UserMapper
package com.feifei.demo.mapper;
import com.feifei.demo.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface UserMapper extends Mapper<User> {
//写入SQL
@Insert("INSERT INTO tab_user(name,password) VALUES(#{name},#{password})")
int saveUser(@Param("name") String name, @Param("password") String password);
@Select("select id ,name,password from tab_user where name=#{name}")
User selectUser(@Param("name") String name);
}
6.Demo0603Application
package com.feifei.demo;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.feifei.demo.mapper") //扫描注入mapper类
@SpringBootApplication
public class Demo0603Application {
public static void main(String[] args) {
SpringApplication.run(Demo0603Application.class, args);
}
}
通用mapper
通用mapper解决Mybatis使用中90%的基本操作,方便开发,节省开发人员时间
-
添加依赖
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency>
-
配置(application.yml)
mapper: mappers: - tk.mybatis.mapper.common.Mapper not-empty: true logging: /*这个是打印日志*/ level: com.feifei.demo.mapper: debug
-
UserMapper类进行配置
@Data
@Table(name = "tab_user") //关联表
public class User {
@Id //增加ID字段
@GeneratedValue(strategy = GenerationType.IDENTITY) //注解说明自增
@Column(name = "id") //跟字段关联起来
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
}
-
原来的SQL查询使用通用mapper来解决
User record = new User();record.setName(name);User user=userMapper.selectOne(record);
-
快速查询所有用户
@GetMapping("all")
public List<User> all(){
return userMapper.selectAll();
}
- 按名字查询用户
@GetMapping("allByName")
public List<User> all(String name){
User record=new User();
record.setName(name);
return userMapper.select(record);
}