来自 电脑系统 2019-09-27 01:15 的文章
当前位置: 金沙澳门官网网址 > 电脑系统 > 正文

金沙澳门官网网址一呼百应视图

总体的品类案例:springmvc.zip

  近些日子要做个后台管理连串,就能规划到权力的管理调控,于是就想开 shiro ,上面是上下一心对Spring+shiro的一丝丝知晓,记录下来,一同多搜求:

目录

金沙澳门官网网址 1

类型布局

实例

除开依赖spring-webmvc还亟需信任jackson-databind(用于转移json数据格式)

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.6</version>
</dependency>

品类组织:

金沙澳门官网网址 2

配置web.xml

金沙澳门官网网址 3金沙澳门官网网址 4

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"         version="4.0">    <!-- 请求总控器 -->    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:dispatcher-servlet.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

View Code

配置dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"       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">    <context:component-scan base-package="edu.nf.ch06.controller"/>    <mvc:annotation-driven/>    <mvc:default-servlet-handler/>    <!-- springmvc提供了很多种视图解析器来完成不同的视图解析工作,     例如内部资源视图解析器、模板引擎的视图解析器、XML视图解析器等等     ,而在配置文件中是可以同时配置多个视图解析器的,那么Springmvc     在做视图响应的时候,会根据配置文件中的配置的所有视图解析器按照顺序     进行匹配,直到找到一个合适的视图解析器来完成解析。当有多个视图解析器存在时,     可以通过order属性来指定解析器的优先级(<property name="order" value="0"/>)-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

金沙澳门官网网址 5

恳申请调离控器Controller

(RedirectAttributesController)

金沙澳门官网网址 6金沙澳门官网网址 7

