Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)
1.配置文件
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref = "dataSource"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/alitaobao</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property> </bean> <bean id = "PersonDaoImp" class = "com.cc8w.dao.imp.PersonDaoImp">
<property name="jdbcTemplate" ref = "jdbcTemplate"></property> </bean> </beans>
- <!-- 对静态资源文件的访问 方案二 (二选一) -->
- <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926" />
- <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />
- <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />
2.数据类
package com.cc8w.pojo; public class Person {
private String names;
private String pid;
public Person()
{ }
public Person(String names, String pid) {
this.names = names;
this.pid = pid;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
} }
3.dao接口
package com.cc8w.dao; import java.util.List; import com.cc8w.pojo.Person; public interface PersonDao {
public List<Person> getAllList();
}
4.实现这个dao接口
package com.cc8w.dao.imp; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler; import com.cc8w.dao.PersonDao;
import com.cc8w.pojo.Person; public class PersonDaoImp implements PersonDao {
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
} @Override
public List<Person> getAllList() { String sql = "SELECT * FROM `think_promoter` LIMIT 0 , 30"; final List<Person> personList = new ArrayList<Person>(); jdbcTemplate.query(sql,new RowCallbackHandler(){
@Override
public void processRow(ResultSet rs) throws SQLException {
Person p = new Person();
p.setNames(rs.getNString("groupName"));
p.setPid(rs.getNString("PID"));
personList.add(p);
}
}); return personList;
}
}
5.测试类
package com.cc8w; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cc8w.dao.imp.PersonDaoImp;
import com.cc8w.pojo.Person; //import com.cc8w.Entity.Person; public class Test01 { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDaoImp p1 = (PersonDaoImp)context.getBean("PersonDaoImp");
List<Person> p = p1.getAllList();
for(Person val:p)
{ System.out.println(val.getNames());
} } }
6.结果
本次参考文章:http://blog.csdn.net/mypanlong/article/details/46984535
下次实验:有C3P0的配置,链接数据库
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- spring MVC 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 配置自动扫描包 -->
<context:component-scan base-package="com.cc8w.control"></context:component-scan> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 引入外部配置文件 Spring 2.5-->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 引入jdbc配置文件 (通过bean) Spring 2.0-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean> <bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value = "${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref = "dataSource"></property>
</bean> <bean id = "PersonDaoImp" class = "com.cc8w.dao.imp.PersonDaoImp">
<property name="jdbcTemplate" ref = "jdbcTemplate"></property> </bean>
</beans>
jdbc.properties文件
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/alitao
username = root
password =
下次参考:http://www.cnblogs.com/caoyc/p/5630622.html
Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)的更多相关文章
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- SpringBoot学习(二)——Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 一.@Configuration 和 @Bean Spring的Java配置方式是通过@Configuration和@Bean ...
- Spring的Java配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1 @Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration ...
- SpringBoot学习(二)-->Spring的Java配置方式
二.Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @Bean Spring的Java配置方式是通过 @ ...
- Spring 的java 配置方式
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.1@Configuration 和 @Bean Spring的Java配置方式是通过 @Configuration 和 @ ...
- Spring AOP 不同配置方式产生的冲突问题
Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...
- 理解class.forName() ---使用jdbc方式链接数据库时会经常看到这句代码
目录(?)[-] 官方文档 类装载 两种装载方法的区别 不同的类装载器 是否实例化类 在jdbc链接数据库中的应用 资源 原文地址:http://yanwushu.sinaapp.com/clas ...
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- Spring有哪些配置方式
1.XML 配置文件. Bean 所需的依赖项和服务在 XML 格式的配置文件中指定.这些配置文件通常包含许多 bean 定义和特定于应用程序的配置选项.它们通常以 bean 标签开头.例如: < ...
随机推荐
- 笔记本wifi热点设置好后,手机连上但不能上网问题
这个问题我遇到过,我的原因是因为电脑上装有安全防护软件360的原因 解决方法是:打开360-->找到功能大全中的流量防火墙-->打开局域网防护-->关闭局域网隐身功能,立刻解决了这个 ...
- Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layou
android无法静态显示ui效果. Missing styles. Is the correct theme chosen for this layout? Use the Theme combo ...
- C#.NET常见问题(FAQ)-list比数组效率低多少
对于List,即长度不确定的数组而言,十万笔数据*12倍,就是120万笔数据,只需要93ms左右 换成了二维数组,效果也是差不多,78ms,可见list的效率只比double差一点点 更多 ...
- AutoPlay Menu Builder入门教程
1 拖动窗口可以设置主界面的窗口大小,在下面有版面预览 2 常用东西介绍.右侧的素材库除了按钮还有图像,背景,音乐等.使用按钮的时候选中需要的按钮样式,双击即可上屏.图形按钮即使可以使用图像作为背景的 ...
- 在MyEclipse中设置jsp页面为默认utf-8编码(转)
http://www.cnblogs.com/xdp-gacl/p/3496161.html 在MyEclispe中创建Jsp页面,Jsp页面的默认编码是“ISO-8859-1”,如下图所示: 在这种 ...
- HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)
题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路: 设A = |(1, 1),(1, 0) ...
- Phpcms没有找到网址列表
今天在搞phpcms的采集遇到了这个问题. 没有找到网址列表,请先进行网址采集. 百度,google了好久: 网上答案: 方案1 1.尝试清除 v9_collection_history 表里的内容 ...
- 【#254_DIV2】-A B C
题目链接:http://codeforces.com/contest/445 解题报告: 俄国人今天不知道为什么九点钟就比赛了.仅仅过了两道题,第三题全然没思路,有时间单独去刷第三题吧,看起来非常难 ...
- Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册
Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册 ctypes 是一个Python 标准库中的一个库.为了实现调用 DLL,或者共享库等C数据类型而设计.它可以把这 ...
- webservice系统学习笔记7-异常处理
接口类:IMyService.java @WebResult(name="testExceptionResult") public void testException() thr ...