SpringBoot 集成Junit 测试环境
导入spring-boot-starter-test坐标
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMybatisApplication.class)
public class DaoTest {
@Autowired
private IUserDao iUserDao;
@Test
public void test(){
List<User> all = iUserDao.findAll();
System.out.println(all);
}
}
SpringBoot集成Mybatis
1.导入mybatis-spring-boot-starter坐标
1.1导入 对应版本 mysql-connector-java 驱动包
2.application.properties中 配置数据库和Mybatis
#配置数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
#配置数据库连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#配置Mybatis
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.lxwc.domain
3.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.lxwc.dao.IUserDao">
<select id="findAll" resultType="User">
select * from user;
</select>
</mapper>
SpringBoot集成redis
需要提前配置好数据库连接
1.添加坐标 spring-boot-starter-data-redis
2.application.properties中添加配置
#配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMybatisApplication.class)
public class DaoTest {
@Autowired
private IUserDao iUserDao;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void test() throws JsonProcessingException {
String userList = redisTemplate.boundValueOps("userList").get();
if(null == userList){
List<User> all = iUserDao.findAll();
ObjectMapper objectMapper = new ObjectMapper();
userList = objectMapper.writeValueAsString(all);
redisTemplate.boundValueOps("userList").set(userList);
System.out.println("数据库中查询存入redis");
}else{
System.out.println("redis中查询");
System.out.println(userList);
}
}
}
SpringBoot集成Shiro
添加shiro-spring-boot-web-starter坐标
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.7.0</version>
</dependency>
编写shiroConfig类
@Configuration
public class ShiroConfig {
//1.ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean (@Qualifier("securityManager")DefaultWebSecurityManager SecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
bean.setSecurityManager(SecurityManager);
//创建过滤器容器
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
//添加授权过滤
linkedHashMap.put("/user/add","perms[user:add]");
linkedHashMap.put("/user/delete" ,"perms[user:delete]");
//添加shiro过滤器
linkedHashMap.put("/user/*","authc");//拦截请求
bean.setFilterChainDefinitionMap(linkedHashMap);
bean.setLoginUrl("/toLogin");//跳转到登录页面
return bean;
}
//2.DefaultWebSecurityManager
@Bean(name="securityManager")
public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
return new DefaultWebSecurityManager(userRealm);
}
//realm类
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
}
编写UserRealm 类
public class UserRealm extends AuthorizingRealm {
@Autowired
private IUserDao iUserDao;
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("1=====doGetAuthorizationInfo");
//获取当前用户
Subject subject = SecurityUtils.getSubject();
User user = (User) subject.getPrincipal();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//给当前用户添加授权
info.addStringPermission(user.getAuthority());
return info;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("2=====doGetAuthenticationInfo");
//认证账号密码
UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
User user = iUserDao.findUserByName(token.getUsername());
if(user == null){
return null;
}
//传入密码由Shiro自动认证 传入user共享
return new SimpleAuthenticationInfo(user,user.getPassword(),"");
}
}
Controller类
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll")
@ResponseBody
public List<User> findAll(){
return userService.findAll();
}
@RequestMapping({"/","index"})
public String testThymeleaf(Model model){
model.addAttribute("name","测试首页");
return "index";
}
@RequestMapping("/user/add")
public String add(){
return "user/add";
}
@RequestMapping("/user/delete")
public String delete(){
return "user/delete";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "user/login";//转到登录页面
}
@RequestMapping("/noAuth")
public String noAuth(){
return "noAuth";//转到未授权提示页面
}
@RequestMapping("/login")
public String login(String username,String password,Model model){
//获取当前用户
Subject subject = SecurityUtils.getSubject();
//封装账号密码
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
return "index";//登录成功 返回首页
}catch(UnknownAccountException e){
model.addAttribute("msg","账号错误");
return "user/login";//转到登录页面
}catch (IncorrectCredentialsException e){
model.addAttribute("msg","密码错误");
return "user/login";//转到登录页面
}
}
}
网友评论