整合Mybatis

步骤

导入相关jar包

  • junit
  • mybatis
  • mysql数据库
  • spring相关
  • aop织入
  • mybatis-spring

编写文件

第一种方式

spring配置文件

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://192.168.137.151:8066/TESTDB?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true"/><property name="username" value="root"/><property name="password" value="123456"/></bean><bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatisConfig.xml"/><property name="mapperLocations" value="classpath:com/qiyu/mapper/*.xml"/></bean><bean class="org.mybatis.spring.SqlSessionTemplate" id="sessionTemplate"><constructor-arg index="0" ref="sqlSessionFactory"/></bean><bean class="com.qiyu.mapper.StudentMapperImpl" id="studentMapper"><property name="sqlSessionTemplate" ref="sessionTemplate"/></bean>
</beans>

mybatis 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><properties resource="database.properties"/><typeAliases><package name="com.qiyu.pojo"/></typeAliases></configuration>
package com.qiyu.mapper;import com.qiyu.pojo.Student;/*** @program: Spring-study* @description:* @author: cxy* @create: 2021-06-03 14:40**/
public interface StudentMapper
{public Student getStudnetById(int  id);
}

接口类

package com.qiyu.mapper;import com.qiyu.pojo.Student;/*** @program: Spring-study* @description:* @author: cxy* @create: 2021-06-03 14:40**/
public interface StudentMapper
{public Student getStudnetById(int  id);
}

实现类

package com.qiyu.mapper;import com.qiyu.pojo.Student;
import org.mybatis.spring.SqlSessionTemplate;/*** @program: Spring-study* @description:* @author: cxy* @create: 2021-06-03 20:36**/
public class StudentMapperImpl implements StudentMapper
{private SqlSessionTemplate sqlSessionTemplate;public  void  setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){this.sqlSessionTemplate = sqlSessionTemplate;}@Overridepublic Student getStudnetById(int id) {StudentMapper studentMapper = sqlSessionTemplate.getMapper(StudentMapper.class);return  studentMapper.getStudnetById(id);}
}

mpper xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.qiyu.mapper.StudentMapper"><select id="getStudnetById" resultType="com.qiyu.pojo.Student">SELECT * FROM  mybatis_student WHERE id=#{id}</select>
</mapper>

测试

public class Test
{@org.junit.Testpublic  void test() throws IOException {String  resource = "mybatisConfig.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory =  new SqlSessionFactoryBuilder().build(inputStream);SqlSession session = sqlSessionFactory.openSession();StudentMapper student = session.getMapper(StudentMapper.class);Student student1 = student.getStudnetById(2);System.out.println(student1);session.close();}@org.junit.Testpublic void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");StudentMapper studentMapper = context.getBean("studentMapper",StudentMapper.class);Student strudnet = studentMapper.getStudnetById(1);System.out.println(strudnet);}
}
第二种方式

SqlSessionDaoSuppor

事务

  • CADI原则

    原子性

    唯一性

    隔离性

    持久性

<!--声明事务-->
<bean  id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><constructor-arg ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织入配置事务通知
-->
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager"><tx:attributes><!--给方法配置事务--><!---配置事务的传播特性 new propagation  默认是 propagation="REQUIRED" --><tx:method name="add" propagation="REQUIRED"/><tx:method name="*" propagation="REQUIRED"/></tx:attributes>
</tx:advice><!--切入事务-->
<aop:config><aop:pointcut id="transactionCut" expression="execution(* com.qiyu.mapper.*.*(..))"/><aop:advisor advice-ref="transactionInterceptor" pointcut-ref="transactionCut"/>
</aop:config>

