标签:属性 加载 Nid 数据源配置 ast prepare pen cti java
最近工作中用到了mybatis的Java API方式进行开发,顺便也整理下该功能的用法,接下来会针对基本部分进行学习:
3)涉及到类型转化的用法;
4)批量修改、批量查询、批量新增的用法。
Mybatis官网给了具体的文档,但是并没有对以上用法具体介绍,因此在这里整理下,以便以后工作用到时,可以参考。
本章主要增、删、改、查的用法进行学习(本章将结合Spring自动注入《Spring(二十三):Spring自动注入的实现方式》),下边文章分为以下几个步骤:
1)新建maven,并引入spring/mybatis/mybatis-spring/mysql/druid/junit包,新增配置spring-config.xml/mybaits-config.xml/jdbc.properties配置文件;
2)在mysql中新建mydb,并创建article,article_category两张表;
3)新增用法;
4)修改用法;
5)查询用法;
6)删除用法。
新建一个maven项目(Learn-Spring-01,源码以上处至github:https://github.com/478632418/mybatis-spring-auto-annonation/tree/master/Learn-Spring-01),项目pom.xml中引入以下包:
spring相关包:
。。。。。
<org.springframework.version>5.2.0.RELEASE</org.springframework.version> <org.mybatis.version>3.4.6</org.mybatis.version> <com.alibaba.version>1.1.21</com.alibaba.version> <mysql.version>8.0.11</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency> <!-- Sping Test相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency> 。。。
mybatis、mybatis-spring包
<!--MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${org.mybatis.version}</version> </dependency> <!-- Mybatis自身实现的Spring整合依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency>
mysql和druid包
<!--MySql数据库驱动 --> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${com.alibaba.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>
junit包:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
新建以下类:
Mybatis Java API mapper类:
com.dx.test.mapper.sqlprovider.ArticleMapper.java
com.dx.test.mapper.sqlprovider.ArticleMapper.java
Mybatis Java API mapper的sql生成类:
com.dx.test.mapper.sqlprovider.ArticleSqlProvder.java
com.dx.test.mapper.sqlprovider.ArticleCategorySqlProvder.java
Repository层(类上都使用@Repository注解):
com.dx.test.repository.ArticleDao.java
com.dx.test.repository.ArticleCategoryDao.java
Service层(类上都使用@Service注解):
com.dx.test.service.ArticleService.java
com.dx.test.service.ArticleCategoryService.java
包含main入口类:
com.dx.test.App.java
在/src/main/resources下新建jdbc.properties、mybatis-config.xml、spring-config.xml配置文件:
1)jdbc.properties中配置mysql的user/pwd/url/driver/其他先关配置;
2)mybatis-config.xml配置mybatis配置;
3)spring-config.xml配置spring自动注入,以及mybatis整合。
jdbc.properties
#jdbc settings jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false jdbc.username=root jdbc.password=123456 #pool settings jdbc.pool.init=1 jdbc.pool.minIdle=3 jdbc.pool.maxActive=20 #jdbc.testSql=SELECT ‘x‘ jdbc.testSql=SELECT ‘x‘ FROM DUAL
mybatis-config.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> <!-- 打开延迟加载的开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 将积极加载改为消极加载(即按需加载) --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 打开全局缓存开关(二级缓存)默认值就是 true --> <setting name="cacheEnabled" value="true"/> <!--使用jdbc的getGeneratekeys获取自增主键值--> <setting name="useGeneratedKeys" value="true"/> <!--使用列别名替换别名 默认true select name as title form table; --> <setting name="useColumnLabel" value="true"/> <!--开启驼峰命名转换--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--打印sql日志--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- 引用db.properties配置文件 --> <!-- <properties resource="db.properties"/> <typeAliases> <package name="com.dx.test.model"/> </typeAliases> --> <!-- 对事务的管理和连接池的配置 --> <!-- <environments default="mysql_jdbc"> <environment id="oracle_jdbc"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> <environment id="mysql_jdbc"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> --> <!-- <mappers> <mapper resource="resources/mapper/TaskAutoExecutePlanMapper.xml"/> </mappers> --> <mappers> <mapper class="com.dx.test.mapper.ArticleCategoryMapper"></mapper> <mapper class="com.dx.test.mapper.ArticleMapper"></mapper> </mappers> </configuration>
备注:
1)因这里采用的是mybatis与spring整合,因此mysql先关数据源信息不在mybatis-config.xml中配置,而是配置spring-config.xml的dataSource的bean下;
2)mappers节点下的mapper需要指定mapper类,又因这里是采用的mybatis java api方式(只需要在ArticleCategoryMapper和ArticleMapper上添加上@Mapper注解即可),因此不再需要配置*Mapper.xml。
spring-config.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <!-- bean annotation driven --> <context:annotation-config /> <context:component-scan base-package="com.dx.test.repository,com.dx.test.mapper,com.dx.test.service" /> <!-- 配置整合Mybatis过程 --> <!-- 配置数据库相关参数 properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> --> <!--2.配置连接池属性 --> <!-- 数据源配置, 使用 Druid 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass --> <property name="driverClassName" value="${jdbc.driver}"/> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${jdbc.pool.init}"/> <property name="minIdle" value="${jdbc.pool.minIdle}"/> <property name="maxActive" value="${jdbc.pool.maxActive}"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="${jdbc.testSql}"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用) --> <!-- <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> --> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat"/> </bean> <!--3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!--配置mybatis全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 因为我们这采用的是Mybatis Java API方式,因此不需要配置 --> <property name="typeAliasesPackage" value="com.dx.test.model"/> <!--扫描entity包,使用别名,多个用;隔开 --> <!--扫描sql配置文件:mapper需要的xml文件 --> <!-- <property name="mapperLocations" value="classpath:mapper/*.xml"/> --> </bean> <!-- Mapper接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dx.test.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 如果想对单个mapper类关联上sqlSessionFactory,可以这么使用,具体参考:http://mybatis.org/spring/getting-started.html --> <!-- <bean id="articlerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.dx.test.dao.mapper.ArticleMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> --> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
备注:
上编码spring-config.xml配置部分包含两大部分配置:
1)spring自动注入配置;
2)spring注入sqlSessionFatcory到mybatis:
2.1)引入jdbc.properties配置信息到配置文件中;
2.2)dataSource bean的定义,这里采用的是com.alibaba.druid.pool.DruidDataSource作为dataSource实例;
2.3)使用sqlSessionFactory bean,需要引入mybatis-config.xml配置、dataSource bean实例;
2.4)使用org.mybatis.spring.mapper.MapperScannerConfigurer试下扫描mapper包路径下mapper,并关联上sqlSesssionFactory;
2.5)事务管理。
MyBatis(七):mybatis Java API编程实现增、删、改、查的用法
标签:属性 加载 Nid 数据源配置 ast prepare pen cti java
原文地址:https://www.cnblogs.com/yy3b2007com/p/11871145.html