【一】简易的数据源配置

(1)配置文件

<!--springJdbcTemplemate数据操作配置信息 -->

    <bean id="driver" class="com.mysql.jdbc.Driver"></bean>

   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="url"><value>jdbc:mysql://localhost:3306/mobile_thinks</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>shangxiaofei</value></property>
<property name="driver" ref="driver"/>
</bean>
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

(2)测试类

package com.mobile.thinks.service.impl;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service; import com.mobile.thinks.entity.User;
import com.mobile.thinks.service.UserInfoService; @Service(value="userInfoServiceImpl")
public class UserInfoServiceImpl implements UserInfoService{ @Autowired
private JdbcTemplate JdbcTemplate; @Override
public User createUserAcountByUser(String userId) {
//sql
String sql="select * from thinks_user where id='"+userId+"'"; //转换器
RowMapper<User> rowMapper=new RowMapper<User>() { @Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user=new User();
user.setId(rs.getString("id"));
user.setUserName(rs.getString("user_name"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setAddress(rs.getString("address"));
user.setAge(rs.getInt("age"));
user.setCreateTime(rs.getDate("create_time"));
user.setUpdateTime(rs.getDate("update_time"));
return user;
} }; //查询
List<User> users= JdbcTemplate.query(sql,rowMapper);
User user=users.get(0); //创建记录
String insertSql="INSERT INTO thinks_user_acount(id, acount_name, acount_type, amount, user_id, age, create_time, update_time)VALUES('12344567890poiuytrewq', '"+user.getName()+"的账户', '人民币',"+new BigDecimal(88888888)+",'"+user.getId()+"', 28, now(), now());"; JdbcTemplate.execute(insertSql);
return user;
} }

【二】JNDI方式配置在tomcat数据源,使用com.alibaba.druid连接池

(1)将数据库数据源用到的jdbc的jar包和数据库连接池的jar包copy到tomcat解压包的lib目录下

(2)在tomcat的conf目录下的context.xml配置文件中添加jndi数据源的配置

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context> <Resource
name="jdbc/thinkDS"
auth="Container"
type="javax.sql.DataSource"
factory="com.alibaba.druid.pool.DruidDataSourceFactory"
maxActive="10"
minIdle="1"
initialSize="1"
maxWait="10000"
username="root"
password="shangxiaofei"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mobile_thinks"
/> <!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
--> <!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>

(3)在项目的xml配置文件里引用jndi的配置

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- <context:property-placeholder location="classpath:resources.properties"/> --> <!-- 扫描注解Bean -->
<context:component-scan base-package="com.mobile.thinks.**">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation" expression="org.springframework.beans.factory.annotation.Autowired"/>
</context:component-scan> <!--springJdbcTemplemate数据操作配置信息 -->
<bean id="driver" class="com.mysql.jdbc.Driver"></bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="url"><value>jdbc:mysql://localhost:3306/mobile_thinks</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>shangxiaofei</value></property>
<property name="driver" ref="driver"/>
</bean>
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- spring集成jndi数据源配置 -->
<bean id="jndiDataSources" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/thinkDS</value>
</property>
</bean> <bean id="jndiJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jndiDataSources"/>
</bean> <!-- springHibernate继承 --> </beans>

(4)项目中使用jndi数据源

@Service(value="userInfoServiceImpl")
public class UserInfoServiceImpl implements UserInfoService{ @Autowired
private JdbcTemplate JdbcTemplate; @Resource(name="jndiJdbcTemplate")
private JdbcTemplate jndiJdbcTemplate;
@Override
public User findUserById(String userId) {
//sql
String sql="select * from thinks_user where id='"+userId+"'"; //转换器
RowMapper<User> rowMapper=new RowMapper<User>() { @Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user=new User();
user.setId(rs.getString("id"));
user.setUserName(rs.getString("user_name"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setAddress(rs.getString("address"));
user.setAge(rs.getInt("age"));
user.setCreateTime(rs.getDate("create_time"));
user.setUpdateTime(rs.getDate("update_time"));
return user;
} }; //查询
List<User> users= jndiJdbcTemplate.query(sql,rowMapper);
User user=users.get(0);
return user;
}
}

【spring源码学习】spring集成orm数据框架的更多相关文章

  1. spring源码学习——spring整体架构和设计理念

    Spring是在Rod Johnson的<Expert One-On-One J2EE Development and Design >的基础上衍生而来的.主要目的是通过使用基本的java ...

  2. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  3. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  4. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

  5. 【目录】Spring 源码学习

    [目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...

  6. spring源码学习之路---深入AOP(终)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...

  7. spring源码学习之路---IOC初探(二)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...

  8. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  9. Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作

    写在前面 上文 Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件主要讲Spring容器创建时通过XmlBeanDefinitionReader读 ...

  10. spring源码学习(一):eclipse导入spring源码

    前言 对于一门技术,我们最先是了解它(what),然后再熟练的使用它(how)以及何时用它(when),最后肯定要看透它(why).spring作为Java开发人员可以说是最熟悉不过的了,基本每个Ja ...

随机推荐

  1. 【三小时学会Kubernetes!(五) 】完成整个架构

    完成整个架构 现在我们学习了完成架构的所有必须的资源,因此这一节会非常快.图 22 中灰色的部分是需要做的事情.让我们从底部开始:部署 sa-logic 的部署. 图 22:当前应用程序状态 部署 S ...

  2. JavaScript内部原理系列-变量对象(Variable object)

    概要 我们总是会在程序中定义一些函数和变量,之后会使用这些函数和变量来构建我们的系统.然而,对于解释器来说,它又是如何以及从哪里找到这些数据的(函数,变量)?当引用一个对象的时候,在解释器内部又发生了 ...

  3. 数据库原理及应用-SQL数据操纵语言(Data Manipulation Language)和嵌入式SQL&存储过程

    2018-02-19 18:03:54 一.数据操纵语言(Data Manipulation Language) 数据操纵语言是指插入,删除和更新语言. 二.视图(View) 数据库三级模式,两级映射 ...

  4. Python实现H5页面

    from selenium import webdriver mobile_emulation = {'deviceName':'iPhone X'} options = webdriver.Chro ...

  5. flask学习(九):模板渲染和参数传递

    一. 如何渲染模板 1. 模板放在templates文件夹下 2. 从flask中导入render_template函数 3. 在视图函数中,使用render_template函数,渲染模板 注意:只 ...

  6. hdu4347The Closest M Points kdtree

    kdtree讲解: https://blog.csdn.net/qing101hua/article/details/53228668 https://blog.csdn.net/acdreamers ...

  7. mybatis定义拦截器

    applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...

  8. mysql数据库基础知识和认识

    mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail mysql -u root -ppassworduse mysql;insert into user(h ...

  9. session、cookie、viewstate

    session的用法 定义:保存在服务器内存的数据,sesson 只应该应用在需要跨页面且与每个访问用户相关的变量和对象存储上,session在默认情况下20分钟就过期,在页面之中最好不要过多使用,因 ...

  10. SpringInAction--条件化的Bean

    学习了profile bean之后,发现有的时候bean还是有根据环境来选择的余地的,那么假设我们希望某个bean只有当另外某个特定的bean也声明了之后才会创建.我们还可能要求只有某个特定的环境变量 ...