p14

p15 Spring整合Mybatis思路,及SqlSessionDaoSupport整合方式

5. 之前使用MyBatis:conf.xml ->SqlSessionFactory

现在整合的时候,需要通过spring管理SqlSessionFactory,因此产生SqlSessionFactory所需要的数据库信息,不再放入conf.xml, 而要放入spring中

配置spring配置文件(applicationContext.xml)

p16

6. 使用Spring-MyBatis整合产物开发程序

目标:通过spring产生mybatis最终操作需要的,动态mapper对象(StudentMapper对象)

spring产生动态mapper对象,有3种方法:

StudentDaoImpl.java

package org.lanqiao.dao.impl;import org.apache.ibatis.session.SqlSession;
import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {@Overridepublic void addStudent(Student student) {SqlSession session = super.getSqlSession() ;StudentMapper stuDao = session.getMapper(StudentMapper.class) ;stuDao.addStudent(student);}}

Student.java

package org.lanqiao.entity;public class Student {private int stuNo;private String stuName;private int stuAge ;public int getStuNo() {return stuNo;}public void setStuNo(int stuNo) {this.stuNo = stuNo;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;} }

StudentMapper.java

package org.lanqiao.mapper;import org.lanqiao.entity.Student;public interface StudentMapper {public void addStudent(Student student  );
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.lanqiao.mapper.StudentMapper"><select id="queryStudentByStuno"    parameterType="int"      resultType="org.lanqiao.entity.Student"  >select * from student where stuno = #{stuNo}</select><insert id="addStudent" parameterType="org.lanqiao.entity.Student" >insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge})</insert></mapper>

IStudentService.java

package org.lanqiao.service;import org.lanqiao.entity.Student;public interface IStudentService {public void addStudent(Student student);
}

StudentServiceImpl.java

package org.lanqiao.service.impl;import org.lanqiao.entity.Student;
import org.lanqiao.mapper.StudentMapper;
import org.lanqiao.service.IStudentService;public class StudentServiceImpl implements IStudentService {private StudentMapper studentMapper ;public void setStudentMapper(StudentMapper studentMapper) {this.studentMapper = studentMapper;}@Overridepublic void addStudent(Student student) {//����dao��studentMapper.addStudent(student);}}

Test.java

package org.lanqiao.test;import org.lanqiao.entity.Student;
import org.lanqiao.service.IStudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");IStudentService studentService = (IStudentService)context.getBean("studentService") ;Student student = new Student();student.setStuAge(333);student.setStuName("33s9");student.setStuNo(339);studentService.addStudent(student);}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 加载db.properties文件 --><bean  id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="locations"><array><value>classpath:db.properties</value></array></property></bean><!--  第一种方式生成mapper对象<bean id="studentMapper" class="org.lanqiao.dao.impl.StudentDaoImpl">将SPring配置的sqlSessionFactory 对象交给mapper(dao) <property name="sqlSessionFactory" ref="sqlSessionFacotry"></property></bean>--><!-- 第二种方式生成mapper对象 <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="org.lanqiao.mapper.StudentMapper"></property><property name="sqlSessionFactory" ref="sqlSessionFacotry"></property></bean>--><!-- 第三种方式生成mapper对象(批量产生多个mapper)批量产生Mapper对在SpringIOC中的 id值 默认就是  首字母小写接口名 (首字母小写的接口名=id值)--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property><!--指定批量产生 哪个包中的mapper对象--><property name="basePackage" value="org.lanqiao.mapper"></property></bean><bean id="studentService" class="org.lanqiao.service.impl.StudentServiceImpl"><property name="studentMapper" ref="studentMapper"></property></bean><!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><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><!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory --><bean id="sqlSessionFacotry" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><!-- 加载mybatis配置文件 <property name="configLocation" value="classpath:conf.xml"></property>--><!-- 加载mapper.xml路径 --><property name="mapperLocations" value="org/lanqiao/mapper/*.xml"></property></bean></beans>

conf.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 数据库信息 -->   <!-- 加载映射文件 studentMapper.xml<mappers><mapper resource="org/lanqiao/mapper/studentMapper.xml"/></mappers>-->
</configuration>

db.properties

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=scott
password=tiger
maxIdle=1000
maxActive=500

《Spring视频教程》(p14~p16)相关推荐

  1. 尚学堂Spring视频教程(二):Spring控制反转

    用Spring来实现IOC 在上节中我们自定义了一个接口BeanFactory和类ClassPathXmlApplicationContext来模拟Spring,其实它们在Spring中确实是存在的, ...

  2. spring框架教程 Spring开发实例 spring mvc视频教程下载

    基于SpringMVC.MyBatis.FreeMarker架构实战CMS大型门户网站(自定义模板) spring框架教程 Spring开发实例 spring mvc视频教程下载地址: http:// ...

  3. 【超详细】SSM框架项目实战|Spring+Mybatis+Springmvc框架项目实战整合-【CRM客户管理系统】——课程笔记

    相关资料网盘链接: CRM客户管理系统资料 提取码 :0u04 P1--CRM阶段简介: web项目开发:如何分析,设计,编码,测试.        形成编程思想和编程习惯. P2--CRM的技术架构 ...

  4. 分享一百多套开发视频教程的下载地址(转)

    1. 北京圣思Java培训教学视频(资源共享网) 2.Lucene/WebService/SVN/Ant/SpringMVC视频(学习资料库网) 3.JUnit和Ant视频教程(VeryCD社区) 4 ...

  5. 【视频教程免费领取】聚焦Python分布式爬虫必学框架Scrapy 打造搜索引擎

    领取方式 关注公众号,发送Python0407获取下载链接. 扫码关注公众号,公众号回复 Python0407 获取下载地址 目录结构 目录:/读书ReadBook [57.6G] ┣━━48G全套J ...

  6. Day 9 : Spring框架网易云课堂教程1~22

    Spring视频教程 JavaGuide中对Spring一些资料的整理 准备跟着网易云课堂的Spring教程先过一遍Spring框架的基础知识. 但是感觉视频有点过时,所以不用跟着视频教程把所有代码和 ...

  7. 2014年末最强悍IT学习视频教程分享

    (1)一年又时尽,今天给大家分享最近一年的积蓄!喜欢的请点赞支持 说明:由于使用的网盘可能无法分享视频连接,所以如果连接失效需要的请留言,说明哪一个视频,和QQ邮箱地址,我会尽快回复! Ajax和jQ ...

  8. 【视频教程免费领取】48G全套Java视频教程,从入门到跑路!

    领取方式 关注公众号,发送java0407获取下载链接. 扫码关注公众号,公众号回复 java0407 获取下载地址 目录结构 目录:/读书ReadBook [57.6G] ┣━━48G全套Java视 ...

  9. Spring注解大全,最后一个经常容易记不住

    前言 随着技术的更新迭代,Java5.0开始支持注解.而作为java中的领军框架Spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制Spring框架. 而Spring的的 ...

最新文章

  1. AD下批量导入域用户
  2. mega_[MEGA DEAL]带有Kotlin捆绑包的完整Android Oreo(95%折扣)
  3. Naveen Tewari先生荣获艾奇奖“年度商业创新领袖人物”
  4. 顺序表的所有基本操作
  5. WebGL(五)——WEBGL缓冲区,绘制三角形
  6. 小米5安卓使用微信X5 Blink内核调试
  7. 虚拟视频驱动程序vivi.c源码分析
  8. python用字典统计出现次数_python 字典(dict)列表(list),统计重复出现字典的数量...
  9. 正则表达式中原子的5种类型
  10. 图像处理中的深度学习技术
  11. JTree创建、获取和删除节点的方法
  12. 「技术选型」Solr与ES难以抉择?且看第一回
  13. 【STM32H750】玩转ART-Pi(八)——添加动态模块
  14. 微信小程序社区疫情防控+后台管理系统|前后分离VUE
  15. (转)Winton:如何在100多个期货市场交易
  16. c 语言怎么实现可视化编程,自定义编程语言的实现
  17. MCU-CPU-GPU-APU系列
  18. 七月算法机器学习笔记4 凸优化
  19. 微信充值钱数更改但微信充值单钱数不变的问题
  20. 【C语言进阶】很诡异的编译报错expected declaration or statement at end of input

热门文章

  1. python 计算两个经纬度的距离_python 通过两个点的经纬度计算距离
  2. arcgisengine 线转面方法
  3. 学生出国参加会议(西班牙商务签流程)
  4. poco遇到的问题及解决办法
  5. Excel IF 函数多条件判断
  6. 意大利菜--斐波那契汤。具体做法是把昨天的和前天剩下的汤加热后混合,得到就是今天新鲜的“斐波那契汤”
  7. java斐波那契优化_用HashMap优化斐波那契数列 java算法
  8. 码农干货系列【10】--光线追踪进阶:javascript玩转3D纹理映射
  9. OCTL _IO,_IOW,_IOWR 介绍
  10. 9个最实用的PS插件盘点!