springMybatis整合相关推荐

  1. Spring-Mybatis整合源码分析

    文章目录 Sprimg整合Mybatis步骤 Spring中的重要接口 BeanDefinition BeanDefinitionRegistry BeanFactory BeanFactoryPos ...

  2. Spring-Mybatis 整合的两种方式

    整合Mybatis 回顾Mybatis 步骤: 导入相关包 junit mybatis myssql spring aop mybatis-spring 编写配置文件 测试 导包 <depend ...

  3. Spring-Mybatis整合 从零开始

    创建项目 1.通过maven的方式创建一个项目 2.创建POJO对象 public class Hello {private String str;public void setStr(String ...

  4. Spring-Mybatis整合 第一个Spring-Mybatis程序

    1.pom.xml配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  5. 关于spring-mybatis整合出现的问题Error creating bean with name ‘sqlSessionFactory‘ defined in class path reso

    文章目录 问题原因 解决问题 网上找的方案 问题原因 今天学习spring时,出现这个问题,网上查了很多,大家错误各自不同,我一个一个改了之后还是报这个错误,总结了一下大家的改错方案,我发现百分之八十 ...

  6. spring mvc学习(60):ssm项目整合

    SSM整合 建立springmvc项目,先跑起来,再整合spring和mybatis 一.SpringMVC建立 1.新建maven工程,安装tomcat 2.导入pom <!-- spring ...

  7. Spring和Mybatis整合

    9. Spring和Mybatis整合 9.1 创建工程 新建工程,导入所需jar包: <dependencies><!-- mybatis核心包 --><depende ...

  8. springMVC第一天——入门、整合与参数绑定

    大纲摘要: 1.Springmvc介绍 2.入门程序 3.Springmvc架构讲解 a) 框架结构 b) 组件说明 4.Springmvc整合mybatis 5.参数绑定 乱码问题解决 a) Spr ...

  9. SqlMapConfig.xml

    mybatis的全局配置文件: 1.properties(属性) 将数据库连接参数单独配置在db.properties中,只需要在SqlMapConfig.xml中加载db.properties的属性 ...

最新文章

  1. Ubuntu15.04 网站服务器环境搭建,php/html/css等学习环境搭建教程
  2. 《基于张量网络的机器学习入门》学习笔记5
  3. pytorch中数组维度的理解
  4. 需求获取安排计划书_6分钟教你写一份融资计划书
  5. python requests cookie_python requests 带cookie访问页面
  6. c++中stack用法( 算法竞赛入门)
  7. 【渝粤教育】国家开放大学2018年春季 0281-21T色装概论 参考试题
  8. 软件工程概述思维导图总结(一)
  9. 常见驱动程序相关知识
  10. 常用的网站建设程序有哪些?
  11. Anroid app版本更新
  12. ArduPilot添加新的惯导方案
  13. 【游戏设计模式】之三 状态模式、有限状态机
  14. redis mysql qps_测算Redis处理实际生产请求的QPS/TPS
  15. 初二因式分解奥数竞赛题_初中数学因式分解(含答案)竞赛题精选.doc
  16. 1-十六烷基-3-三乙氧基丙基硅烷咪唑溴盐离子液体([HDTIm]Br)和1-十八烷基-3-三乙氧基丙基硅烷咪唑溴盐离子液体([ODTIm]Br)修饰磁性纳米颗粒(MNPs)
  17. SAP 销售订单中的成本中心
  18. Android N音频播放延迟
  19. JZ45,46,47,48
  20. python123第四章第五题_第五章 Python 函数

热门文章

  1. python语言高空坠球_高空坠球砸中婴儿,整栋楼要赔钱,若孩子还在,现在应上幼儿园了...
  2. bzoj乱刷计划2 19/20
  3. 服务注册中心Nacos
  4. 弘辽科技:淘宝店铺如何添加客服?在哪添加客服?
  5. 授予用户在此计算机的请求登录,解决未授予用户在此计算机上的请求登录类型方法...
  6. 受限波尔兹曼(Boltzmann)机简介
  7. 337、用三角形triangle、方形square、圆形circle等“基本图形“来表示任意的 图形 如菱形 半圆
  8. 记一次应用破解——脱壳修改后重打包
  9. 如何在代码中查看服务器是否能够ping通
  10. 用IDEA进行Java后台开发(二)