springMVC+ibatis数据持久化入门级学习例子
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&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数据持久化入门级学习例子的更多相关文章
- Android开发学习之路--数据持久化之初体验
上班第一天,虽然工作上处于酱油模式,但是学习上依旧不能拉下,接着学习android开发吧,这里学习数据持久化的 知识. 其实数据持久化就是数据可以保存起来,一般我们保存数据都是以文件,或者数据库的形式 ...
- IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)
IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...
- iOS学习之数据持久化详解
前言 持久存储是一种非易失性存储,在重启设备时也不会丢失数据.Cocoa框架提供了几种数据持久化机制: 1)属性列表: 2)对象归档: 3)iOS的嵌入式关系数据库SQLite3: 4)Core Da ...
- Docker 容器数据 持久化(系统学习Docker05)
写在前面 本来是可以将数据存储在 容器内部 的.但是存在容器内部,一旦容器被删除掉或者容器毁坏(我亲身经历的痛,当时我们的大数据平台就是运行在docker容器内,有次停电后,不管怎样容器都起不来.以前 ...
- Redis学习总结(1)——数据持久化
以前研究Redis的时候,很多东西都不太明白,理解得也不太深,现在有时间重新拾起来看看,将一些心得记录下来,希望和大家一起探讨. 一.简介 Redis是一个单线程高可用的Key-Value存储系统,和 ...
- Redis学习笔记(5)——Redis数据持久化
出处http://www.cnblogs.com/xiaoxi/p/7065328.html 一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存 ...
- redis学习——数据持久化
一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内存中的数据持久 ...
- redis学习(九)——数据持久化
一.概述 Redis的强大性能很大程度上都是因为所有数据都是存储在内存中的,然而当Redis重启后,所有存储在内存中的数据将会丢失,在很多情况下是无法容忍这样的事情的.所以,我们需要将内存中的数据持久 ...
- python学习总结----内置函数及数据持久化
抽象基类(了解) - 说明: - 抽象基类就是为了统一接口而存在的 - 它不能进行实例化 - 继承自抽象类的子类必须实现抽象基类的抽象方法 - 示例: from abc import ABC, abs ...
随机推荐
- 关于火狐浏览器不支持img onerror的办法
项目中,要使用到缺省图,除了火狐浏览器,其它浏览器都支持img onerror事件.我使用到的解决的办法就是给图片的外层标签加背景,背景图用的就是缺省图. 上代码 <img src=" ...
- http: URL、请求方式
URL:统一资源定位符,可以从互联网得到和访问资源,是标准资源的位置 结构包括协议.服务器名称(IP地址).端口(有的服务器需要).路径.文件名 协议:告诉浏览器如何处理打开的文件,常用的就是http ...
- web应用中webapp. root重用问题解决方案
同一个tomcat服务器里面部署两个JavaEE项目,都是用了log4j做日志.并且web.xml里面都监听了日志信息. 启动服务的时候报错. 于是在web.xml添加以下代码: <di ...
- C#学习笔记(第1周作业)
受队友诱惑,去听了李强的C#公选课,第二天就完成作业了. 作业要求: 1. 小学生加法程序: 2. 能自由设置难度: 3. 对结果进行统计. 第一次写C#程序,遇到不少困难,和队友讨论,百度谷歌一齐上 ...
- Spring MVC的启动过程
一.概述 下面一个基本的运用springMVC的的web.xml的配置,这里要注意两个地方,一个是ContextLoadListener,一个是DispatcherServlet.web容器正是通过这 ...
- Ubuntu14.04忘记root密码的解决方法
电脑20多天没用忘记密码了,下面是在网上找到的一个解决办法,其它的和这个也大概相同.因为其中有些缺漏,没能给我解决问题.通过分析最终问题还是解决了,现解决方案的关键点记录一下.希望能方便到其它人. 1 ...
- 版本控制器 (Svn,Git)
Svn: 集中式版本控制器,首先开发者在开始新一天的工作之前必须从服务器获取代码,然后进入自己的分支开发,开发完成后把自己的分支合并到主分支上进行提交,解决冲突.所有的版本信息都放在服务器上.如果脱离 ...
- sublime text3的配置(整理)
一.代码片段 开发人员很多时候是在做一些重复的工作. 针对不同数据表的增删改查都差不多,重复来重去的.很久不写程序了,利用十一假期在家看看书,写写程序. 最近一直很喜欢使用Sublime Text,发 ...
- Delphi XE5教程11:Tokens
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...
- 【转载】MySQL 5.6主从Slave_IO_Running:Connecting/error connecting to master *- retry
原文地址:MySQL 5.6主从Slave_IO_Running:Connecting/error connecting to master *- retry 作者:忆雨林枫 刚配置的MySQL主从, ...