- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章SpringMVC+MyBatis分页(最新)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
目前主流的Web MVC框架,除了Struts这个主力 外,还有Spring MVC,主要是由于Spring MVC配置比较简单,使用起来也十分明了,非常灵活,与Spring 集成较好,对RESTful API的支持也比struts要好.
MyBatis是ibatis的升级版,作为hibernate的老对手,它 是一个可以自定义SQL、存储过程和高级映射的持久层框架.
与hibernate的主要区别就是mybatis是半自动化的,而hibernate是全自动的,所以当应用需求越来越复杂的时候,自动化的sql显得比较笨拙.
由于前段时间接了个项目要用springmvc做,所以我抱着练手的态度,又玩起了整合框架的游戏。经常搭框架的人应该都清楚,框架搭建的核心就是配置文件。所以我主要贴下几个配置文件的代码。还是那句话,我都是写好配置文件之后,运行报错再加jar。这里列一下我用的jar包(应该是最少的):
备注:上图有一些额外的jar,比如我用的数据库连接池是阿里巴巴的druid、日志框架式logback,所以引入了相关jar。关于这两个框架的使用和配置都是非常简单的,所以这里就不细说.
1.整合SpringMVC 。
springMybatis-servlet.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<?
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">
<!-- 启用spring mvc 注解-->
<
mvc:annotation-driven
>
</
mvc:annotation-driven
>
<!-- 自动扫描的包名 ,使Spring支持自动检测组件,如注解的Controller-->
<
context:component-scan
base-package
=
"com.alibaba.controller"
/>
<
context:component-scan
base-package
=
"com.alibaba.service"
/>
<!-- 视图解析器:定义跳转的文件的前后缀 -->
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<
property
name
=
"prefix"
value
=
"/WEB-INF/jsp/"
/>
<
property
name
=
"suffix"
value
=
".jsp"
/>
<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</
bean
>
<!--配置拦截器, 多个拦截器,顺序执行 -->
<
mvc:interceptors
>
<
mvc:interceptor
>
<!-- 匹配的是url路径 -->
<
mvc:mapping
path
=
"/"
/>
<
mvc:mapping
path
=
"/user/**"
/>
<
mvc:mapping
path
=
"/test/**"
/>
<
bean
class
=
"com.alibaba.interceptor.CommonInterceptor"
></
bean
>
</
mvc:interceptor
>
<!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
</
mvc:interceptors
>
</
beans
>
|
2.整合Mybatis 。
spring-dao.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?
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:mybatis
=
"http://mybatis.org/schema/mybatis-spring"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 该包下的类支持注解,表示会被当作{@code mybatis mapper}处理 配置了之后表示可以自动引入mapper类-->
<
mybatis:scan
base-package
=
"com.alibaba.dao"
/>
<!--引入属性文件 -->
<
context:property-placeholder
location
=
"classpath:configuration.properties"
/>
<!--数据库连接-->
<
bean
id
=
"dataSource"
class
=
"com.alibaba.druid.pool.DruidDataSource"
init-method
=
"init"
destroy-method
=
"close"
>
<
property
name
=
"url"
value
=
"${jdbc.url}"
/>
<
property
name
=
"username"
value
=
"${jdbc.username}"
/>
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
<!-- 配置初始化大小、最小、最大 -->
<
property
name
=
"initialSize"
><
value
>1</
value
></
property
>
<
property
name
=
"maxActive"
><
value
>5</
value
></
property
>
<
property
name
=
"minIdle"
><
value
>1</
value
></
property
>
<!-- 配置获取连接等待超时的时间 -->
<
property
name
=
"maxWait"
><
value
>60000</
value
></
property
>
<!-- 配置监控统计拦截的filters -->
<
property
name
=
"filters"
><
value
>stat</
value
></
property
>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<
property
name
=
"timeBetweenEvictionRunsMillis"
><
value
>60000</
value
></
property
>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<
property
name
=
"minEvictableIdleTimeMillis"
><
value
>300000</
value
></
property
>
<!--
<property name="validationQuery"><value>SELECT 'x'</value></property>
<property name="testWhileIdle"><value>true</value></property>
<property name="testOnBorrow"><value>false</value></property>
<property name="testOnReturn"><value>false</value></property>
<property name="poolPreparedStatements"><value>true</value></property>
<property name="maxOpenPreparedStatements"><value>20</value></property>
-->
</
bean
>
<!-- mybatis配置 -->
<
bean
id
=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
</
beans
>
|
3.web.xml整合SpringMVC和Mybatis 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version
=
"3.0"
>
<!-- 该servlet为tomcat,jetty等容器提供,将静态资源映射从/改为/static/目录,如原来访问 http://localhost/foo.css ,现在http://localhost/static/foo.css -->
<!-- 不拦截静态文件 -->
<
servlet-mapping
>
<
servlet-name
>default</
servlet-name
>
<
url-pattern
>/js/*</
url-pattern
>
<
url-pattern
>/css/*</
url-pattern
>
<
url-pattern
>/images/*</
url-pattern
>
<
url-pattern
>/fonts/*</
url-pattern
>
</
servlet-mapping
>
<!-- 配置字符集 -->
<
filter
>
<
filter-name
>encodingFilter</
filter-name
>
<
filter-class
>org.springframework.web.filter.CharacterEncodingFilter</
filter-class
>
<
init-param
>
<
param-name
>encoding</
param-name
>
<
param-value
>UTF-8</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>forceEncoding</
param-name
>
<
param-value
>true</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>encodingFilter</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
<!-- 初始化 DispatcherServlet时,该框架在 web应用程序WEB-INF目录中寻找一个名为[servlet-名称]-servlet.xml的文件,
并在那里定义相关的Beans,重写在全局中定义的任何Beans -->
<
servlet
>
<
servlet-name
>springMybatis</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>springMybatis</
servlet-name
>
<!-- 所有的的请求,都会被DispatcherServlet处理 -->
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>/WEB-INF/config/spring-*.xml</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<!-- druid web 监控 -->
<
servlet
>
<
servlet-name
>DruidStatView</
servlet-name
>
<
servlet-class
>com.alibaba.druid.support.http.StatViewServlet</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>DruidStatView</
servlet-name
>
<
url-pattern
>/druid/*</
url-pattern
>
</
servlet-mapping
>
<
error-page
>
<
error-code
>404</
error-code
>
<
location
>/error/404.jsp</
location
>
</
error-page
>
<
error-page
>
<
error-code
>500</
error-code
>
<
location
>/error/500.jsp</
location
>
</
error-page
>
</
web-app
>
|
4.logback.xml日志配置 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
configuration
>
<
appender
name
=
"STDOUT"
class
=
"ch.qos.logback.core.ConsoleAppender"
>
<
encoder
>
<
pattern
>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</
pattern
>
</
encoder
>
</
appender
>
<
logger
name
=
"test.LogbackTest"
level
=
"TRACE"
/>
<
logger
name
=
"com.alibaba.controller.TestController"
level
=
"TRACE"
/>
<
logger
name
=
"org.springframework.web.servlet.DispatcherServlet"
level
=
"DEBUG"
/>
<
logger
name
=
"druid.sql"
level
=
"INFO"
/>
<!-- 如果spring-config里面没有配置slf4j,就不会显示sql日志,logback只是slf4j的一个实现 -->
<
root
level
=
"debug"
>
<
appender-ref
ref
=
"STDOUT"
/>
</
root
>
</
configuration
>
|
5.configuration.properties配置 。
1
2
3
|
jdbc.url=jdbc\:mysql\://localhost\:3306/druid?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
jdbc.username=root
jdbc.password=123456
|
6.测试搭建是否成功,后台代码 。
首先是登录,用了加密,可以去掉 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package
com.alibaba.controller;
import
javax.annotation.Resource;
import
javax.servlet.http.HttpServletRequest;
import
org.apache.commons.codec.digest.DigestUtils;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam;
import
com.alibaba.model.User;
import
com.alibaba.service.UserService;
import
com.alibaba.util.RequestUtil;
/**
* @author tfj
* 2014-7-26
*/
@Controller
public
class
SystemController {
private
final
Logger log = LoggerFactory.getLogger(SystemController.
class
);
@Resource
private
UserService userService;
@RequestMapping
(value =
"/"
,method = RequestMethod.GET)
public
String home() {
log.info(
"返回首页!"
);
return
"index"
;
}
@RequestMapping
(value =
"/test/hello"
,method = RequestMethod.GET)
public
String testHello() {
log.info(
"执行了testHello方法!"
);
return
"testHello"
;
}
@RequestMapping
(value =
"/login"
,method = RequestMethod.POST)
public
String testLogin(HttpServletRequest request,
@RequestParam
String username,
@RequestParam
String password) {
log.info(
"执行了testLogin方法!"
);
User user = userService.findUserByName(username);
if
(user!=
null
){
if
(user.getPassword().equals(DigestUtils.md5Hex(password))){
request.getSession().setAttribute(
"userId"
, user.getId());
request.getSession().setAttribute(
"user"
, username);
return
"redirect:"
+ RequestUtil.retrieveSavedRequest();
//跳转至访问页面
}
else
{
log.info(
"密码错误"
);
request.getSession().setAttribute(
"message"
,
"用户名密码错误,请重新登录"
);
return
"login"
;
}
}
else
{
log.info(
"用户名不存在"
);
request.getSession().setAttribute(
"message"
,
"用户名不存在,请重新登录"
);
return
"login"
;
}
}
}
|
关于service和model就不写了,写一下mybatis的mapper类映射 。
1
2
3
4
5
6
7
|
<?
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.alibaba.dao.UserMapper"
>
<
select
id
=
"findUserByName"
resultType
=
"com.alibaba.model.User"
>
select id, username , password from sysuser where username = #{username}
</
select
>
</
mapper
>
|
7.前台jsp主要是登录和登录成功的页面,就不写了 。
贴一下截图:
到此,springmvc+mybatis整合成功。后续复杂的功能待添加 。
注意事项 。
1.框架中关于druid和logback的配置都是从官网上copy下来的,所以都是最基本的,读者可以忽略,也可以换成读者熟悉的数据库组件和日志框架,如c3p0和log4j.
2.代码里加入了权限管理,即访问前需登录,登录后跳转至待访问页面,关于springmvc的权限管理请看:http://www.zzvips.com/article/84142.html 。
3.本文是从我的测试代码里剥离出来的最简单的也是最基本的代码,有些没剥离干净的地方还请见谅.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/tonytfjing/article/details/39203121 。
最后此篇关于SpringMVC+MyBatis分页(最新)的文章就讲到这里了,如果你想了解更多关于SpringMVC+MyBatis分页(最新)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1.概述 转载:MyBatis 二级缓存全详解 上一篇文章中我们介绍到了 MyBatis 一级缓存其实就是 SqlSession 级别的缓存,什么是 SqlSession 级别的缓存呢?一级缓存的本质
1.概述 转载:核心配置综述之StatementHandler 2.MyBatis 四大组件之StatementHandler StatementHandler 是四大组件中最重要的一个对象,负责操作
1.概述 转载:MyBatis 启动流程 MyBatis 是第一个支持自定义 SQL、存储过程和高级映射的类持久框架。MyBatis 消除了大部分 JDBC 的样板代码、手动设置参数以及检索结果。My
1.概述 转载:MyBatis 基础搭建及架构概述 2.MyBatis 是什么? MyBatis是第一个支持自定义SQL、存储过程和高级映射的类持久框架。MyBatis消除了大部分JDBC的样板代码、
1.概述 转载:核心配置综述之 ParameterHandler MyBatis 四大核心组件我们已经了解到了两种,一个是 Executor ,它是MyBatis 解析SQL请求首先会经过的第一道关卡
1.概述 转载:核心配置综述之 ResultSetHandler 我们之前介绍过了MyBatis 四大核心配置之 Executor、StatementHandler、 ParameterHandler
如果我使用mybatis,我可以很容易地得到更新的行数,就像 update table set desc = 'xxx' where name = ? 但是,如果我想获取更新的行数,而不是计数,我该如
如何在MyBatis 3中使用小于等于 SELECT * FROM( SELECT * FROM TABLE1 WHERE COL1 =#{COL1,jdbc
我将 mybatis3.0.6 与 java 一起使用 哪个性能更好? [select id="getData" parameterType="Integer" resultType="Integer
我无法在 mybatis 中使用动态排序类型创建 SQL,如下例 select user_profile.user_profile_id, user_profile.first_name
这是一个流行的例子。 insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL )values (
我下载了MyBatis,文件夹中有一个mybatis-3.0.4-javadoc.jar,我解压并打开它,但它几乎是空的。 哪里可以找到MyBatis的API文档? 最佳答案 http://repo1
我正在尝试为 ArrayList 编写类型处理程序,但这给了我错误,任何人都可以帮助我。 我想将 ArrayList 作为 VARCHAR 存储在数据库中并将其检索为 ArrayList。 packa
目录 依赖 配置 CodeGenerator mybatis-plus-generator + clickhouse 自动生成代码 依赖
目录 三者实现对比 使用fluent mybatis 来实现上面的功能 换成mybatis原生实现效果 换成mybatis plus
例如,我有查询从员工中选择 ID、姓名、年龄、地址,而不是拥有员工对象列表。我希望有一个 map 列表,如 list{ map{ ("id", 123), ("name","jac
我在使 MyBatis (3.4.6) 工作时遇到一些麻烦。 我已将 mybatis-config.xml 文件放置在项目的 src/main/resources 文件夹中,但是当我运行单元测试时,出
我现在使用 Mybatis 和 spring-boot。我没有添加mybatis-config.xml。我根据说明通过 application.properties 为数据源和 mybatis 进行所
这是我的第一篇文章,用我糟糕的英语...... 我使用的是MyBatis3.0 在查询中,我使用 SqlBuilder 的方法如下: public class DataStatisticSqlBuil
主题:MyBatis:Boolean Paraeter:MyBatis 正在使用 Getter 内容: 大家好, 我一直在寻找解决我近乎简单的 MyBatis 问题的方法: 给定代码(仅必要部分):
我是一名优秀的程序员,十分优秀!