Java-bug点滴记录(十)

1.myspring-mvc配置拦截时全局拦截放开某些拦截时,放开路径出错了,少了路径

2.shiro

import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService{

 @Autowired
 private PermissionDAO permissionDAO;
 @Override
 public List<String> queryPermsByUsername(String username) {
     return permissionDAO.queryPermsByUsername(username);
 }
}

(1)一个权限service方法中忘记书写@Service 注解了,导致handler方法不识别,在自己书写的权限配置文件里语法时找不到permissionServiceImpl 。

(2)在自己书写的realm文件中添加各个实体类的set/get方法实现权限配置文件里的语法找到对应的属性

(3)例如:

<!-- 声明realm -->
    <bean id="realm1" class="com.qianfeng.realm.MyRealm">
        <!-- 注入身份认证 授权 依赖的Service -->
        <property name="userService" ref="userServiceImpl"/>
        <property name="roleService" ref="roleServiceImpl"/>
        <property name="permissionService" ref="permissionServiceImpl"/>
        <!-- 此属性如果通过注解注入,则需要将注解加载set方法上,不能用在属性上
         此属性是父类属性,所以只有在set方法上注入,才能覆盖父类属性值
         -->
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="SHA-256"/>
                <!-- true means hex encoded,false means base64 encoded -->
                <property name="storedCredentialsHexEncoded" value="false"/>
                <property name="hashIterations" value="1000"/>
            </bean>
        </property>
    </bean>

3.SSM开发模式:

(1)applicationContext.xml项目内配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.alibaba.com/schema/stat"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.alibaba.com/schema/stat
                        http://www.alibaba.com/schema/stat.xsd">

 <!-- 1.导入mybatis连接数据库 外部参数文件 导入连接数据库的参数 ===>property-placeholder -->
 <context:property-placeholder location="classpath:jdbc1905.properties"/>
 <!-- 2.将com.qianfeng.dao包下的所有内容注入到 MapperScannerConfigurer 类中的 basePackage属性中 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="sqlSessionFactoryBeanName" value=""/>
     <property name="basePackage" value="com.qianfeng.dao"/>
 </bean>
 <!-- 3.连接池:druid ===> dataSource -->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
     <!-- 基本属性 ===> url、user、password -->
     <property name="url" value="${jdbc.url}"/>
     <property name="username" value="${jdbc.user}"/>
     <property name="password" value="${jdbc.password}"/>
     <!-- 配置初始化大小、最小、最大 ===>initialSize  minIdle  maxActive-->
     <property name="initialSize" value="1"/>
     <property name="minIdle" value="1"/>
     <property name="maxActive" value="${jdbc.maxPoolSize}"/>
     <!-- 配置获取连接等待超时的时间 ===> maxWait -->
     <property name="maxWait" value="3000"/>
     <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 ===>  timeBetweenEvictionRunsMillis -->
     <property name="timeBetweenEvictionRunsMillis" value="60000"/>
     <!-- 配置一个连接在池中最小空闲的时间,单位是毫秒 ===> minEvictableIdleTimeMillis -->
     <property name="minEvictableIdleTimeMillis" value="300000"/>
     <!-- 在连接数据库前,尝试连接,测试连接是否可以继续实用(主要检测:是否有意外中断的连接)==> validationQuery testWhileIdle testOnBorrow testOnReturn -->
     <property name="validationQuery" value="SELECT 1"/>
     <property name="testWhileIdle" value="true"/>
     <property name="testOnBorrow" value="false"/>
     <property name="testOnReturn" value="false"/>
 </bean>
 <!-- 4.启动Spring工厂 实现被动接受值 ==> IOC控制反转 DI依赖注入-->
 <bean id="sqlSessionFactory1905" class="org.mybatis.spring.SqlSessionFactoryBean">
     <!-- (1)将连接池放入工厂,使得工厂中数据可以让连接池使用 ref:连接池的id 注入到 name:工厂类中的id -->
     <property name="dataSource" ref="dataSource"/>
     <!-- (2)将MVC和项目的所有的.xml的配置文件放入工厂 ==> mapperLocations中 就是Mapper中-->
     <property name="mapperLocations">
         <list>
             <value>classpath:com/qianfeng/dao/*.xml</value>
         </list>
     </property>
     <!-- (3)定义包别名在DAO中使用类型时可以不用前加包路径(类型别名包)==> typeAliasesPackage -->
     <property name="typeAliasesPackage" value="com.qianfeng.pojo"/>
     <!-- (4)将分页插件放入工厂 ==>plugins中 -->
     <property name="plugins">
         <list>
             <bean class="com.github.pagehelper.PageInterceptor">
                 <property name="properties">
                     <props>
                         <prop key="reasonable">true</prop>
                     </props>
                 </property>
             </bean>
         </list>
     </property>
 </bean>
 <!-- 5.将Spring工厂和Spring-MVC工厂 整合 注解识别的划分 目的 避免注解识别重复,造成额外功能冗余问题 -->
 <context:component-scan base-package="com.qianfeng" use-default-filters="true">
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
 </context:component-scan>
 <!-- 6.事务管理 -->
 <!-- 总结:
    1.将所有的内容都注入到工厂,其中包括:
     (1)Controller、
     (2)pojo(类,和包路径)、
     (3)service、
     (4)连接池、
     (5)项目内的.xml和mvc.xml配置
     (6)将分页插件放入工厂
    2.将另外两个文件存到Spring配置文件中,就是当前配置文件中
     (1)外部的项目内的.xml文件和mvc.xml配置文件
     (2)将DAO下的文件存到此处
    3.Spring和Spring-MVC整合,注解识别的划分
 至此:整个项目内的所有的信息全都到了applicationContext.xml内部配置文件,然后将此配置文件加到项目的配置文件中即可,
     使得整个项目得到所有的数据。
      -->
</beans>

(2)mvc.xml 项目内配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
xml:  根  开  闭
dtd: document type definition   xxx.dtd
xsd: xml schema definition    xxxx.xsd
标签,属性,子标签,顺序
-->

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context.xsd
                      http://www.springframework.org/schema/mvc
                      http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 1.注解扫描 ==>只注解Controller -->
<context:component-scan base-package="com.qianfeng" use-default-filters="false">
   <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2.注解开发的驱动配置 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 3.视图解析器 ===> 将InternalResourceViewResolver类中的prefix和suffix属性注入值
    接收 后端控制的方法返回值  "index"  "hello"   解析后: “/index.jsp”  "/hello.jsp"
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <!-- 注入前后缀 -->
   <property name="prefix" value="/"/>
   <property name="suffix" value=".jsp"/>
</bean>

<!-- 4.解决静态资源无法访问的问题 -->
<mvc:default-servlet-handler/>
</beans>

(3)在修改application.xml配置文件时

在此文件的最上方有一句提示语句:
大概意思是:修改此文件后要重新加载,但是需要删除已加载的此文件,然后才能自动加载。
注意:不要修改除此之外的所有的内容,我的bug是删除了项目的别的限制条件的文件,导致项目崩溃。重新开启了一个项目没有在项目配置文件中<build></build>中配置加载项目下的.xml和.properties操作。导致项目找不到DAO类内的方法。然而我一直认为是上面重新加载文件的错误,然后删,加,删,加,再删别的,再加别的。。。等n次后,就凌晨2:30分了。噩梦的一个晚上!!!
带着电脑跑,回到住处后打开电脑后键盘和鼠标都乱了,重启后才恢复正常
前两天,电脑CUP占用高,重启不能降低,强制关机后,不行,拔掉网线,强关后启动才可以了

(4)实体收参 SpringMVC中Controller

请求参数和实体的属性 同名即可

public class User{
 private Integer id;
 private String name;
 @DateTimeFormat(pattern="yyyy-MM-dd")
 private Date birth;
 private Boolean gender;
}
http://localhost:8989/.../test?id=1&name=Tom&gender=false&birth=2019-05-16 12:29:28
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("/test01")
public String testParam(User user){
System.out.println("user"+user);
return "index";
	}
}
public class Test01 extends HttpServlet {
 @Override
 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     String id = req.getParameter("id");
     String name = req.getParameter("name");
     String birth = req.getParameter("birth");
     String gender = req.getParameter("gender");
     User user = new User(id,name,birth,gender);
     System.out.println(user);
 }
}

上面是SSM架构的收参和传统收参的代码比较:

两个从形式上看,

<1>.传统收参较过于繁琐。

<2>.传统收参含有强耦合。

4.动态SQL语句在SSM框架利使用时:

在DAO接口类中,方法的参数前加别名 ---> @Pama(String name)

不然会出错,条件里的where name = #{name}不识别,要梳理

或者是service中的实体类的注解类型 要梳理

5.我的开发流程:

SSM框架+Filter(编码格式)+shiro权限控制(权限关键词@...)+分页查询(limit x x)+redis二级缓存(Cache):pojo、dao(applicationContext、jdbc)、测试test---->service(控制事务)、controller(mvc、监听工厂)、shiro(登录不能控制、shiro_spring)--->MyCache(DAO中开启缓存、redis_spring) 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值