测试DAO接口以及服务层编写

测试Dao接口是否有问题

利用测试类来看dao层是否有问题,分别用三个测试方法测试三个dao接口。

测试类如下:

 package com.cao.frs;
 
 import com.cao.frs.dao.InvoiceMapper;
 import com.cao.frs.dao.ReimburseMapper;
 import com.cao.frs.dao.UserMapper;
 import com.cao.frs.entities.Invoice;
 import com.cao.frs.entities.Reimburse;
 import com.cao.frs.entities.Users;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 
 @SpringBootTest
 class FrsApplicationTests {
 
     @Autowired
     UserMapper userMapper;
     @Autowired
     InvoiceMapper invoiceMapper;
     @Autowired
     ReimburseMapper reimburseMapper;
     @Test
     void contextLoads() {
         userMapper.remove(4);
         userMapper.add(new User(4,"北京",new Date(),"123@13.com",1,"教授","123","12354566","pro",200));
         HashMap<String, Object> map = new HashMap<>();
         map.put("id",1);
         map.put("city","天津");
         map.put("isAdmin",0);
         map.put("limit",500);
         userMapper.update(map);
         List<User> all = userMapper.findAll();
         for (User users : all) {
             System.out.println(users);
        }
 
 
 
    }
     @Test
     void test1(){
         invoiceMapper.add(new Invoice(6,"小绿","交通",200,1,new Date(),"122122Ad","xxxx大学"));
         List<Invoice> list1 = invoiceMapper.searchByName("小红");
         for (Invoice invoice : list1) {
             System.out.println(invoice.toString());
        }
         invoiceMapper.remove(6);
         HashMap<String, Object> map = new HashMap<>();
         map.put("id",1);
         map.put("name","小了");
         map.put("type","教育");
         map.put("hasBill",0);
         map.put("billDate",new Date());
         map.put("title","xxxxx小学");
         invoiceMapper.update(map);
         List<Invoice> all = invoiceMapper.findAll();
         for (Invoice invoice : all) {
             System.out.println(invoice);
        }
 
    }
     @Test
     void test2(){
         reimburseMapper.add(new Reimburse(5,2,new Date(1111111),new Date(),3,500));
         List<Reimburse> reimburses = reimburseMapper.searchByUserId(2);
         for (Reimburse reimburs : reimburses) {
             System.out.println(reimburs);
        }
         List<Reimburse> all = reimburseMapper.findAll();
         for (Reimburse reimburse : all) {
             System.out.println(reimburse);
        }
    }
 }

搭建service层

service接口

InvoiceService

 package com.cao.frs.repos;
 
 import com.cao.frs.entities.Users;
 
 import java.util.List;
 import java.util.Map;
 
 public interface UserService {
     int add(Users users);
 
     int remove(int id);
 
     int update(Map<String,Object> map);
 
     List<Users> findAll();
 
     Users findByName(String username);
 }
 

ReimburseService

 package com.cao.frs.repos;
 
 import com.cao.frs.entities.Users;
 
 import java.util.List;
 import java.util.Map;
 
 public interface UserService {
     int add(Users users);
 
     int remove(int id);
 
     int update(Map<String,Object> map);
 
     List<Users> findAll();
 
     Users findByName(String username);
 }
 

UserService

 package com.cao.frs.repos;
 
 import com.cao.frs.entities.Users;
 
 import java.util.List;
 import java.util.Map;
 
 public interface UserService {
     int add(Users users);
 
     int remove(int id);
 
     int update(Map<String,Object> map);
 
     List<Users> findAll();
 
     Users findByName(String username);
 }

对应实现类

InvoiceServiceImpl

 package com.cao.frs.service;
 
 import com.cao.frs.dao.InvoiceMapper;
 import com.cao.frs.entities.Invoice;
 import com.cao.frs.repos.InvoiceService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
 import java.util.Map;
 @Service
 public class InvoiceServiceImpl implements InvoiceService {
     @Autowired
     InvoiceMapper invoiceMapper;
     @Override
     public int add(Invoice invoice) {
         return invoiceMapper.add(invoice);
    }
 
     @Override
     public int remove(Integer id) {
         return invoiceMapper.remove(id);
    }
 
     @Override
     public int update(Map<String, Object> map) {
         return invoiceMapper.update(map);
    }
 
     @Override
     public List<Invoice> findAll() {
         return invoiceMapper.findAll();
    }
 
     @Override
     public List<Invoice> searchByName(String name) {
         return invoiceMapper.searchByName(name);
    }
 }
 