package edu.nf.ch06.controller;import edu.nf.ch06.entity.Users;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.servlet.mvc.support.RedirectAttributes;/** * @author wangl * @date 2018/10/31 * 使用RedirectAttributes解决重定向传参的问题 * 这个对象是在Springmvc 3.1的版本加入的 */@Controllerpublic class RedirectAttributesController {    /**     * 重定向一个JSP页面,使用addAttribute的方式     * 将参数绑定在重定向的url后面带回到页面     * @param user     * @param redirectAttributes     * @return     */    @PostMapping("/redirect3")    public String redirectPage(Users user, RedirectAttributes redirectAttributes){        redirectAttributes.addAttribute("user", user.getUserName;        return "redirect:redirectattr.jsp";    }    /**     * 重定向一个新的请求,使用FlushAttribute的方式,     * 将数据存入一个FlushMap中,它利用了Session的机制保存,     * 在重定向完成之后会自动清除这个对象     *     * @param user     * @param redirectAttributes     * @return     */    @PostMapping("/redirect4")    public String redirectReq(@ModelAttribute("user") Users user, RedirectAttributes redirectAttributes){        redirectAttributes.addFlashAttribute("user", user);        return "redirect:redirectAction";    }    /**     * 从redirect4的请求重定向到这个方法     * @param user     * @return     */    @GetMapping("/redirectAction")    public String getRedirectAttr(@ModelAttribute("user") Users user){        System.out.println(user.getUserName;        return "index";    }}

View Code

(ResponseBodyController)

金沙澳门官网网址 8金沙澳门官网网址 9

package edu.nf.ch06.controller;import edu.nf.ch06.entity.Users;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;/** * @author wangl * @date 2018/10/31 *///@Controller/** * @RestController是在spring4.0后新加入的一个注解, * 同样用于标注一个类为控制器组件,如果当前控制器中所有的 * 请求处理方法都需要以ResponseBody的方式响应,那么 * 就是用这个注解,而不需要再每一个方法上都标注@ResponseBody注解  */@RestControllerpublic class ResponseBodyController {    /**     * 使用@ResponseBody标注,     * 表示将方法的返回值以Response输出流写回客户端,     * 这时,springmvc就会将序列化的数据放入响应体中并写回     * @return     */    @GetMapping("/getUser")    public Users getUser(){        Users user = new Users();        user.setUserName("user1");        user.setAge(21);        return user;    }}

View Code

(RequestBodyController)

金沙澳门官网网址 10金沙澳门官网网址 11

package edu.nf.ch06.controller;import edu.nf.ch06.entity.Users;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;/** * @author wangl * @date 2018/10/31springmvc */@RestControllerpublic class RequestBodyController {    /**     * 接收ajax提交的数据并做类型转换映射     * @param user     * @return     */    @PostMapping("/addUser")    public String addUser(Users user){        System.out.println(user.getUserName;        System.out.println(user.getAge;        return "success";    }    /**     * 是用@RequestBody标注参数,     * 表示将接收的json文本数据进行格式转换后映射到实体中     * @param user     * @return     */    @PostMapping("/addUser2")    public String addUser2(@RequestBody Users user){        System.out.println(user.getUserName;        System.out.println(user.getAge;        return "success";    }}

View Code

(UserController)

金沙澳门官网网址 12金沙澳门官网网址 13

package edu.nf.ch06.controller;import edu.nf.ch06.entity.Users;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.view.RedirectView;import java.util.Map;/** * @author wangl * @date 2018/10/31 */@Controllerpublic class UserController {    /**     * 转发视图     */    @GetMapping("/forwrd1")    public ModelAndView forward1(){        //通过ModelAndView转发        return new ModelAndView("index");    }    @GetMapping("/forwrd2")    public String forward2(){        //通过ModelAndView转发        return "index";    }    /**     * 重定向视图     * @return     */    @GetMapping("/redirect1")    public ModelAndView redirect1(){        //在ModelAndView的构造方法传入一个RedirectView的视图对象来完成重定向        //return new ModelAndView(new RedirectView("redirect.jsp"));        //简化        return new ModelAndView("redirect:redirect.jsp");    }    @GetMapping("/redirect2")    public String redirect2(Users user){        return "redirect:redirect.jsp";    }}

View Code

 

1. pom.xml 配置

1.1. 本子属性音信配置

金沙澳门官网网址 14金沙澳门官网网址 15

 1 <properties>
 2         <!-- base setting -->
 3         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 4         <project.build.locales>zh_CN</project.build.locales>
 5         <project.build.jdk>1.8</project.build.jdk>
 6 
 7         <!-- plugin setting -->
 8         <mybatis.generator.generatorConfig.xml>${basedir}/src/test/java/generatorConfig.xml</mybatis.generator.generatorConfig.xml>
 9         <mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/java/generatorConfig.properties</mybatis.generator.generatorConfig.properties>
10 
11         <!-- plugin versions -->
12         <plugin.mybatis.generator>1.3.1</plugin.mybatis.generator>
13         <plugin.maven-compiler>3.1</plugin.maven-compiler>
14         <plugin.maven-surefire>2.18.1</plugin.maven-surefire>
15         <skipTests>true</skipTests>
16 
17         <!-- lib versions -->
18         <junit.version>4.11</junit.version>
19         <spring.version>4.0.2.RELEASE</spring.version>
20         <mybatis.version>3.2.2</mybatis.version>
21         <mybatis.spring.version>1.2.2</mybatis.spring.version>
22         <mysql.connector.version>5.1.6</mysql.connector.version>
23         <slf4j.version>1.6.6</slf4j.version>
24         <log4j.version>1.2.12</log4j.version>
25         <httpclient.version>4.1.2</httpclient.version>
26         <jackson.version>1.9.13</jackson.version>
27         <druid.version>1.0.5</druid.version>
28         <jstl.version>1.2</jstl.version>
29         <google.collections.version>1.0</google.collections.version>
30         <cglib.version>3.1</cglib.version>
31         <shiro.version>1.2.3</shiro.version>
32         <commons.fileupload.version>1.3.1</commons.fileupload.version>
33         <commons.codec.version>1.9</commons.codec.version>
34         <commons.net.version>3.3</commons.net.version>
35         <aspectj.version>1.6.12</aspectj.version>
36         <netty.version>4.0.18.Final</netty.version>
37         <hibernate.validator.version>5.1.1.Final</hibernate.validator.version>
38     </properties>

View Code

  1.2. 信赖库音信配置

 

金沙澳门官网网址 16金沙澳门官网网址 17

  1     <dependencies>
  2         <!-- springframe start -->
  3         <dependency>
  4             <groupId>org.springframework</groupId>
  5             <artifactId>spring-core</artifactId>
  6             <version>${spring.version}</version>
  7         </dependency>
  8 
  9         <dependency>
 10             <groupId>org.springframework</groupId>
 11             <artifactId>spring-web</artifactId>
 12             <version>${spring.version}</version>
 13         </dependency>
 14 
 15         <dependency>
 16             <groupId>org.springframework</groupId>
 17             <artifactId>spring-oxm</artifactId>
 18             <version>${spring.version}</version>
 19         </dependency>
 20 
 21         <dependency>
 22             <groupId>org.springframework</groupId>
 23             <artifactId>spring-tx</artifactId>
 24             <version>${spring.version}</version>
 25         </dependency>
 26 
 27         <dependency>
 28             <groupId>org.springframework</groupId>
 29             <artifactId>spring-jdbc</artifactId>
 30             <version>${spring.version}</version>
 31         </dependency>
 32 
 33         <dependency>
 34             <groupId>org.springframework</groupId>
 35             <artifactId>spring-webmvc</artifactId>
 36             <version>${spring.version}</version>
 37         </dependency>
 38 
 39         <dependency>
 40             <groupId>org.springframework</groupId>
 41             <artifactId>spring-aop</artifactId>
 42             <version>${spring.version}</version>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>org.springframework</groupId>
 47             <artifactId>spring-context-support</artifactId>
 48             <version>${spring.version}</version>
 49         </dependency>
 50 
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-test</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56         <!-- springframe end -->
 57 
 58         <!-- shiro -->
 59         <dependency>
 60             <groupId>org.apache.shiro</groupId>
 61             <artifactId>shiro-spring</artifactId>
 62             <version>${shiro.version}</version>
 63         </dependency>
 64         <dependency>
 65             <groupId>org.apache.shiro</groupId>
 66             <artifactId>shiro-ehcache</artifactId>
 67             <version>${shiro.version}</version>
 68         </dependency>
 69         <dependency>
 70             <groupId>org.apache.shiro</groupId>
 71             <artifactId>shiro-core</artifactId>
 72             <version>${shiro.version}</version>
 73         </dependency>
 74         <dependency>
 75             <groupId>org.apache.shiro</groupId>
 76             <artifactId>shiro-web</artifactId>
 77             <version>${shiro.version}</version>
 78         </dependency>
 79         <dependency>
 80             <groupId>org.apache.shiro</groupId>
 81             <artifactId>shiro-quartz</artifactId>
 82             <version>${shiro.version}</version>
 83         </dependency>
 84 
 85         <!-- mybatis start-->
 86         <dependency>
 87             <groupId>org.mybatis</groupId>
 88             <artifactId>mybatis</artifactId>
 89             <version>${mybatis.version}</version>
 90         </dependency>
 91 
 92         <dependency>
 93             <groupId>org.mybatis</groupId>
 94             <artifactId>mybatis-spring</artifactId>
 95             <version>${mybatis.spring.version}</version>
 96         </dependency>
 97         <!--mybatis end-->
 98 
 99         <!-- mysql-connector -->
100         <dependency>
101             <groupId>mysql</groupId>
102             <artifactId>mysql-connector-java</artifactId>
103             <version>${mysql.connector.version}</version>
104         </dependency>
105 
106         <!-- DruidDataSource -->
107         <dependency>
108             <groupId>com.alibaba</groupId>
109             <artifactId>druid</artifactId>
110             <version>${druid.version}</version>
111         </dependency>
112 
113         <!-- jackson -->
114         <dependency>
115             <groupId>org.codehaus.jackson</groupId>
116             <artifactId>jackson-mapper-asl</artifactId>
117             <version>${jackson.version}</version>
118         </dependency>
119 
120         <!-- log start -->
121         <dependency>
122             <groupId>log4j</groupId>
123             <artifactId>log4j</artifactId>
124             <version>${log4j.version}</version>
125         </dependency>
126         <dependency>
127             <groupId>org.slf4j</groupId>
128             <artifactId>slf4j-api</artifactId>
129             <version>${slf4j.version}</version>
130         </dependency>
131         <dependency>
132             <groupId>org.slf4j</groupId>
133             <artifactId>slf4j-log4j12</artifactId>
134             <version>${slf4j.version}</version>
135         </dependency>
136         <!-- log end -->
137 
138         <!-- start apache -->
139         <dependency>
140             <groupId>commons-fileupload</groupId>
141             <artifactId>commons-fileupload</artifactId>
142             <version>${commons.fileupload.version}</version>
143         </dependency>
144 
145         <dependency>
146             <groupId>org.apache.httpcomponents</groupId>
147             <artifactId>httpclient</artifactId>
148             <version>${httpclient.version}</version>
149         </dependency>
150 
151         <dependency>
152             <groupId>commons-codec</groupId>
153             <artifactId>commons-codec</artifactId>
154             <version>${commons.codec.version}</version>
155         </dependency>
156 
157         <dependency>
158             <groupId>commons-net</groupId>
159             <artifactId>commons-net</artifactId>
160             <version>${commons.net.version}</version>
161         </dependency>
162 
163         <dependency>
164             <groupId>commons-logging</groupId>
165             <artifactId>commons-logging</artifactId>
166             <version>1.1.3</version>
167         </dependency>
168         <dependency>
169             <groupId>commons-collections</groupId>
170             <artifactId>commons-collections</artifactId>
171             <version>3.2.1</version>
172         </dependency>
173         <!-- end apache -->
174  
175         <!-- google -->
176         <dependency>
177             <groupId>com.google.collections</groupId>
178             <artifactId>google-collections</artifactId>
179             <version>${google.collections.version}</version>
180         </dependency>
181 
182         <!-- cglib -->
183         <dependency>
184             <groupId>cglib</groupId>
185             <artifactId>cglib-nodep</artifactId>
186             <version>${cglib.version}</version>
187         </dependency>
188  
189         <!-- aspectjweaver -->
190         <dependency>
191             <groupId>org.aspectj</groupId>
192             <artifactId>aspectjweaver</artifactId>
193             <version>${aspectj.version}</version>
194         </dependency>
195         <dependency>
196             <groupId>org.aspectj</groupId>
197             <artifactId>aspectjrt</artifactId>
198             <version>${aspectj.version}</version>
199         </dependency>
200 
201         <!-- hibernate-validator -->
202         <dependency>
203             <groupId>org.hibernate</groupId>
204             <artifactId>hibernate-validator</artifactId>
205             <version>${hibernate.validator.version}</version>
206         </dependency>
207  
208         <!-- netty -->
209         <dependency>
210             <groupId>io.netty</groupId>
211             <artifactId>netty-all</artifactId>
212             <version>${netty.version}</version>
213         </dependency>
214  
215         <!-- junit -->
216         <dependency>
217             <groupId>junit</groupId>
218             <artifactId>junit</artifactId>
219             <version>${junit.version}</version>
220             <scope>test</scope>
221         </dependency>
222 
223         <!-- servlet api -->
224         <dependency>
225             <groupId>javax.servlet</groupId>
226             <artifactId>javax.servlet-api</artifactId>
227             <version>3.0.1</version>
228             <scope>provided</scope>
229         </dependency>
230 
231         <!-- jstl -->
232         <dependency>
233             <groupId>javax.servlet</groupId>
234             <artifactId>jstl</artifactId>
235             <version>${jstl.version}</version>
236         </dependency>
237     </dependencies>

View Code

 

  1.3. 编写翻译及tomcat布署配置

金沙澳门官网网址 18金沙澳门官网网址 19

 1     <build>
 2         <finalName>shiro01</finalName>
 3         <plugins>
 4             <!-- web.xml 配置 -->
 5             <plugin>
 6                 <groupId>org.apache.maven.plugins</groupId>
 7                 <artifactId>maven-war-plugin</artifactId>
 8                 <version>2.6</version>
 9                 <configuration>
10                     <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
11                 </configuration>
12             </plugin>
13             <!-- Tomcat 部署至本机 -->
14             <plugin>
15                 <groupId>org.apache.tomcat.maven</groupId>
16                 <artifactId>tomcat7-maven-plugin</artifactId>
17                 <version>2.2</version>
18                 <configuration>
19                   <url>http://localhost:8080/manager</url>
20                   <server>tomcat</server>
21                   <username>hunter</username> 
22                   <password>hunter</password>
23                   <path>/shiro01</path>
24                   <contextReloadable>true</contextReloadable>
25                 </configuration>
26               </plugin>
27               <!-- Mybatis generator代码生成插件 配置 -->
28               <plugin>
29                 <groupId>org.mybatis.generator</groupId>
30                 <artifactId>mybatis-generator-maven-plugin</artifactId>
31                 <version>${plugin.mybatis.generator}</version>
32                 <configuration>
33                     <configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
34                     <overwrite>true</overwrite>
35                     <verbose>true</verbose>
36                 </configuration>
37               </plugin>
38         </plugins>
39     </build>

View Code

  

2. spring-mvc.xml 核心配置

金沙澳门官网网址 20金沙澳门官网网址 21

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 8        xmlns:p="http://www.springframework.org/schema/p"
 9        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
10         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
11         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
12         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
14 
15     <!-- 扫描controller(controller层注入) -->
16     <context:component-scan base-package="com.hunter.shiro.web.controller"/>
17 
18     <!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的 -->
19     <!-- 指定自己定义的validator -->
20     <mvc:annotation-driven validator="validator"/>
21 
22     <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 -->
23     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
24         <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
25         <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
26         <property name="validationMessageSource" ref="messageSource"/>
27     </bean>
28 
29     <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) -->
30     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
31         <property name="basenames">
32             <list>
33                 <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找 -->
34                 <value>classpath:messages</value>
35                 <value>classpath:org/hibernate/validator/ValidationMessages</value>
36             </list>
37         </property>
38         <property name="useCodeAsDefaultMessage" value="false"/>
39         <property name="defaultEncoding" value="UTF-8"/>
40         <property name="cacheSeconds" value="60"/>
41     </bean>
42 
43     <mvc:interceptors>
44         <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
45     </mvc:interceptors>
46 
47     <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
48         <property name="defaultLocale" value="zh_CN"/>
49     </bean>
50 
51     <!-- 支持返回json(避免IE在ajax请求时,返回json出现下载 ) -->
52     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
53         <property name="messageConverters">
54             <list>
55                 <ref bean="mappingJacksonHttpMessageConverter"/>
56             </list>
57         </property>
58     </bean>
59     <bean id="mappingJacksonHttpMessageConverter"
60           class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
61         <property name="supportedMediaTypes">
62             <list>
63                 <value>text/plain;charset=UTF-8</value>
64                 <value>application/json;charset=UTF-8</value>
65             </list>
66         </property>
67     </bean>
68     <!-- 支持返回json -->
69 
70     <!-- 对模型视图添加前后缀 -->
71     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
72           p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
73 
74     <!-- 启用shrio授权注解拦截方式 -->
75     <aop:config proxy-target-class="true"></aop:config>
76     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
77         <property name="securityManager" ref="securityManager"/>
78     </bean>
79 </beans>

View Code

 

3. server.properties 配置

金沙澳门官网网址 22金沙澳门官网网址 23

##JDBC Global Setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hunter_shiro?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=hunter

##DataSource Global Setting

#配置初始化大小、最小、最大
ds.initialSize=1
ds.minIdle=1
ds.maxActive=20

#配置获取连接等待超时的时间 
ds.maxWait=60000

#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
ds.timeBetweenEvictionRunsMillis=60000

#配置一个连接在池中最小生存的时间,单位是毫秒
ds.minEvictableIdleTimeMillis=300000

View Code

 

4. spring-mybatis.xml 配置

金沙澳门官网网址 24金沙澳门官网网址 25

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
  5        xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6        xmlns:cache="http://www.springframework.org/schema/cache"
  7        xsi:schemaLocation="
  8     http://www.springframework.org/schema/context
  9     http://www.springframework.org/schema/context/spring-context.xsd
 10     http://www.springframework.org/schema/beans
 11     http://www.springframework.org/schema/beans/spring-beans.xsd
 12     http://www.springframework.org/schema/tx
 13     http://www.springframework.org/schema/tx/spring-tx.xsd
 14     http://www.springframework.org/schema/jdbc
 15     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
 16     http://www.springframework.org/schema/cache
 17     http://www.springframework.org/schema/cache/spring-cache.xsd
 18     http://www.springframework.org/schema/aop
 19     http://www.springframework.org/schema/aop/spring-aop.xsd
 20     http://www.springframework.org/schema/util
 21     http://www.springframework.org/schema/util/spring-util.xsd">
 22 
 23     <!-- 自动扫描shiro.web包 ,将带有注解的类 纳入spring容器管理 -->
 24     <context:component-scan base-package="com.hunter.shiro.web"></context:component-scan>
 25 
 26     <!-- 引入配置文件 -->
 27     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 28         <property name="locations">
 29             <list>
 30                 <value>classpath*:server.properties</value>
 31             </list>
 32         </property>
 33     </bean>
 34 
 35     <!-- dataSource 配置 -->
 36     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 37         <!-- 基本属性 url、user、password -->
 38         <property name="url" value="${jdbc.url}"/>
 39         <property name="username" value="${jdbc.username}"/>
 40         <property name="password" value="${jdbc.password}"/>
 41 
 42         <!-- 配置初始化大小、最小、最大 -->
 43         <property name="initialSize" value="${ds.initialSize}"/>
 44         <property name="minIdle" value="${ds.minIdle}"/>
 45         <property name="maxActive" value="${ds.maxActive}"/>
 46 
 47         <!-- 配置获取连接等待超时的时间 -->
 48         <property name="maxWait" value="${ds.maxWait}"/>
 49 
 50         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
 51         <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/>
 52 
 53         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
 54         <property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/>
 55 
 56         <property name="validationQuery" value="SELECT 'x'"/>
 57         <property name="testWhileIdle" value="true"/>
 58         <property name="testOnBorrow" value="false"/>
 59         <property name="testOnReturn" value="false"/>
 60 
 61         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
 62         <property name="poolPreparedStatements" value="false"/>
 63         <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
 64 
 65         <!-- 配置监控统计拦截的filters -->
 66         <property name="filters" value="stat"/>
 67     </bean>
 68 
 69     <!-- myBatis文件 -->
 70     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 71         <property name="dataSource" ref="dataSource" />
 72         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
 73         <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
 74         <property name="mapperLocations" value="classpath:mappers/*.xml" />
 75     </bean>
 76 
 77     <!-- spring与mybatis整合配置,扫描所有mapper -->
 78     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 79         <property name="basePackage" value="com.hunter.shiro.web.mapper" />
 80         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 81     </bean>
 82 
 83     <!-- 对dataSource 数据源进行事务管理 -->
 84     <bean id="transactionManager"
 85         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 86         <property name="dataSource" ref="dataSource" />
 87     </bean>
 88 
 89     <!-- 拦截器方式配置事物 -->
 90     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
 91         <tx:attributes>
 92             <!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 -->
 93             <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable" />
 94             <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable" />
 95             <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable" />
 96             <!-- get,find,select,count开头的方法,开启只读,提高数据库访问性能 -->
 97             <tx:method name="get*" read-only="true" />
 98             <tx:method name="find*" read-only="true" />
 99             <tx:method name="select*" read-only="true" />
100             <tx:method name="count*" read-only="true" />
101             <!-- 对其他方法 使用默认的事务管理 -->
102             <tx:method name="*"/>
103         </tx:attributes>
104     </tx:advice>
105 
106     <!-- 事务 aop 配置 -->
107     <aop:config>
108         <aop:pointcut id="serviceMethods" expression="execution(* com.hunter.shiro.web.service..*(..))"/>
109         <aop:advisor advice-ref="transactionAdvice" pointcut-ref="serviceMethods"/>
110     </aop:config>
111 
112     <!-- 配置使Spring采用CGLIB代理 -->
113     <aop:aspectj-autoproxy proxy-target-class="true"/>
114 
115     <!-- 启用对事务注解的支持 -->
116     <tx:annotation-driven transaction-manager="transactionManager"/>
117 
118     <!-- Cache配置 -->
119     <cache:annotation-driven cache-manager="cacheManager"/>
120     <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
121           p:configLocation="classpath:ehcache.xml"/>
122     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
123           p:cacheManager-ref="ehCacheManagerFactory"/>
124 </beans>

View Code

 

5. mybatis-config.xml 配置

本文由金沙澳门官网网址发布于电脑系统,转载请注明出处:金沙澳门官网网址一呼百应视图

关键词: