Mybatis与Spring整合连接MySQL
2023-09-14 08:59:41 时间
import club.chuxing.tech.learn.mybatis.domain.Student;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
@Component
public interface StudentDao {
@Select("select * from student where id = #{id}")
Student selectStudent(int id);
}
MyBatis 与 Spring 整合原理分析 我们常常将 Spring 与 MyBatis 结合在一起使用,由于篇幅问题,上篇《MyBatis 快速整合 Spring》仅介绍了将 MyBatis 整合到 Spring 的方式,这篇在上篇的基础上总结出几个问题,并尝试通过分析其底层源码进行回答。
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载
此DAO不需要对应的Mapper文件。
5 Mybatis配置文件mybatis.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 settings !-- Globally enables or disables any caches configured in any mapper under this configuration -- setting name="cacheEnabled" value="true"/ !-- Sets the number of seconds the driver will wait for a response from the database -- setting name="defaultStatementTimeout" value="3000"/ !-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -- setting name="mapUnderscoreToCamelCase" value="true"/ !-- Allows JDBC support for generated keys. A compatible driver is required. This setting forces generated keys to be used if set to true, as some drivers deny compatibility but still work -- setting name="useGeneratedKeys" value="true"/ /settings !-- Continue going here -- typeAliases typeAlias alias="Student" type="club.chuxing.tech.learn.mybatis.domain.Student" / /typeAliases /configuration6 Spring配置文件
applicationContext-common.xml:
?xml version="1.0" encoding="UTF-8"? beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" context:annotation-config/ !--扫描包进行注入-- context:component-scan base-package="club.chuxing.tech.learn.mybatis.dao" / !--创建数据源dataSource对象并注入到SqlSessionFactoryBean对象中-- bean id="dataSource" property name="driverClassName" value="com.mysql.jdbc.Driver" / property name="url" value="jdbc:mysql://127.0.0.1:3306/test" / property name="username" value="root" / property name="password" value="" / /bean bean id="sqlSessionFactory" property name="dataSource" ref="dataSource" / property name="configLocation" value="classpath:mybatis.xml" / property name="mapperLocations" list value classpath:mybatis-mapper/*Mapper.xml /value /list /property /bean bean id="sqlSessionTemplate" constructor-arg ref="sqlSessionFactory"/ /bean !-- 配置扫描Mapper接口的包路径 -- bean !-- 如果是多个包名可用",; \t\n"中任意符号分隔开,详见:MapperScannerConfigurer[269行] -- property name="basePackage" value="club.chuxing.tech.learn.mybatis.dao"/ property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/ /bean bean id="transactionManager" property name="dataSource" ref="dataSource" / /bean /beans7 DAO单元测试
package club.chuxing.tech.learn.mybatis.dao; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import club.chuxing.tech.learn.mybatis.domain.Student; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static club.chuxing.tech.learn.utils.PrintUtil.*; @ContextConfiguration(locations = "classpath:applicationContext-common.xml") @RunWith(SpringJUnit4ClassRunner.class) public class StudentDaoTest { @Autowired private StudentDao dao; @Test public void testSelectStudent() throws Exception { Student student = dao.selectStudent(1); print(student); }
也可以在Main函数中加载DAO bean:
package club.chuxing.tech.learn.mybatis; import club.chuxing.tech.learn.mybatis.dao.StudentDao; import club.chuxing.tech.learn.mybatis.domain.Student; import org.springframework.context.support.ClassPathXmlApplicationContext; import static club.chuxing.tech.learn.utils.PrintUtil.print; public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-common.xml"); StudentDao dao = context.getBean(StudentDao.class); Student student = dao.selectStudent(1); print(student); }
转自:http://blog.csdn.net/foreverling/article/details/50987203
MyBatis 与 Spring 整合原理分析 我们常常将 Spring 与 MyBatis 结合在一起使用,由于篇幅问题,上篇《MyBatis 快速整合 Spring》仅介绍了将 MyBatis 整合到 Spring 的方式,这篇在上篇的基础上总结出几个问题,并尝试通过分析其底层源码进行回答。
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载
相关文章
- 使用 mysqldump 或 mydumper 配置 MySQL 主从复制
- MySQL通过 XtraBackup 备份恢复单个库
- Spring错误:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.B详解编程语言
- Maven+Spring+Spring MVC+MyBatis+MySQL整合SSM框架详解编程语言
- MySQL Variables old_passwords 数据库 参数变量解释及正确配置使用
- 快速学习: MySQL中复制数据库的技巧(mysql如何复制数据库)
- MySQL:Unlock the Power of Database Translations(mysql外文翻译)
- MySQL数据表查询:探索更多可能(mysql数据表的查询)
- MySQL中构建交叉表的基本操作(mysql交叉表)
- MySQL实现批量字符串替换(mysql替换所有字符串)
- MySQL:实现字段自增的简单方法(mysql让字段自增)
- C语言操作MySQL数据库入门实践(c引用mysql)
- MySQL与DNS的友好邂逅(mysqldns)
- 精选MySQL常用SQL语句,让你轻松执行数据库操作(mysql常用sql语句)
- MySQL远程使用:掌握基础知识,远程操作Mysql数据库。(mysql远程使用)
- 比较MySQL时间比较:优化计算任务(mysql时间之间)
- MySQL:开放数据库服务(mysql对外开放)
- 如何解决MySQL死锁问题?——25个中文字符的技巧指导(mysql死锁解决方法)
- MySQL参数详解,让你更加了解Mysql参数定义及优化。(mysql参数定义)
- MySQL如何查找重复数据(mysql查找数据重复)
- MySQL 事务查看:一探究竟(mysql事物查看)
- 来效率提升使用C语言开发的MySQL语句带来的效率大幅提升(c mysql 语句带)
- CMS与MySQL三角之殇(cms不支持mysql)
- MySQL如何实现10W并发请求(10w并发mysql)
- GET MYSQL 免费下载并破解MySQL数据库软件(mysql下载和破解)