ReimburseSeviceImpl

 package com.cao.frs.service;
 
 import com.cao.frs.dao.ReimburseMapper;
 import com.cao.frs.entities.Reimburse;
 import com.cao.frs.repos.ReimburseService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
 @Service
 public class ReimburseSeviceImpl implements ReimburseService {
     @Autowired
     private ReimburseMapper reimburseMapper;
     @Override
     public int add(Reimburse reimburse) {
         return reimburseMapper.add(reimburse);
    }
 
     @Override
     public List<Reimburse> findAll() {
         return reimburseMapper.findAll();
    }
 
     @Override
     public List<Reimburse> searchByUserId(Integer userId) {
         return reimburseMapper.searchByUserId(userId);
    }
 }
 

UserSecurityDetailService

注意:与security的搭建结合,更新springSecurity的配置

 package com.cao.frs.service;
 
 import com.cao.frs.dao.UserMapper;
 import org.springframework.security.core.userdetails.User;
 import com.cao.frs.repos.UserService;
 import com.cao.frs.entities.Users;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
 @Service
 public class UserSecurityDetailService implements UserDetailsService, UserService {
 
     @Autowired
     UserMapper userMapper;
 
     @Autowired
     private PasswordEncoder passwordEncoder;
 
     @Override
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
         Users user = userMapper.findByName(username);
         System.out.println(user);
         // 获得角色
         String role = String.valueOf(user.getIsAdmin());
         // 角色集合
         List<GrantedAuthority> authorities = new ArrayList<>();
         // 角色必须以`ROLE_`开头,数据库中没有,则在这里加
         authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
         // 数据库密码是明文, 需要加密进行比对
         return new User(user.getUsername(), passwordEncoder.encode(user.getPassword()),authorities);
    }
 
 
     @Override
     public int add(Users user) {
         return userMapper.add(user);
    }
 
     @Override
     public int remove(int id) {
         return userMapper.remove(id);
    }
 
     @Override
     public int update(Map<String, Object> map) {
         return userMapper.update(map);
    }
 
     @Override
     public List<Users> findAll() {
         return userMapper.findAll();
    }
 
     @Override
     public Users findByName(String username) {
         return userMapper.findByName(username);
    }
 }
 

新安全配置

 package com.cao.frs.configuration;
 
 import com.cao.frs.service.UserSecurityDetailService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 import org.springframework.security.config.annotation.web.builders.WebSecurity;
 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 
 @EnableWebSecurity
 public class MySecurityConfig extends WebSecurityConfigurerAdapter {
     @Autowired
     private UserSecurityDetailService userSecurityDetailService;
     @Override
     protected void configure(HttpSecurity http) throws Exception {
         //授权
         http.formLogin()
                 //自定义登陆页面
                .loginPage("/login")
                 //如果URL为loginPage,则用SpringSecurity中自带的过滤器去处理该请求
                .successForwardUrl("/index")
                .loginProcessingUrl("/use/login")
                .and()
                 //请求授权
                .authorizeRequests()
                 //在访问我们的URL时,我们是不需要省份认证,可以立即访问
                .antMatchers("/javaex/**","/","/favicon.ico","/login","/user/login").permitAll()
                 //所有请求都被拦截,都需认证
                .anyRequest().authenticated()
                .and()
                 // 请求头允许X-ContentType-Options
                 //.headers().contentTypeOptions().disable()
                 //.and()
                 // 请求头允许X-Frame-Options, 否则所有iframe将失效
                .headers().frameOptions().disable()
                .and()
                 // 注销, 回到首页
                .logout().logoutSuccessUrl("/")
                 //SpringSecurity保护机制
                .and()
                .csrf().disable();
 
         // 开启记住我功能
         http.rememberMe();
                 //.rememberMeParameter("remember");
    }
 
     @Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         // 认证
         auth.userDetailsService(userSecurityDetailService).passwordEncoder(passwordEncoder());
    }
     @Override
     public void configure(WebSecurity web) throws Exception {
         // swagger 资源放行
         web.ignoring().antMatchers("/webjars/**","/v2/**","/swagger-resources/**","/doc.html","/docs.html","swagger-ui.html");
    }
     @Bean
     public PasswordEncoder passwordEncoder(){
         // 使用BCrypt加密密码
         return new BCryptPasswordEncoder();
    }
 }
 

 

posted on 2022-01-07 17:19  阿ming  阅读(109)  评论(0编辑  收藏  举报

导航