Spring_xml方式开发
1. spring核心配置文件:
<?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"
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">
<!-- 读取占位符配置文件,和mybatis的一样 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- apache的连接池依赖类,pom里配置引入commons-dbcp -->
<!-- 这里需要占位符配置url、username、password -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean> <!-- spring提供的jdbc template方便dao层编写,pom引入依赖spring-jdbc -->
<!-- 这里需要注入上面生成好的数据库连接池dataSource -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 自己编写的持久层,内置一个叫template的属性(必须要有setter) -->
<!-- 往属性template注入上面生成好的jdbcTemplate -->
<bean id="userDao" class="dao.daoXmlImpl">
<property name="template" ref="jdbcTemplate"></property>
</bean> <!-- 自己编写的业务层,内置一个叫dao的属性(必须要有setter) -->
<!-- 往属性dao注入上面生成好的userDao -->
<bean id="userService" class="service.xmlImpl">
<property name="dao" ref="userDao"></property>
</bean> </beans>
2.POJO类
public class User {
private int id;
private String username;
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", address=" + address + "]";
}
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3. dao类
public class daoXmlImpl implements userDao {
private JdbcTemplate template;
// 留一个dao的setter让Spring帮忙注入jdbcTemplate
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
@Override
public User getUserById(int id) {
// 业务SQL
String sql = "SELECT * FROM user WHERE id = ?";
// 参数数组
Object[] args = { id };
// 使用jdbcTemplate来查询数据库
return template.queryForObject(sql, args, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setAddress(rs.getString("address"));
return user;
}
});
}
}
4.service类
public class xmlImpl implements userService{
private userDao dao;
// 留一个dao的setter让Spring帮忙注入userDao
public void setDao(userDao dao) {
this.dao = dao;
}
@Override
public User findUser(int id) {
return dao.getUserById(id);
}
}
5.使用类(这里用servlet)
public class userView extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取参数
String id = request.getParameter("id");
// 获取Spring IOC容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过Spring IOC容器获取 service bean
userService service = context.getBean(userService.class);
// 调用service的方法,获取user
User user = service.findUser(Integer.parseInt(id));
// 响应,打印user
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(user);
}
}
Spring_xml方式开发的更多相关文章
- VS 2010 WebSite网站 使用CodeBehide 方式开发[Web应用程序项目转Web网站]
由于生成Web应用程序的文件非常大,100M左右,上传到香港太慢,对于运维工作很不现实, 所以只能改用单个源代码文件上传方式,也就是Web网站方式,但VS2010中只提供Web网站转Web应用程序功能 ...
- mapper代理方式开发
使用mapper代理方式开发: 需要编写mapper接口,UserMapper.java需要编写映射文件,UserMapper.xml需要遵循一些开发规范,mybatis便可以自动生成mapper接口 ...
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
- mybatis入门--mapper代理方式开发
不使用代理开发 之前,我们说了如何搭建mybatis框架以及我们使用mybatis进行简单的增删改查.现在,我们一起来构建一个dao层的完整代码.并用@test来模拟service层对dao层进行一下 ...
- 从零开始学JAVA(09)-使用SpringMVC4 + Mybatis + MySql 例子(注解方式开发)
项目需要,继续学习springmvc,这里加入Mybatis对数据库的访问,并写下一个简单的例子便于以后学习,希望对看的人有帮助.上一篇被移出博客主页,这一篇努力排版整齐,更原创,希望不要再被移出主页 ...
- 《开源框架那点事儿23》:採用TinyDB组件方式开发
採用TinyDB组件方式开发 步骤 Icon 前文介绍四则运算的流程编程开发时,说过流程编排在开发反复功能时.能够利用已有的组件库高速开发.对于开发者而言仅仅须要简单配置流程就能够完毕工作了.开发增删 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型 Office 2 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office的JavaScript对象模型
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office的JavaScript对象模型 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps 集成SP和Office App
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps 集成SP和Office App 你能够用两种 ...
随机推荐
- Nginx split_client模块
一般用户AB测试根据比例调用指定的接口 默认编译进nginx Syntax: split_clients string $variable { ... } Default: — Context: h ...
- python时间模块datetime
datetime模块 datetime在python中比较常用,主要用来处理时间日期,使用前先倒入datetime模块.下面总结下本人想到的几个常用功能. 1.当前时间(日期.小时.字符串时....) ...
- Spring 使用介绍(六)—— AOP(二)
一.切入点语法 1)通配符 AOP支持的通配符: *:匹配任何数量字符 ..:匹配任何数量字符的重复,在类型模式中匹配任何数量子包,在方法参数模式中匹配任何数量参数 +:匹配指定类型的子类型,仅能作为 ...
- 检测某一目录下md5相同的文件
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import jav ...
- [BZOJ 2083] [POI 2010] Intelligence test
Description 霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列.Lyx很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算 ...
- python 脚本之 IP地址探测
#第一种方法#!/usr/bin/env python #_*_ coding:utf8 _*_ #### 该脚本需要使用fping命令 如果没有安装需要提前安装fping #### yum inst ...
- 洛谷P1048采药题解
题目 这是一个裸的01背包,因为题目中没说可以采好多次,不多说上代码, #include<iostream> using namespace std; int main() { int n ...
- Matplotlib学习---用wordcloud画词云(Word Cloud)
画词云首先需要安装wordcloud(生成词云)和jieba(中文分词). 先来说说wordcloud的安装吧,真是一波三折.首先用pip install wordcloud出现错误,说需要安装Vis ...
- pfSense用户界面汉化翻译教程
pfSense用户界面汉化翻译教程 来源 https://blog.51cto.com/fxn2025/2087182 为了记录自己的汉化过程,同时也为了方便网友自己制作汉化版本,我把自己汉化pfSe ...
- Install KVM Hypervisor on arrch64 Linux Server
Install KVM Hypervisor on arrch64 Linux Server 参考链接: https://wiki.ubuntu.com/ARM64/QEMU https://wiki ...