整合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类:
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private Integer id;
private String name;
private String password;
private String sex;
private String fullName;
private String email;
}
UserLogin接口的编写:
public interface UserLogin {
/**
*查询用户名是否存在
*/
public Boolean IsExit(String name);
/**
* 判断账号密码是否存在
* @param name
* @return
*/
public User Login(String name);
}
UserLoginImpl接口实现类的编写:
@Service
public class UserLoginImpl implements UserLogin {
//自动注入UserLoginMapper
@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
层编写来测试登录注册。
@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";
}
}
登录成功时:
会跳转到主页面
登录失败时:
会重定向到这个页面
到此这篇关于SpringBoot整合MyBatis笔记的文章就介绍到这了,更多相关SpringBoot整合MyBatis内容请搜索html5模板网以前的文章希望大家以后多多支持html5模板网!