1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

2.application.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="handler" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="test.do">testControllerAction</prop>
</props>
</property>
</bean> <bean id="testControllerAction" class="org.ue.action.TestControllerAction">
<property name="pageSize">
<value>20</value>
</property>
<property name="successView">
<value>list.jsp</value>
</property>
</bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:db-properties.properties</value>
</property>
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:sqlmap-config.xml</value>
</property>
</bean> <bean id="studentDao" class="org.ue.dao.impl.StudentDaoImpl">
<property name="dataSource" ref="ds"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean> </beans>

3.db-properties.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8
jdbc.username=test
jdbc.password=test

4.sqlmap-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /> <!--
Identify all SQL Map XML files to be loaded by this SQL map.
Notice the paths are relative to the classpath.
-->
<sqlMap resource="sqlmap-test.xml" /> </sqlMapConfig>

5.sqlmap-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap namespace="ts">
<typeAlias alias="stu" type="org.ue.po.Student" /> <select id="listStu" resultClass="stu">
select id, name from tbl_student
</select>
</sqlMap>

6.Action

public class TestControllerAction implements Controller {

    private String successView;
private String pageSize;
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("---pageSize---" + pageSize);
request.setAttribute("pageSize", pageSize);
return new ModelAndView(successView);
} public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
} }

7.dao

public interface StudentDao {
public List getStudent() throws Exception;
}
public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao { /* (non-Javadoc)
* @see org.ue.dao.StudentDao#getStudent()
*/
public List getStudent() throws Exception{
// TODO Auto-generated method stub
return getSqlMapClientTemplate().queryForList("listStu",null);
} }

8.po

public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }

9.list.jsp

<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> <%
String pageSize = (String)request.getAttribute("pageSize");
out.println("pageSize:" + pageSize);
%>
</head> <body>
This is my JSP page. <br>
<%
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//DataSource ds = (DataSource) ac.getBean("ds");
//System.out.println(ds.getConnection().getMetaData().getDriverName()); StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
List ls = studentDao.getStudent();
for (int i = 0; i < ls.size(); i++) {
Student stu = (Student)ls.get(i);
out.println("<strong>--id--"+stu.getId()+"--name--"+stu.getName()+"</strong><br/>");
}
%>
</body>
</html>

10.数据库脚本test.tbl_student

create database if not exists `test`;

USE `test`;

/*数据表 `tbl_student` 的表结构*/

CREATE TABLE `tbl_student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk; /*数据表 `tbl_student` 的数据*/ insert into `tbl_student` values (1,'hnhj');
insert into `tbl_student` values (2,'zndx');
insert into `tbl_student` values (3,'steve');
insert into `tbl_student` values (4,'china');

springMVC+ibatis数据持久化入门级学习例子的更多相关文章

  1. Android开发学习之路--数据持久化之初体验

    上班第一天,虽然工作上处于酱油模式,但是学习上依旧不能拉下,接着学习android开发吧,这里学习数据持久化的 知识. 其实数据持久化就是数据可以保存起来,一般我们保存数据都是以文件,或者数据库的形式 ...

  2. IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)

    IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...

  3. iOS学习之数据持久化详解

    前言 持久存储是一种非易失性存储,在重启设备时也不会丢失数据.Cocoa框架提供了几种数据持久化机制: 1)属性列表: 2)对象归档: 3)iOS的嵌入式关系数据库SQLite3: 4)Core Da ...

  4. Docker 容器数据 持久化(系统学习Docker05)

    写在前面 本来是可以将数据存储在 容器内部 的.但是存在容器内部,一旦容器被删除掉或者容器毁坏(我亲身经历的痛,当时我们的大数据平台就是运行在docker容器内,有次停电后,不管怎样容器都起不来.以前 ...

  5. Redis学习总结(1)——数据持久化

    以前研究Redis的时候,很多东西都不太明白,理解得也不太深,现在有时间重新拾起来看看,将一些心得记录下来,希望和大家一起探讨. 一.简介 Redis是一个单线程高可用的Key-Value存储系统,和 ...

  6. Redis学习笔记(5)——Redis数据持久化

    出处http://www.cnblogs.com/xiaoxi/p/7065328.html 一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存 ...

  7. redis学习——数据持久化

    一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内存中的数据持久 ...

  8. redis学习(九)——数据持久化

    一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内存中的数据持久 ...

  9. python学习总结----内置函数及数据持久化

    抽象基类(了解) - 说明: - 抽象基类就是为了统一接口而存在的 - 它不能进行实例化 - 继承自抽象类的子类必须实现抽象基类的抽象方法 - 示例: from abc import ABC, abs ...

随机推荐

  1. spring事务配置的坑

    基于 <tx> 命名空间的声明式事务管理 前面两种声明式事务配置方式奠定了 Spring 声明式事务管理的基石.在此基础上,Spring 2.x 引入了 <tx> 命名空间,结 ...

  2. mybatis 聚合查询

    <resultMap id="ExtResultMap" type="com.demo.partner.po.PartnerPO"> <id ...

  3. C++ Maps 映射

    C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空 ...

  4. NSS_06 extjs弹出窗口上的文本框默认获得焦点

    这个问题其实是个窗户纸, 没什么技术含量,但是做的过程中有点曲折, 所以也记录下来吧. Ext.window.Window中有focus(o1, o2)方法, 作用:Try to focus this ...

  5. js中ajax异步导致的一些问题

    问题1:ajax默认是异步,所以在ajax中对外面定义的变量赋值,不能正确赋值 $("form").submit( var flag; $.ajax({ type: 'GET', ...

  6. cadence 16.6 Pspice 仿真步骤

    从ADI官网下载后缀为 cir 的文件,AD8210 为例 进行仿真 1 打开 Cadence -> Release 16.6 -> PSpice Accessories -> Mo ...

  7. linux终端io笔记

    简介 终端的两种工作模式:以行为单位的工作模式,以字符数或时间为单位自定义模式 终端判断函数: int isatty(int fd) 终端属性的获取与设置: int tcgetattr(int fd, ...

  8. [ios]ios的延迟执行方法

    1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法   2.使用类别,用BOLCK执行 [代码]c#/cpp/oc代码 ...

  9. UML 小结(6)- UML九种图的比较与学习

    UML中的九种图: 用例图.类图.对象图.状态图.时序图.协作图.活动图.部署图.构件图. 1)用例图(Use Case Diagram) 它是UML中最简单也是最复杂的一种图.说它简单是因为它采用了 ...

  10. sharepoint 认证

    MCPD http://www.microsoft.com/learning/en/us/mcpd-certification.aspx#fbid=YktyKIYXeFg Exam 70-573: T ...