【spring原始碼學習】spring整合orm資料框架
Love Lenka發表於2017-08-24
【一】簡易的資料來源配置
(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; } }
相關文章
- node 整合sequelize ORM 框架學習2020-11-21ORM框架
- spring學習:spring原始碼_BeanDefinition2024-05-30Spring原始碼Bean
- Spring原始碼深度解析(郝佳)-學習-原始碼解析-Spring MVC2020-12-30Spring原始碼MVC
- 【spring原始碼學習】spring的事務管理的原始碼解析2017-08-07Spring原始碼
- Spring原始碼學習之:spring註解@Transactional2016-10-13Spring原始碼
- 【spring原始碼學習】spring的IOC容器之BeanFactoryPostProcessor介面學習2017-07-26Spring原始碼Bean
- 【spring原始碼學習】Spring的IOC容器之BeanPostProcessor介面學習2017-07-19Spring原始碼Bean
- SSM框架學習之Spring的AOP學習以及資料整理2020-10-12SSM框架Spring
- 【spring原始碼學習】spring的遠端呼叫實現原始碼分析2017-08-04Spring原始碼
- Spring學習之——手寫Mini版Spring原始碼2020-07-28Spring原始碼
- Spring原始碼閱讀學習一2017-05-08Spring原始碼
- java spring 框架學習2015-09-16JavaSpring框架
- Spring原始碼學習之:ClassLoader學習(2)2016-11-07Spring原始碼
- Spring原始碼學習之:ClassLoader學習(3)2016-11-07Spring原始碼
- Spring原始碼學習之:ClassLoader學習(4)2016-11-07Spring原始碼
- JAVA學習Spring整合Mybatis2020-11-26JavaSpringMyBatis
- Spring boot學習(三) Spring boot整合mybatis2019-01-17Spring BootMyBatis
- Mybatisi和Spring整合原始碼分析2022-06-19MyBatisSpring原始碼
- 【spring原始碼學習】Spring @PostConstruct和@PreDestroy例項2018-04-18Spring原始碼Struct
- Spring框架|整合JdbcTemplate2020-05-03Spring框架JDBC
- Spring整合其他框架2020-12-19Spring框架
- springMvc原始碼學習之:spring原始碼總結2016-11-04SpringMVC原始碼
- 【spring原始碼學習】spring的事件釋出監聽機制原始碼解析2017-07-26Spring原始碼事件
- 原始碼學習之Spring容器建立原理2022-11-14原始碼Spring
- Spring原始碼學習之路---IOC初探(二)2018-04-03Spring原始碼
- Spring原始碼學習之:FactoryBean的使用2016-12-30Spring原始碼Bean
- Spring boot學習(四)Spring boot整合Druid2019-01-17Spring BootUI
- Spring Boot系列(三):Spring Boot整合Mybatis原始碼解析2020-08-20Spring BootMyBatis原始碼
- 手寫Spring ioc 框架,狠狠的“Spring 原始碼2020-10-31Spring框架原始碼
- Spring5.0原始碼學習系列之Spring AOP簡述2020-11-26Spring原始碼
- spring原始碼分析之freemarker整合2019-03-25Spring原始碼
- 深入原始碼理解Spring整合MyBatis原理2021-07-29原始碼SpringMyBatis
- Spring原始碼學習之:ClassLoader學習(5)-自測2017-04-01Spring原始碼
- Spring框架(五)實戰Spring整合Mybatis2020-12-17Spring框架MyBatis
- Spring Cloud 原始碼學習之 Hystrix 入門2018-09-16SpringCloud原始碼
- Spring-SpringAOP原始碼學習(上)2022-06-10Spring原始碼
- 學習Spring原始碼篇之環境搭建2022-03-19Spring原始碼
- Spring原始碼學習之:你不知道的spring注入方式2016-11-04Spring原始碼