【学习笔记】SpringBoot整合MyBatis
依赖的导入
整合mybatis 之前我们需要相对应的导入相关依赖。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
大致目录
我们首先需要在java 的目录和resources 下创建mapper 文件夹。前者是方法的接口,后者则是xml 文件的编写。
例如:
@Mapper
@Repository
public interface UserLoginMapper {
public List<User> queryAll();
public int add(User user);
public User queryByName(String name);
}
xml实现
<mapper namespace="com.example.springbootdemo03.mapper.UserLoginMapper">
<select id="queryAll" resultType="com.example.springbootdemo03.bean.User">
select * from student
</select>
<insert id="add" parameterType="com.example.springbootdemo03.bean.User">
insert into student values (#{name},#{password})
</insert>
<select id="queryByName" resultType="com.example.springbootdemo03.bean.User">
select * from student where name = #{name}
</select>
</mapper>
相关文件配置
我们可以使用yml 文件也可以使用properties 两种文件选择一个(当然也都可以都用)。
配置:
#配置mapper
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.springbootdemo03.mapper
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mysql1?serverTimezone=GMT%2B8&useSSL=true
username: root
password: 密码
jdbc:
template:
query-timeout: 3
创建数据库和相对应的Pojo类
数据库格式:
POJO类:
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private Integer id;
private String name;
private String password;
private String sex;
private String fullName;
private String email;
}
Service层的编写
UserLogin接口的编写:
public interface UserLogin {
public Boolean IsExit(String name);
public User Login(String name);
}
UserLoginImpl接口实现类的编写:
@Service
public class UserLoginImpl implements UserLogin {
@Autowired
UserLoginMapper userLoginMapper;
@Override
public Boolean IsExit(String name){
if(userLoginMapper.queryByName(name) == null){
return false;
}else return true;
}
@Override
public User Login(String name) {
User user = userLoginMapper.queryByName(name);
return user;
}
}
Controller层的编写
我们可以通过Controller 层编写来测试登录注册。
@Autowired
UserLogin userLogin;
@GetMapping(value = {"/","/login"})
public String Index(){
return "login";
}
@GetMapping("main.html")
public String Main(){
return "index";
}
@PostMapping("/login")
public String Login(User user, HttpSession session, Model model){
if(!userLogin.IsExit(user.getName())){
session.setAttribute("msg", "登录失败");
return "redirect:/login";
}
User login = userLogin.Login(user.getName());
if(user.getName().equals(login.getName()) && user.getPassword().equals(login.getPassword())){
session.setAttribute("loginUser",user);
return "redirect:/main.html";
}
else {
session.setAttribute("msg", "登录失败");
return "redirect:/login";
}
}
登录测试
登录成功时:
会跳转到主页面
登录失败时:
会重定向到这个页面
OK,今天的笔记分享就到这里吧!我们下期再见!!!
|