05_ssm基础之Spring与持久层的整合
Posted 姿势帝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了05_ssm基础之Spring与持久层的整合相关的知识,希望对你有一定的参考价值。
30.31.spring对jdbc的支持jdbcTemplate
使用Spring的JDBC准备:
1):拷贝jar:
mysql-connector-java-5.1.11.jar:MySQL驱动包
spring-jdbc-4.1.2.RELEASE.jar:支持JDBC
spring-tx-4.1.2.RELEASE.jar: 支持事务
2): 操作Product对象的DAO实现:
3):参考文档:参考Spring4.x文档的362页.
代码:如下
spring配置文件代码
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!-- 读取配置文件--> 10 <context:property-placeholder location="classpath:db.properties"/> 11 <!--配置dataSources--> 12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 13 <property name="driverClassName" value="${mysql.driverClass}"/> 14 <property name="url" value="${mysql.JdbcUrl}"/> 15 <property name="username" value="${mysql.User}"/> 16 <property name="password" value="${mysql.Password}"/> 17 </bean> 18 19 <bean id="product2Dao" class="com.day02.ssm.spring.dao.impl.Product2Dao"> 20 <property name="dataSource" ref="dataSource"/> 21 </bean> 22 </beans>
dao是实现代码:
1 package com.day02.ssm.spring.dao.impl; 2 3 import com.day02.ssm.spring.dao.IProductDao; 4 import com.day02.ssm.spring.model.Product; 5 import org.apache.commons.dbcp.BasicDataSource; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.jdbc.core.JdbcTemplate; 8 import org.springframework.jdbc.core.RowMapper; 9 10 import javax.sql.DataSource; 11 import java.sql.Connection; 12 import java.sql.PreparedStatement; 13 import java.sql.ResultSet; 14 import java.sql.SQLException; 15 16 /** 17 * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 18 * 疑问咨询wx:851298348 19 */ 20 public class Product2Dao implements IProductDao { 21 private JdbcTemplate jdbcTemplate; 22 23 24 public void setDataSource(DataSource dataSource) { 25 this.jdbcTemplate = new JdbcTemplate(dataSource); 26 } 27 28 @Override 29 public void save(Product product) { 30 String sql = "INSERT INTO product (product_name,sale_price) VALUES (?,?)"; 31 this.jdbcTemplate.update(sql,product.getProductName(),product.getSalePrice()); 32 } 33 34 @Override 35 public void delete(int id) { 36 String sql="DELETE FROM product WHERE id=?"; 37 this.jdbcTemplate.update(sql,id); 38 39 } 40 41 @Override 42 public void update(Product product) { 43 String sql="UPDATE product SET sale_price=? WHERE id=?"; 44 this.jdbcTemplate.update(sql,product.getSalePrice(),product.getId()); 45 } 46 47 @Override 48 public Product query(int id) { 49 String sql="SELECT id,product_name,sale_price FROM product WHERE id=?"; 50 Product product = this.jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper<Product>() { 51 public Product mapRow(ResultSet rs, int rowNum) throws SQLException { 52 Product product = new Product(); 53 product.setId(rs.getInt("id")); 54 product.setProductName(rs.getString("product_name")); 55 product.setSalePrice(rs.getInt("sale_price")); 56 return product; 57 } 58 }); 59 return product; 60 } 61 }
dao测试代码
1 package com.day02.ssm.spring.test; 2 3 import com.day02.ssm.spring.dao.impl.Product2Dao; 4 import com.day02.ssm.spring.model.Product; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 11 /** 12 * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 13 * 疑问咨询wx:851298348 14 */ 15 @RunWith(SpringJUnit4ClassRunner.class)//把junit加载到spring容器中去 16 @ContextConfiguration("classpath:spring-config_jdbcTemplat.xml") 17 public class TestProduct2Dao { 18 19 // private ProductDao productDao=new ProductDao(); 20 @Autowired 21 private Product2Dao productDao;//从spring中获取dao对象 22 23 @Test 24 public void test() { 25 Product product = new Product(); 26 product.setProductName("苹果22"); 27 product.setSalePrice(892); 28 productDao.save(product); 29 } 30 @Test 31 public void testDelete() { 32 productDao.delete(24); 33 } 34 35 @Test 36 public void testUpdate() { 37 Product product = new Product(); 38 product.setId(23); 39 product.setSalePrice(1000); 40 productDao.update(product); 41 } 42 @Test 43 public void testQuery() { 44 Product query = productDao.query(23); 45 System.out.println("query="+query); 46 } 47 48 }
32.spring对jdbc的支持jdbcTemplate之继承方式
注意:和上面原理一样,只是写法不一样
33.spring与mybatis集成方案1
依赖包:
xml文件配置:
映射文件:
dao实现:
34.spring与mybatis整合方案2(不写实现类)
34.spring与mybatis整合方案3(终极生产版本)
spring与mybatis整合终极版本代码:
整合的xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 <!-- 读取配置文件--> 10 <context:property-placeholder location="classpath:db.properties"/> 11 <!--配置dataSources--> 12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 13 <property name="driverClassName" value="${mysql.driverClass}"/> 14 <property name="url" value="${mysql.JdbcUrl}"/> 15 <property name="username" value="${mysql.User}"/> 16 <property name="password" value="${mysql.Password}"/> 17 </bean> 18 <!--整合mybatis--> 19 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 20 <!--数据源--> 21 <property name="dataSource" ref="dataSource"/> 22 <!--属性文件--> 23 <!--映射文件地址--> 24 <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/> 25 </bean> 26 27 <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 28 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 29 <property name="basePackage" value="com.day03.ssm.spring.mybatis.dao"/> 30 </bean> 31 32 <!-- <bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean"> 33 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 34 <property name="mapperInterface" value="com.day03.ssm.spring.mybatis.dao.IProduct2Dao"/> 35 </bean>--> 36 37 <!-- <bean id="productDao" class="com.day03.ssm.spring.mybatis.dao.impl.ProductDao"> 38 <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 39 </bean>--> 40 41 42 43 44 </beans>
mybatis映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <!-- 5 namespace:该映射文件的名字,理论上可以任意取 6 没有实现类时 命名空间必须为dao接口的权限命名地址 7 --> 8 <mapper namespace="com.day03.ssm.spring.mybatis.dao.IProduct2Dao"> 9 <insert id="save" parameterType="com.day03.ssm.spring.mybatis.model.Product"> 10 INSERT INTO product (product_name,sale_price) VALUES (#{productName},#{salePrice}) 11 </insert> 12 </mapper>
测试代码:
1 package com.day03.ssm.spring.mybatis.testDao; 2 3 import com.day03.ssm.spring.mybatis.dao.IProduct2Dao; 4 import com.day03.ssm.spring.mybatis.model.Product; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 11 /** 12 * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 13 * 疑问咨询wx:851298348 14 */ 15 @RunWith(SpringJUnit4ClassRunner.class)//把junit加载到spring容器中去 16 @ContextConfiguration("classpath:spring-config3.xml") 17 public class TestProduct3Dao { 18 @Autowired 19 private IProduct2Dao productDao; 20 21 @Test 22 public void testSave() { 23 Product product = new Product(); 24 product.setProductName("小米33"); 25 product.setSalePrice(789); 26 productDao.save(product); 27 } 28 }
以上是关于05_ssm基础之Spring与持久层的整合的主要内容,如果未能解决你的问题,请参考以下文章