springboot-shiro:整合mybatis
1 导入整合mybatis的相关依赖
pom.xml
<!-- 引入myBatis,这是MyBatis官方提供的适配Spring Boot的,而不是SpringBoot自己的-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
2 编写配置文件application.yml的连接配置
src/main/resources/application.yaml
spring:
datasource:
username: root
password: 123456
#假如时区报错了,就增加一个时区配置就OK了 serverTimezone=UTC
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
#切换数据源
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException:org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
3 创建目录结构
创建三个包和一个文件夹:mapper包,pojo包,service包,resources目录下新建一个mapper文件夹
4 在application.properties中编写mybatis的配置
src/main/resources/application.properties
mybatis.type-aliases-package=com.lv.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
5 编写实体类
引入lombok依赖
pom.xml
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
在pojo包下编写一个User类
src/main/java/com/lv/pojo/User.java
package com.lv.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
6 在mapper包下编写一个UserMapper接口
src/main/java/com/lv/mapper/UserMapper.java
package com.lv.mapper;
import com.lv.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface UserMapper {
public User queryUserByName(String name);
}
7 在mapper文件夹下编写一个UserMapper.xml文件
src/main/resources/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.lv.mapper.UserMapper">
<select id="queryUserByName" parameterType="String" resultType="User">
select * from user where name = #{name}
</select>
</mapper>
8 在service包下编写UserService接口
src/main/java/com/lv/service/UserService.java
package com.lv.service;
import com.lv.pojo.User;
public interface UserService {
public User queryUserByName(String name);
}
9 在service包下编写UserService接口对应的实现类
src/main/java/com/lv/service/UserServiceImpl.java
package com.lv.service;
import com.lv.mapper.UserMapper;
import com.lv.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryUserByName(String name) {
return userMapper.queryUserByName(name);
}
}
10 修改UserRealm,连接到数据库进行真实的操作
src/main/java/com/lv/config/UserRealm.java
package com.lv.config;
import com.lv.pojo.User;
import com.lv.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
//自定义的UserRealm extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
@Autowired
UserService userService;
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了=>授权doGetAuthorizationInfo");
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("执行了=>认证doGetAuthorizationInfo");
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
//连接真实的数据库
User user = userService.queryUserByName(userToken.getUsername());
if (user == null){ //没有这个人
return null; //UnknownAccountException
}
//密码认证,交给shiro做(可以加密: MD5,MD5盐值加密)
return new SimpleAuthenticationInfo("",user.getPwd(),"");
}
}
11 启动程序测试
访问登录页面,输入错误的账号,和密码
登录失败,提示用户名错误,接下来输入正确的账号,和错误的密码
登录失败,提示密码错误,接下来输入数据库中正确的用户名和密码
登录成功,再输入数据库中另一组正确的用户名和密码
依旧登录成功,实现了整合mybatis连接数据库